|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/logrusorgru/aurora/v3" |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "log" |
| 8 | + "math/rand" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "regexp" |
| 12 | + "sort" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 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", |
| 28 | + Short: "SHORT", |
| 29 | + Example: "", |
| 30 | +} |
| 31 | + |
| 32 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 33 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 34 | +func 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 { |
| 40 | + fmt.Println(err) |
| 41 | + os.Exit(1) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func init() { |
| 46 | + cobra.OnInitialize() |
| 47 | +} |
| 48 | + |
| 49 | +func addDevScriptTasksAsCommands(rootCmd *cobra.Command) { |
| 50 | + currentDirectory, err := os.Getwd() |
| 51 | + if err != nil { |
| 52 | + log.Fatalf("Failed to execute: '%s'", err.Error()) |
| 53 | + } |
| 54 | + |
| 55 | + steps := 0 |
| 56 | + 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)) |
| 65 | + } |
| 66 | + 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 |
| 71 | + } |
| 72 | + } |
| 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))) |
| 76 | + break |
| 77 | + } |
| 78 | + // Moving up one directory |
| 79 | + currentDirectory = filepath.Dir(currentDirectory) |
| 80 | + steps += 1 |
| 81 | + } |
| 82 | +} |
| 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