@@ -7,32 +7,36 @@ import (
7
7
"github.com/spf13/cobra"
8
8
)
9
9
10
- var rootCmd = & cobra.Command {
11
- Use : "go-ls" ,
12
- Short : "go-ls is a re-implementation of the ls command" ,
13
- RunE : func (cmd * cobra.Command , args []string ) error {
14
- // By default, ls the current directory
15
- dir := "."
16
- // If an argument was passed, use the first as our directory
17
- if len (args ) > 0 {
18
- dir = args [0 ]
19
- }
10
+ func NewRoodCmd () * cobra.Command {
11
+ return & cobra.Command {
12
+ Use : "go-ls" ,
13
+ Short : "go-ls is a re-implementation of the ls command" ,
14
+ Args : cobra .ArbitraryArgs ,
15
+ RunE : func (cmd * cobra.Command , args []string ) error {
16
+ // By default, ls the current directory
17
+ dir := "."
18
+ // If an argument was passed, use the first as our directory
19
+ if len (args ) > 0 {
20
+ dir = args [0 ]
21
+ }
20
22
21
- // Read this directory to get a list of files
22
- // https://pkg.go.dev/os#ReadDir
23
- files , err := os .ReadDir (dir )
24
- if err != nil {
25
- return err
26
- }
27
- // Iterate through each file in the directory, printing the file name
28
- for _ , file := range files {
29
- fmt .Println (file .Name ())
30
- }
31
- return nil
32
- },
23
+ // Read this directory to get a list of files
24
+ // https://pkg.go.dev/os#ReadDir
25
+ files , err := os .ReadDir (dir )
26
+ if err != nil {
27
+ return err
28
+ }
29
+ // Iterate through each file in the directory, printing the file name
30
+ for _ , file := range files {
31
+ fmt .Fprintln (cmd .OutOrStdout (), file .Name ())
32
+ }
33
+ return nil
34
+ },
35
+ }
33
36
}
34
37
35
38
func Execute () {
39
+ rootCmd := NewRoodCmd ()
36
40
if err := rootCmd .Execute (); err != nil {
37
41
fmt .Fprintln (os .Stderr , err )
38
42
os .Exit (1 )
0 commit comments