Skip to content

Commit bad0c13

Browse files
author
Florian Heinze
committed
FEATURE: continue searching upward if marker is missing
also improve docs and promts
1 parent f8c8f4c commit bad0c13

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

main.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const DEV_SCRIPT_MARKER = "DEV_SCRIPT_MARKER"
1717
const DEV_SCRIPT_NAME = "dev.sh"
1818
const MAX_DEPTH = 100
1919
const INIT_ARGUMENT = "INIT"
20-
const LOGGING_PREFIX = "DSR - "
20+
const LOGGING_PREFIX = "DSR - " // DevScriptRunner
2121
const LOGGING_WSPACE = " "
2222
const LOGGING_BAR = "-------------------------------------------------------"
2323

@@ -42,9 +42,11 @@ func main() {
4242
runTask()
4343
}
4444
} else {
45+
// Show usage
4546
fmt.Println(aurora.Bold("USAGE:"))
4647
fmt.Println(" ", aurora.Bold("dev <TASK_NAME>"), "- to run a task of your dev.sh")
4748
fmt.Println(" ", aurora.Bold("dev INIT"), "- to create a `dev.sh` in the current folder")
49+
// Explicitly show that we exit here
4850
os.Exit(0)
4951
}
5052
}
@@ -59,8 +61,11 @@ func runInit() {
5961
devSetupShTargetPath := filepath.Join(currentDirectory, "dev_setup.sh")
6062

6163
if !fileExists(devShTargetPath) {
64+
// we can access embedded assets by using the path use din the annotation
6265
copyAssetToPath("templates/dev.sh", devShTargetPath)
6366
if !fileExists(devSetupShTargetPath) {
67+
// We do not want to add dev_setup.sh if INIT was already run.
68+
// The file might have been deleted on purpose.
6469
copyAssetToPath("templates/dev_setup.sh", devSetupShTargetPath)
6570
} else {
6671
fmt.Println(aurora.Yellow(LOGGING_PREFIX + "dev_setup.sh is already present!"))
@@ -93,17 +98,19 @@ func runTask() {
9398
} else {
9499
fmt.Println(aurora.Yellow(LOGGING_PREFIX + "Marker '" + DEV_SCRIPT_MARKER + "' is missing in "))
95100
fmt.Println(aurora.Yellow(LOGGING_WSPACE + currentDirectory + "/" + DEV_SCRIPT_NAME))
96-
fmt.Println(aurora.Yellow(aurora.Bold(LOGGING_PREFIX + "Nothing to do here :(")))
97-
break
101+
fmt.Println(aurora.Yellow(LOGGING_WSPACE + "Moving up, looking for " + DEV_SCRIPT_NAME))
102+
// Not breaking here as we want to move up
98103
}
99-
} else {
100-
// Moving up one directory
101-
currentDirectory = filepath.Dir(currentDirectory)
102-
steps += 1
103104
}
104105
if currentDirectory == "/" || steps >= MAX_DEPTH {
106+
fmt.Println(aurora.Yellow(aurora.Bold(LOGGING_PREFIX + "No " + DEV_SCRIPT_NAME + " with " + DEV_SCRIPT_MARKER + " found")))
107+
fmt.Println(aurora.Yellow(aurora.Bold(LOGGING_WSPACE + "Nothing to do here :(")))
108+
fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_BAR)))
105109
break
106110
}
111+
// Moving up one directory
112+
currentDirectory = filepath.Dir(currentDirectory)
113+
steps += 1
107114
}
108115
}
109116

@@ -120,18 +127,19 @@ func fileContains(filePath string, needle string) bool {
120127
if err != nil {
121128
log.Fatalf("Failed to execute: '%s'", err.Error())
122129
}
123-
isContained, err := regexp.Match(needle, b)
130+
markerIsPresent, err := regexp.Match(needle, b)
124131
if err != nil {
125132
log.Fatalf("Failed to execute: '%s'", err.Error())
126133
}
127-
return isContained
134+
return markerIsPresent
128135
}
129136

130137
func copyAssetToPath(embedPath string, targetPath string) {
131138
data, err := Assets.ReadFile(embedPath)
132139
if err != nil {
133140
log.Fatalf("Failed to execute: '%s'", err.Error())
134141
}
142+
// Shell scripts need to be executable -> 0755
135143
err = os.WriteFile(targetPath, []byte(data), 0755)
136144
if err != nil {
137145
log.Fatalf("Failed to execute: '%s'", err.Error())
@@ -146,7 +154,7 @@ func execDevScriptWithArguments(devScriptPath string, arguments []string) {
146154
if err != nil {
147155
log.Fatalf("Failed to execute: '%s'", err.Error())
148156
}
149-
157+
// In case the script is not executable
150158
err = os.Chmod(devScriptPath, 0755)
151159
if err != nil {
152160
log.Fatalf("Failed to execute: '%s'", err.Error())
@@ -155,9 +163,10 @@ func execDevScriptWithArguments(devScriptPath string, arguments []string) {
155163
cmd := exec.Command(devScriptPath, arguments...)
156164
cmd.Stdout = os.Stdout
157165
cmd.Stderr = os.Stderr
158-
// Run wil wait for the process to finish
166+
// Run will wait for the process to finish
159167
err = cmd.Run()
160168
if err != nil {
161169
log.Fatalf("Failed to run shell script: '%s'", err.Error())
162170
}
171+
// IMPORTANT: no exit here as we do not want to kill the spanned child process!
163172
}

0 commit comments

Comments
 (0)