Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit 1eb9f92

Browse files
committed
Extract parsing to its own files
1 parent fb8d732 commit 1eb9f92

4 files changed

Lines changed: 272 additions & 250 deletions

File tree

cli/cli.go

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package cli
22

33
import (
4-
"bufio"
54
"fmt"
65
"io/ioutil"
76
"os"
87
"path/filepath"
9-
"regexp"
10-
"strconv"
118
"strings"
129
"syscall"
1310

@@ -229,105 +226,6 @@ func visitDir(path string) ([]*cobra.Command, error) {
229226
return cmds, nil
230227
}
231228

232-
/*
233-
234-
Looks for a line like this:
235-
236-
# name-of-the-file: short description.
237-
238-
*/
239-
func shortDescriptionFrom(path string) (string, error) {
240-
file, err := os.Open(path)
241-
if err != nil {
242-
return "", err
243-
}
244-
defer func() {
245-
err = file.Close()
246-
if err != nil {
247-
logrus.Error(err)
248-
}
249-
}()
250-
251-
r := regexp.MustCompile(fmt.Sprintf(`^# %s: (.*)$`, regexp.QuoteMeta(filepath.Base(path))))
252-
scanner := bufio.NewScanner(file)
253-
for scanner.Scan() {
254-
match := r.FindStringSubmatch(scanner.Text())
255-
if len(match) == 2 {
256-
logrus.Debug("Found short description line: ", filepath.Join(path), ", set to: ", match[1])
257-
return match[1], nil
258-
}
259-
}
260-
return "", nil
261-
}
262-
263-
/*
264-
Argument validators. Looks for a line like this:
265-
266-
# args: 3
267-
268-
*/
269-
func argsFrom(path string) (cobra.PositionalArgs, error) {
270-
file, err := os.Open(path)
271-
if err != nil {
272-
return nil, err
273-
}
274-
defer func() {
275-
err = file.Close()
276-
if err != nil {
277-
logrus.Error(err)
278-
}
279-
}()
280-
281-
r := regexp.MustCompile(`^# args: (\d+)$`)
282-
scanner := bufio.NewScanner(file)
283-
for scanner.Scan() {
284-
match := r.FindStringSubmatch(scanner.Text())
285-
if len(match) == 2 {
286-
logrus.Debug("Found example line: ", filepath.Join(path), ", set to: ", match[1])
287-
i, err := strconv.ParseUint(match[1], 10, 32)
288-
if err != nil {
289-
return nil, err
290-
}
291-
if i == 0 {
292-
return cobra.NoArgs, nil
293-
}
294-
return cobra.ExactArgs(int(i)), nil
295-
}
296-
}
297-
return cobra.ArbitraryArgs, nil
298-
}
299-
300-
/*
301-
302-
Looks for a line like this:
303-
304-
# example: foo bar 1 2 3
305-
306-
*/
307-
func exampleFrom(path string) (string, error) {
308-
file, err := os.Open(path)
309-
if err != nil {
310-
return "", err
311-
}
312-
defer func() {
313-
err = file.Close()
314-
if err != nil {
315-
logrus.Error(err)
316-
}
317-
}()
318-
319-
r := regexp.MustCompile(`^# example: (.*)$`)
320-
scanner := bufio.NewScanner(file)
321-
for scanner.Scan() {
322-
match := r.FindStringSubmatch(scanner.Text())
323-
if len(match) == 2 {
324-
logrus.Debug("Found example line: ", filepath.Join(path), ", set to: ", match[1])
325-
return " sd " + match[1], nil
326-
}
327-
}
328-
return "", nil
329-
}
330-
331229
func commandFromScript(path string) (*cobra.Command, error) {
332230
shortDesc, err := shortDescriptionFrom(path)
333231
if err != nil {

cli/cli_test.go

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -121,154 +121,6 @@ func TestDeduplicate(t *testing.T) {
121121
})
122122
}
123123

124-
func TestShortDescriptionFrom(t *testing.T) {
125-
t.Run("happy path", func(t *testing.T) {
126-
f, err := ioutil.TempFile("", "test-short-description-ok")
127-
assert.NoError(t, err)
128-
129-
f.WriteString(fmt.Sprintf("#\n# %s: blah\n#\n", filepath.Base(f.Name())))
130-
defer func() {
131-
f.Close()
132-
os.Remove(f.Name())
133-
}()
134-
135-
v, err := shortDescriptionFrom(f.Name())
136-
assert.NoError(t, err)
137-
assert.Equal(t, "blah", v)
138-
})
139-
t.Run("missing", func(t *testing.T) {
140-
f, err := ioutil.TempFile("", "test-short-description-missing")
141-
assert.NoError(t, err)
142-
143-
f.WriteString("#\n#\n#\n")
144-
defer func() {
145-
f.Close()
146-
os.Remove(f.Name())
147-
}()
148-
149-
v, err := shortDescriptionFrom(f.Name())
150-
assert.NoError(t, err)
151-
assert.Equal(t, "", v)
152-
})
153-
}
154-
155-
func TestArgsFrom(t *testing.T) {
156-
t.Run("happy path with 3 args", func(t *testing.T) {
157-
f, err := ioutil.TempFile("", "test-args3-ok")
158-
assert.NoError(t, err)
159-
160-
f.WriteString("#\n# args: 2\n#\n")
161-
defer func() {
162-
f.Close()
163-
os.Remove(f.Name())
164-
}()
165-
166-
v, err := argsFrom(f.Name())
167-
assert.NoError(t, err)
168-
assert.Error(t, v(&cobra.Command{}, []string{}))
169-
assert.Error(t, v(&cobra.Command{}, []string{"first"}))
170-
assert.NoError(t, v(&cobra.Command{}, []string{"first", "second"}))
171-
assert.Error(t, v(&cobra.Command{}, []string{"first", "second", "third"}))
172-
})
173-
174-
t.Run("happy path with no args", func(t *testing.T) {
175-
f, err := ioutil.TempFile("", "test-args0-ok")
176-
assert.NoError(t, err)
177-
178-
f.WriteString("#\n# args: 0\n#\n")
179-
defer func() {
180-
f.Close()
181-
os.Remove(f.Name())
182-
}()
183-
184-
v, err := argsFrom(f.Name())
185-
assert.NoError(t, err)
186-
assert.NoError(t, v(&cobra.Command{}, []string{}))
187-
assert.Error(t, v(&cobra.Command{}, []string{"first"}))
188-
assert.Error(t, v(&cobra.Command{}, []string{"first", "second"}))
189-
})
190-
191-
t.Run("missing", func(t *testing.T) {
192-
f, err := ioutil.TempFile("", "test-args-missing")
193-
assert.NoError(t, err)
194-
195-
f.WriteString("#\n#\n#\n")
196-
defer func() {
197-
f.Close()
198-
os.Remove(f.Name())
199-
}()
200-
201-
v, err := argsFrom(f.Name())
202-
assert.NoError(t, err)
203-
assert.NoError(t, v(&cobra.Command{}, []string{}))
204-
assert.NoError(t, v(&cobra.Command{}, []string{"first", "second"}))
205-
})
206-
207-
t.Run("bad value: not numeric", func(t *testing.T) {
208-
f, err := ioutil.TempFile("", "test-args-not-numeric")
209-
assert.NoError(t, err)
210-
211-
f.WriteString("#\n# args: banana\n#\n")
212-
defer func() {
213-
f.Close()
214-
os.Remove(f.Name())
215-
}()
216-
217-
v, err := argsFrom(f.Name())
218-
assert.NoError(t, err)
219-
assert.NoError(t, v(&cobra.Command{}, []string{}))
220-
assert.NoError(t, v(&cobra.Command{}, []string{"first", "second"}))
221-
})
222-
223-
t.Run("bad value: too large", func(t *testing.T) {
224-
f, err := ioutil.TempFile("", "test-args-too-large")
225-
assert.NoError(t, err)
226-
227-
f.WriteString("#\n# args: 999999999999999999999999999999999999999999999999999999999999999999999999\n#\n")
228-
defer func() {
229-
f.Close()
230-
os.Remove(f.Name())
231-
}()
232-
233-
v, err := argsFrom(f.Name())
234-
if !assert.Error(t, err) {
235-
assert.NoError(t, v(&cobra.Command{}, []string{}))
236-
assert.NoError(t, v(&cobra.Command{}, []string{"first", "second"}))
237-
}
238-
})
239-
}
240-
241-
func TestExampleFrom(t *testing.T) {
242-
t.Run("happy path", func(t *testing.T) {
243-
f, err := ioutil.TempFile("", "test-example-ok")
244-
assert.NoError(t, err)
245-
246-
f.WriteString("#\n# example: blah\n#\n")
247-
defer func() {
248-
_ = f.Close()
249-
_ = os.Remove(f.Name())
250-
}()
251-
252-
v, err := exampleFrom(f.Name())
253-
assert.NoError(t, err)
254-
assert.Equal(t, " sd blah", v)
255-
})
256-
t.Run("missing", func(t *testing.T) {
257-
f, err := ioutil.TempFile("", "test-example-missing")
258-
assert.NoError(t, err)
259-
260-
f.WriteString("#\n#\n#\n")
261-
defer func() {
262-
_ = f.Close()
263-
_ = os.Remove(f.Name())
264-
}()
265-
266-
v, err := exampleFrom(f.Name())
267-
assert.NoError(t, err)
268-
assert.Equal(t, "", v)
269-
})
270-
}
271-
272124
func TestCommandFromScript(t *testing.T) {
273125
t.Run("happy path", func(t *testing.T) {
274126
f, err := ioutil.TempFile("", "test-command-from-script")

cli/parsing.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package cli
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"strconv"
10+
11+
"github.com/Sirupsen/logrus"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
/*
16+
17+
Looks for a line like this:
18+
19+
# name-of-the-file: short description.
20+
21+
*/
22+
func shortDescriptionFrom(path string) (string, error) {
23+
file, err := os.Open(path)
24+
if err != nil {
25+
return "", err
26+
}
27+
defer func() {
28+
err = file.Close()
29+
if err != nil {
30+
logrus.Error(err)
31+
}
32+
}()
33+
34+
r := regexp.MustCompile(fmt.Sprintf(`^# %s: (.*)$`, regexp.QuoteMeta(filepath.Base(path))))
35+
scanner := bufio.NewScanner(file)
36+
for scanner.Scan() {
37+
match := r.FindStringSubmatch(scanner.Text())
38+
if len(match) == 2 {
39+
logrus.Debug("Found short description line: ", filepath.Join(path), ", set to: ", match[1])
40+
return match[1], nil
41+
}
42+
}
43+
return "", nil
44+
}
45+
46+
/*
47+
48+
Looks for a line like this:
49+
50+
# example: foo bar 1 2 3
51+
52+
*/
53+
func exampleFrom(path string) (string, error) {
54+
file, err := os.Open(path)
55+
if err != nil {
56+
return "", err
57+
}
58+
defer func() {
59+
err = file.Close()
60+
if err != nil {
61+
logrus.Error(err)
62+
}
63+
}()
64+
65+
r := regexp.MustCompile(`^# example: (.*)$`)
66+
scanner := bufio.NewScanner(file)
67+
for scanner.Scan() {
68+
match := r.FindStringSubmatch(scanner.Text())
69+
if len(match) == 2 {
70+
logrus.Debug("Found example line: ", filepath.Join(path), ", set to: ", match[1])
71+
return " sd " + match[1], nil
72+
}
73+
}
74+
return "", nil
75+
}
76+
77+
/*
78+
Argument validators. Looks for a line like this:
79+
80+
# args: 3
81+
82+
*/
83+
func argsFrom(path string) (cobra.PositionalArgs, error) {
84+
file, err := os.Open(path)
85+
if err != nil {
86+
return nil, err
87+
}
88+
defer func() {
89+
err = file.Close()
90+
if err != nil {
91+
logrus.Error(err)
92+
}
93+
}()
94+
95+
r := regexp.MustCompile(`^# args: (\d+)$`)
96+
scanner := bufio.NewScanner(file)
97+
for scanner.Scan() {
98+
match := r.FindStringSubmatch(scanner.Text())
99+
if len(match) == 2 {
100+
logrus.Debug("Found example line: ", filepath.Join(path), ", set to: ", match[1])
101+
i, err := strconv.ParseUint(match[1], 10, 32)
102+
if err != nil {
103+
return nil, err
104+
}
105+
if i == 0 {
106+
return cobra.NoArgs, nil
107+
}
108+
return cobra.ExactArgs(int(i)), nil
109+
}
110+
}
111+
return cobra.ArbitraryArgs, nil
112+
}

0 commit comments

Comments
 (0)