diff --git a/dev.sh b/dev.sh old mode 100644 new mode 100755 index 08f1e88..6f8d519 --- a/dev.sh +++ b/dev.sh @@ -15,7 +15,6 @@ function test() { echo "#### TEST ####" _log_success "SUCCESS" _log_warning "WARNING" - cat dev.sh } function setup() { diff --git a/dev_setup.sh b/dev_setup.sh index 93f9305..adf014d 100755 --- a/dev_setup.sh +++ b/dev_setup.sh @@ -1,10 +1,8 @@ -3#!/bin/bash +#!/bin/bash # Setup Script to provide a great first run experience ;) set -e brew install go -# se our homebrew tap to always get the latest updates. brew install goreleaser/tap/goreleaser - diff --git a/main.go b/main.go index f367a43..05cfc9f 100644 --- a/main.go +++ b/main.go @@ -3,18 +3,23 @@ package main import ( "embed" + "fmt" "github.com/logrusorgru/aurora/v3" "log" "os" "os/exec" "path/filepath" "regexp" + "strings" ) const DEV_SCRIPT_MARKER = "DEV_SCRIPT_MARKER" const DEV_SCRIPT_NAME = "dev.sh" const MAX_DEPTH = 100 const INIT_ARGUMENT = "INIT" +const LOGGING_PREFIX = "DSR - " +const LOGGING_WSPACE = " " +const LOGGING_BAR = "-------------------------------------------------------" // Assets represents the embedded files. // @@ -22,18 +27,29 @@ const INIT_ARGUMENT = "INIT" var Assets embed.FS func main() { - // TODO: remove later - // os.Chdir("/Users/florian/src/solarwatt-home-app-flutter/app/lib/features/counter_example") - os.Chdir("/Users/florian/src/go-dev-script-runner/test") - if os.Args[1] == INIT_ARGUMENT { - runInit() + // `os.Args[0]` will always be the path of the script + // `os.Args[1]` will either be INIT or a task of the dev.sh script + if len(os.Args) > 1 { + if os.Args[1] == INIT_ARGUMENT { + // If we called `dev INIT` + fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_BAR))) + fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_PREFIX + "Running 'dev INIT' ..."))) + runInit() + } else { + // If we called `dev ` + fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_BAR))) + fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_PREFIX + "Running 'dev " + strings.Join(os.Args[1:], " ") + "' ..."))) + runTask() + } } else { - runTask() + fmt.Println(aurora.Bold("USAGE:")) + fmt.Println(" ", aurora.Bold("dev "), "- to run a task of your dev.sh") + fmt.Println(" ", aurora.Bold("dev INIT"), "- to create a `dev.sh` in the current folder") + os.Exit(0) } } func runInit() { - log.Println(aurora.Green("RUNNING INIT")) currentDirectory, err := os.Getwd() if err != nil { log.Fatalf("Failed to execute: '%s'", err.Error()) @@ -47,12 +63,13 @@ func runInit() { if !fileExists(devSetupShTargetPath) { copyAssetToPath("templates/dev_setup.sh", devSetupShTargetPath) } else { - log.Println("dev_setup.sh is already present.") + fmt.Println(aurora.Yellow(LOGGING_PREFIX + "dev_setup.sh is already present!")) } } else { - log.Println("dev.sh is already present. Skipping INIT") + fmt.Println(aurora.Yellow(LOGGING_PREFIX + "dev.sh is already present.")) + fmt.Println(aurora.Yellow(aurora.Bold(LOGGING_PREFIX + "Skipping INIT!"))) } - + fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_BAR))) os.Exit(0) } @@ -66,17 +83,22 @@ func runTask() { for { devScriptPath := filepath.Join(currentDirectory, DEV_SCRIPT_NAME) if fileExists(devScriptPath) { - log.Println("Found " + DEV_SCRIPT_NAME + " in path " + currentDirectory) + fmt.Println(aurora.Magenta(LOGGING_PREFIX + "Found " + DEV_SCRIPT_NAME + " in ")) + fmt.Println(aurora.Magenta(LOGGING_WSPACE + currentDirectory)) if fileContains(devScriptPath, DEV_SCRIPT_MARKER) { - log.Println(DEV_SCRIPT_NAME + " contains marker " + DEV_SCRIPT_MARKER) + fmt.Println(aurora.Magenta(LOGGING_PREFIX + "Found marker in ")) + fmt.Println(aurora.Magenta(LOGGING_WSPACE + currentDirectory + "/" + DEV_SCRIPT_NAME)) execDevScriptWithArguments(devScriptPath, os.Args[1:]) break } else { - log.Println("Marker is missing in " + DEV_SCRIPT_NAME) + fmt.Println(aurora.Yellow(LOGGING_PREFIX + "Marker '" + DEV_SCRIPT_MARKER + "' is missing in ")) + fmt.Println(aurora.Yellow(LOGGING_WSPACE + currentDirectory + "/" + DEV_SCRIPT_NAME)) + fmt.Println(aurora.Yellow(aurora.Bold(LOGGING_PREFIX + "Nothing to do here :("))) + break } } else { + // Moving up one directory currentDirectory = filepath.Dir(currentDirectory) - log.Println("MOVING UP:", currentDirectory) steps += 1 } if currentDirectory == "/" || steps >= MAX_DEPTH { @@ -114,21 +136,28 @@ func copyAssetToPath(embedPath string, targetPath string) { if err != nil { log.Fatalf("Failed to execute: '%s'", err.Error()) } - log.Println(targetPath + " was created.") + fmt.Println(aurora.Magenta(LOGGING_PREFIX + targetPath + " was created.")) } func execDevScriptWithArguments(devScriptPath string, arguments []string) { + fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_PREFIX + "Executing dev script :)"))) + fmt.Println(aurora.Magenta(aurora.Bold(LOGGING_BAR))) err := os.Chdir(filepath.Dir(devScriptPath)) if err != nil { log.Fatalf("Failed to execute: '%s'", err.Error()) } + err = os.Chmod(devScriptPath, 0755) + if err != nil { + log.Fatalf("Failed to execute: '%s'", err.Error()) + } + cmd := exec.Command(devScriptPath, arguments...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr // Run wil wait for the process to finish err = cmd.Run() if err != nil { - log.Fatalf("Failed to execute: '%s'", err.Error()) + log.Fatalf("Failed to run shell script: '%s'", err.Error()) } }