Skip to content

Commit edfa9ba

Browse files
committed
accept an argument
1 parent 882e0f4 commit edfa9ba

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cli-files/go-ls/cmd/root.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ var rootCmd = &cobra.Command{
1111
Use: "go-ls",
1212
Short: "go-ls is a re-implementation of the ls command",
1313
RunE: func(cmd *cobra.Command, args []string) error {
14-
files, err := os.ReadDir(".")
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+
}
20+
21+
// Read this directory to get a list of files
22+
// https://pkg.go.dev/os#ReadDir
23+
files, err := os.ReadDir(dir)
1524
if err != nil {
1625
return err
1726
}
27+
// Iterate through each file in the directory, printing the file name
1828
for _, file := range files {
1929
fmt.Println(file.Name())
2030
}

0 commit comments

Comments
 (0)