@@ -10,6 +10,7 @@ import (
1010 "strings"
1111 "syscall"
1212
13+ "github.com/Sirupsen/logrus"
1314 "github.com/spf13/cobra"
1415)
1516
@@ -20,25 +21,49 @@ var (
2021)
2122
2223func main () {
24+ var err error
25+
2326 root := & cobra.Command {
2427 Use : "sd" ,
2528 Version : version ,
2629 }
30+ root .AddCommand (completions (root ))
31+ root .PersistentFlags ().BoolP ("debug" , "d" , false , "Turn debugging on/off" )
32+
33+ root .ParseFlags (os .Args )
2734
28- err := loadCommandsInto (root )
35+ debug , err := root .PersistentFlags ().GetBool ("debug" )
36+ if err != nil {
37+ panic (err )
38+ }
39+
40+ if debug {
41+ logrus .SetLevel (logrus .DebugLevel )
42+ }
43+
44+ err = loadCommandsInto (root )
2945 if err != nil {
3046 panic (err )
3147 }
3248
33- root .AddCommand (completions (root ))
3449 root .Execute ()
3550}
3651
3752func loadCommandsInto (root * cobra.Command ) error {
53+ logrus .Debug ("Loading commands started" )
54+
3855 home := filepath .Join (os .Getenv ("HOME" ), ".sd" )
56+ logrus .Debug ("HOME is set to: " , home )
57+
58+ wd , err := os .Getwd ()
59+ if err != nil {
60+ return err
61+ }
62+
63+ logrus .Debug ("Current working dir is set to: " , wd )
3964
40- wd , _ := os .Getwd ()
4165 current := filepath .Join (wd , "scripts" )
66+ logrus .Debug ("Looking for ./scripts in: " , current )
4267
4368 for _ , path := range []string {home , current } {
4469 cmds , err := visitDir (path )
@@ -51,13 +76,16 @@ func loadCommandsInto(root *cobra.Command) error {
5176 }
5277 }
5378
79+ logrus .Debug ("Loading commands done" )
5480 return nil
5581}
5682
5783func visitDir (path string ) ([]* cobra.Command , error ) {
84+ logrus .Debug ("Visiting path: " , path )
5885 var cmds []* cobra.Command
5986
6087 if _ , err := os .Stat (path ); os .IsNotExist (err ) {
88+ logrus .Debug ("Path does not exist: " , path )
6189 return cmds , nil
6290 }
6391
@@ -69,15 +97,19 @@ func visitDir(path string) ([]*cobra.Command, error) {
6997 for _ , item := range items {
7098 switch {
7199 case strings .HasPrefix (item .Name (), "." ):
100+ logrus .Debug ("Ignoring hidden path: " , filepath .Join (path , item .Name ()))
72101 continue
73102
74103 case item .IsDir ():
104+ logrus .Debug ("Found directory: " , filepath .Join (path , item .Name ()))
75105 cmd := & cobra.Command {
76106 Use : item .Name (),
77107 }
78108
79- readme , err := ioutil .ReadFile (filepath .Join (path , item .Name (), "README" ))
109+ readmePath := filepath .Join (path , item .Name (), "README" )
110+ readme , err := ioutil .ReadFile (readmePath )
80111 if err == nil {
112+ logrus .Debug ("Found README at: " , readmePath )
81113 cmd .Short = strings .Split (string (readme ), "\n " )[0 ]
82114 cmd .Long = string (readme )
83115 cmd .Args = cobra .NoArgs
@@ -92,17 +124,21 @@ func visitDir(path string) ([]*cobra.Command, error) {
92124 }
93125
94126 if cmd .HasSubCommands () {
127+ logrus .Debug ("Directory has scripts (subcommands) inside it: " , filepath .Join (path , item .Name ()))
95128 cmd .Run = func (cmd * cobra.Command , args []string ) {
96129 cmd .Usage ()
97130 }
98131 }
99132 cmds = append (cmds , cmd )
100133
101134 case item .Mode ()& 0100 != 0 :
135+ logrus .Debug ("Script found: " , filepath .Join (path , item .Name ()))
136+
102137 cmd , err := commandFromScript (filepath .Join (path , item .Name ()))
103138 if err != nil {
104139 return nil , err
105140 }
141+
106142 cmds = append (cmds , cmd )
107143 }
108144 }
@@ -128,6 +164,7 @@ func shortDescriptionFrom(path string) (string, error) {
128164 for scanner .Scan () {
129165 match := r .FindStringSubmatch (scanner .Text ())
130166 if len (match ) == 2 {
167+ logrus .Debug ("Found short description line: " , filepath .Join (path ), ", set to: " , match [1 ])
131168 return match [1 ], nil
132169 }
133170 }
@@ -153,6 +190,7 @@ func exampleFrom(path string) (string, error) {
153190 for scanner .Scan () {
154191 match := r .FindStringSubmatch (scanner .Text ())
155192 if len (match ) == 2 {
193+ logrus .Debug ("Found example line: " , filepath .Join (path ), ", set to: " , match [1 ])
156194 return " sd " + match [1 ], nil
157195 }
158196 }
@@ -179,10 +217,13 @@ func commandFromScript(path string) (*cobra.Command, error) {
179217 },
180218 }
181219
220+ logrus .Debug ("Created command: " , filepath .Base (path ))
221+
182222 return cmd , nil
183223}
184224
185225func sh (cmd string , args []string ) error {
226+ logrus .Debug ("Exec" , cmd , args )
186227 return syscall .Exec (cmd , append ([]string {cmd }, args ... ), os .Environ ())
187228}
188229
@@ -212,5 +253,6 @@ func completions(root *cobra.Command) *cobra.Command {
212253 },
213254 })
214255
256+ logrus .Debug ("Completions (bash/zsh) commands added" )
215257 return c
216258}
0 commit comments