@@ -2,41 +2,35 @@ package cmd
22
33import (
44 "fmt"
5- "github.com/logrusorgru/aurora/v3"
65 "github.com/spf13/cobra"
76 "log"
8- "math/rand "
7+ "main/utils "
98 "os"
109 "path/filepath"
11- "regexp"
12- "sort"
13- "strings"
14- "time"
10+ "strconv"
1511)
1612
17- const DEV_SCRIPT_MARKER = "DEV_SCRIPT_MARKER"
18- const DEV_SCRIPT_NAME = "dev.sh"
19- const MAX_DEPTH = 100
20- const INIT_ARGUMENT = "INIT"
21- const LOGGING_PREFIX = "DSR - " // DevScriptRunner
22- const LOGGING_WSPACE = " "
23- const LOGGING_BAR = "-------------------------------------------------------"
24-
25- var rootCmd = & cobra.Command {
26- Long : "LONG" ,
27- Use : "drydock" ,
13+ var RootCmd = & cobra.Command {
14+ Use : "dev" ,
15+ Long : `
16+ DevScriptRunner is a helper to run task from a dev.sh file
17+ from within a nested folder structure also providing autocompletion
18+ and other nifty feature ;)
19+ ` ,
2820 Short : "SHORT" ,
2921 Example : "" ,
3022}
3123
3224// Execute adds all child commands to the root command and sets flags appropriately.
3325// This is called by main.main(). It only needs to happen once to the rootCmd.
3426func Execute (version , commit string ) {
35- rootCmd .AddCommand (buildInitCommand ())
36- addDevScriptTasksAsCommands (rootCmd )
37- rand .Seed (time .Now ().UnixNano ())
38-
39- if err := rootCmd .Execute (); err != nil {
27+ cobra .EnableCommandSorting = false
28+ RootCmd .AddCommand (buildSectionCommand ("tasks" ))
29+ addDevScriptTasksAsCommands (RootCmd )
30+ RootCmd .AddCommand (buildSectionCommand ("utils" ))
31+ RootCmd .AddCommand (buildInitCommand ())
32+ // RootCmd.AddCommand(buildCompletionCommand())
33+ if err := RootCmd .Execute (); err != nil {
4034 fmt .Println (err )
4135 os .Exit (1 )
4236 }
@@ -54,80 +48,27 @@ func addDevScriptTasksAsCommands(rootCmd *cobra.Command) {
5448
5549 steps := 0
5650 for {
57- devScriptPath := filepath .Join (currentDirectory , DEV_SCRIPT_NAME )
58- if fileExists (devScriptPath ) {
59- fmt .Println (aurora .Magenta ("Found " + currentDirectory + "/" + DEV_SCRIPT_NAME ))
60- if fileContains (devScriptPath , DEV_SCRIPT_MARKER ) {
61- fmt .Println (aurora .Magenta ("with '" + DEV_SCRIPT_MARKER + "'" ))
62- tasks := parseDevScriptTasks (devScriptPath )
63- for _ , task := range tasks {
64- rootCmd .AddCommand (buildTaskCommand (task , devScriptPath ))
51+ devScriptPath := filepath .Join (currentDirectory , utils .DEV_SCRIPT_NAME )
52+ if utils .FileExists (devScriptPath ) {
53+ if utils .FileContains (devScriptPath , utils .DEV_SCRIPT_MARKER ) {
54+ tasks := utils .ParseDevScriptTasks (devScriptPath )
55+ rootCmd .Long = rootCmd .Long + "\n DEV SCRIPT WITH " + strconv .Itoa (len (tasks )) + " TASKS FOUND AT:\n " + devScriptPath
56+ if len (tasks ) > 0 {
57+ for _ , task := range tasks {
58+ rootCmd .AddCommand (buildTaskCommand (task , devScriptPath ))
59+ }
60+ } else {
61+ rootCmd .AddCommand (buildNoTaskCommand ("NO TASKS IN DEV SCRIPT!" ))
6562 }
6663 break
67- } else {
68- fmt .Println (aurora .Yellow (LOGGING_WSPACE + "Marker '" + DEV_SCRIPT_MARKER + "' is missing in " + DEV_SCRIPT_NAME ))
69- fmt .Println (aurora .Yellow (LOGGING_WSPACE + "Moving up, looking for new " + DEV_SCRIPT_NAME ))
70- // Not breaking here as we want to move up
7164 }
7265 }
73- if currentDirectory == "/" || steps >= MAX_DEPTH {
74- fmt .Println (aurora .Yellow (aurora .Bold (LOGGING_PREFIX + "No " + DEV_SCRIPT_NAME + " with " + DEV_SCRIPT_MARKER + " found. Nothing to do here :(" )))
75- fmt .Println (aurora .Magenta (aurora .Bold (LOGGING_BAR )))
66+ if currentDirectory == "/" || steps >= utils .MAX_DEPTH {
67+ rootCmd .AddCommand (buildNoTaskCommand ("NO DEV SCRIPT WITH VALID MARKER FOUND!" ))
7668 break
7769 }
7870 // Moving up one directory
7971 currentDirectory = filepath .Dir (currentDirectory )
8072 steps += 1
8173 }
8274}
83-
84- func fileExists (path string ) bool {
85- info , err := os .Stat (path )
86- if os .IsNotExist (err ) {
87- return false
88- }
89- return ! info .IsDir ()
90- }
91-
92- func fileContains (filePath string , needle string ) bool {
93- b , err := os .ReadFile (filePath )
94- if err != nil {
95- log .Fatalf ("Failed to execute: '%s'" , err .Error ())
96- }
97- markerIsPresent , err := regexp .Match (needle , b )
98- if err != nil {
99- log .Fatalf ("Failed to execute: '%s'" , err .Error ())
100- }
101- return markerIsPresent
102- }
103-
104- type DevScriptTask struct {
105- name string
106- comments string
107- }
108-
109- func parseDevScriptTasks (devScriptPath string ) []DevScriptTask {
110- // https://regex101.com/r/5LVRcP/1 -> Iteration 1 without comments before
111- // https://regex101.com/r/XyB410/1 -> Final Iteration with comments before ;)
112- devScriptBytes , err := os .ReadFile (devScriptPath )
113- if err != nil {
114- log .Fatalf ("Failed to execute: '%s'" , err .Error ())
115- }
116-
117- devScriptString := string (devScriptBytes )
118- compiledRegex := regexp .MustCompile (`(?m)(?P<comments>(?:^#.*(?:\n|\r\n|\r))*)^(?:function )?(?P<name>[a-zA-Z0-9_-]+)\s?(?:\(\))?\s?{` )
119- captureGroupNames := compiledRegex .SubexpNames ()
120- commentsIndex := sort .StringSlice (captureGroupNames ).Search ("comments" )
121- taskIndex := sort .StringSlice (captureGroupNames ).Search ("name" )
122- matches := compiledRegex .FindAllStringSubmatch (devScriptString , - 1 )
123-
124- var results = []DevScriptTask {}
125- for _ , match := range matches {
126- task := match [taskIndex ]
127- comments := match [commentsIndex ]
128- if ! strings .HasPrefix (task , "_" ) {
129- results = append (results , DevScriptTask {name : task , comments : comments })
130- }
131- }
132- return results
133- }
0 commit comments