-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (76 loc) · 2.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"os"
"errors"
"path/filepath"
"math/rand/v2"
"github.com/spf13/cobra"
)
var path string
var rootCmd = &cobra.Command{
Use: "randfile",
Short: "randfile picks a random file from a directory path",
Long: `randfile picks a random file from a directory path.`,
Run: func(cmd *cobra.Command, args []string) {
if path != "" {
filePaths, err := directoryPathContents(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
chosenPath, err := randFile(filePaths)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(chosenPath)
}
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of randfile",
Long: `Print the version number of randfile`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("randfile v0.1")
},
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
rootCmd.AddCommand(versionCmd)
rootCmd.Flags().StringVarP(&path, "path", "p", "", "directory path to read from")
}
func directoryPathContents(dirPath string) (filePaths []string, err error) {
absPath, err := filepath.Abs(dirPath)
if os.IsNotExist(err) {
return nil, errors.New("Path does not exist!")
} else if err != nil {
fmt.Println(err)
}
pathContents, err := os.ReadDir(absPath)
if err != nil {
fmt.Println(err)
}
for _, pathObject := range pathContents {
if !pathObject.IsDir() {
fullPath := filepath.Join(absPath, pathObject.Name())
filePaths = append(filePaths, fullPath)
}
}
return filePaths, nil
}
func randFile(filePaths []string) (filePath string, err error) {
count := len(filePaths)
if count <= 0 {
return "", errors.New("Number of files and directories is 0.")
}
filePathIdx := rand.IntN(count)
filePath = filePaths[filePathIdx]
return filePath, nil
}