Skip to content

Commit 40d85eb

Browse files
committed
feat: cli application with go
1 parent d16d9e5 commit 40d85eb

File tree

11 files changed

+1095
-0
lines changed

11 files changed

+1095
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.log
22
.vscode
33
.metals
4+
bin

go/cli/cmd/dirs.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmd
2+
3+
import (
4+
"cli/common"
5+
"cli/dirs"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
)
10+
11+
var Depth int
12+
var Mindirsize int
13+
14+
// dirsCmd represents the dirs command
15+
var dirsCmd = &cobra.Command{
16+
Use: "dirs",
17+
Short: "Show the largest directories in the given path.",
18+
Long: `Quickly scan a directory and find large directories. Use the flags below to target the output.`,
19+
Run: func(cmd *cobra.Command, args []string) {
20+
if Debug {
21+
common.LogFlags()
22+
}
23+
dirsFound, _ := dirs.ReadDirDepth(Path)
24+
dirs.PrintResults(dirsFound)
25+
},
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(dirsCmd)
30+
31+
dirsCmd.PersistentFlags().IntVarP(&Depth, "depth", "", 2, "Depth of directory tree to display")
32+
viper.BindPFlag("depth", dirsCmd.PersistentFlags().Lookup("depth"))
33+
34+
dirsCmd.PersistentFlags().IntVarP(&Mindirsize, "mindirsize", "", 100, "Only display directories larger than this threshold in MB.")
35+
viper.BindPFlag("mindirsize", dirsCmd.PersistentFlags().Lookup("mindirsize"))
36+
}

go/cli/cmd/files.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"cli/common"
7+
"cli/files"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
)
12+
13+
var Path string
14+
var Filecount int
15+
var Minfilesize int64
16+
17+
// filesCmd represents the files command
18+
var filesCmd = &cobra.Command{
19+
Use: "files",
20+
Short: "Show the largest files in the given path.",
21+
Long: `Quickly scan a directory and find large files. . Use the flags below to target the output.`,
22+
Run: func(cmd *cobra.Command, args []string) {
23+
if Debug {
24+
common.LogFlags()
25+
}
26+
27+
filesFound, err := files.ReadDirRecursively(Path)
28+
if err != nil {
29+
fmt.Println(err)
30+
return
31+
}
32+
33+
if Filecount > len(filesFound) {
34+
Filecount = len(filesFound)
35+
}
36+
37+
filesFound = filesFound[0:Filecount]
38+
files.PrintResults(filesFound)
39+
},
40+
}
41+
42+
func init() {
43+
rootCmd.AddCommand(filesCmd)
44+
45+
filesCmd.PersistentFlags().IntVarP(&Filecount, "filecount", "f", 10, "Limit the number of files returned")
46+
viper.BindPFlag("filecount", filesCmd.PersistentFlags().Lookup("filecount"))
47+
48+
filesCmd.PersistentFlags().Int64VarP(&Minfilesize, "minfilesize", "", 50, "Minimum size for files in search in MB.")
49+
viper.BindPFlag("minfilesize", filesCmd.PersistentFlags().Lookup("minfilesize"))
50+
}

go/cli/cmd/root.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/spf13/viper"
8+
)
9+
10+
// rootCmd represents the base command when called without any subcommands
11+
var Verbose bool
12+
var Debug bool
13+
var Highlight int
14+
15+
var rootCmd = &cobra.Command{
16+
Use: "getsize",
17+
Short: "List the size of a local directory.",
18+
Long: `This command will display the size of a directory with several different options.`,
19+
}
20+
21+
// Execute adds all child commands to the root command and sets flags appropriately.
22+
// This is called by main.main(). It only needs to happen once to the rootCmd.
23+
func Execute() {
24+
err := rootCmd.Execute()
25+
if err != nil {
26+
os.Exit(1)
27+
}
28+
}
29+
30+
func init() {
31+
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "Display more verbose output in console output. (default: false)")
32+
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
33+
34+
rootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "Display debugging output in the console. (default: false)")
35+
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
36+
37+
rootCmd.PersistentFlags().IntVarP(&Highlight, "highlight", "", 500, "Highlight files/directories over this threshold, in MB")
38+
viper.BindPFlag("highlight", rootCmd.PersistentFlags().Lookup("highlight"))
39+
40+
rootCmd.PersistentFlags().StringVarP(&Path, "path", "p", "", "Define the path to scan.")
41+
rootCmd.MarkPersistentFlagRequired("path")
42+
viper.BindPFlag("path", rootCmd.PersistentFlags().Lookup("path"))
43+
}

0 commit comments

Comments
 (0)