From 4e432d553bfc6ea335cfc8724b71c3bf377bedbe Mon Sep 17 00:00:00 2001 From: blyndusk Date: Wed, 13 Oct 2021 17:26:55 +0200 Subject: [PATCH 1/4] add(bash): #2 - implement bahs script exec logic --- cmd/flamin-go/main.go | 20 +++++++++++++++----- internal/core/tasks.go | 36 +++++++++++++++--------------------- scripts/apt/update.sh | 6 ++++++ 3 files changed, 36 insertions(+), 26 deletions(-) create mode 100755 scripts/apt/update.sh diff --git a/cmd/flamin-go/main.go b/cmd/flamin-go/main.go index eb28770..876b433 100644 --- a/cmd/flamin-go/main.go +++ b/cmd/flamin-go/main.go @@ -1,9 +1,9 @@ -// - package main import ( "fmt" + "os" + "os/exec" "strings" "github.com/blyndusk/flamin-go/internal/core" @@ -12,6 +12,16 @@ import ( "github.com/ttacon/chalk" ) +func execAction(file string) { + pwd, _ := os.Getwd() + + cmd, err := exec.Command("/bin/bash", fmt.Sprintf("%s/scripts/%s.sh", pwd, file)).Output() + fmt.Print(chalk.Green, "Starting...", chalk.Reset) + helpers.ExitOnError("sh", err) + output := string(cmd) + fmt.Print(output) +} + func main() { fmt.Print("\033[H\033[2J") @@ -49,7 +59,7 @@ func main() { action := task.Actions[j] choice := chalk.Green.NewStyle().WithBackground(chalk.ResetColor).WithTextStyle(chalk.Bold).Style(action.Description) - + fmt.Print("\033[H\033[2J") fmt.Print("Task: ", chalk.Red, task.Name, chalk.Reset, "\n") fmt.Print("Action: ", chalk.Yellow, chalk.Bold, action.Name, chalk.Reset, "\n") @@ -63,7 +73,7 @@ func main() { result, err := confirmPrompt.Run() helpers.ExitOnError("confirmPrompt failed:", err) - if (result == "" || result == "Y" || result == "y") { - action.Exec() + if result == "" || result == "Y" || result == "y" { + execAction(action.Exec) } } diff --git a/internal/core/tasks.go b/internal/core/tasks.go index a5b7103..912eee1 100644 --- a/internal/core/tasks.go +++ b/internal/core/tasks.go @@ -1,7 +1,5 @@ package core -import "github.com/sirupsen/logrus" - type Task struct { Name string Description string @@ -12,11 +10,7 @@ type Task struct { type Action struct { Name string Description string - Exec func() -} - -func foo() { - logrus.Info("test") + Exec string } var Tasks = []Task{ @@ -31,12 +25,12 @@ var Tasks = []Task{ { Name: "install", Description: "install all apt packages from cfg.sh", - Exec: foo, + Exec: "apt/update", }, { Name: "update", Description: "update all apt packages from cfg.sh", - Exec: foo, + Exec: "apt/update", }, }, }, @@ -51,12 +45,12 @@ var Tasks = []Task{ { Name: "install", Description: "install all snap packages from cfg.sh", - Exec: foo, + Exec: "apt/update", }, { Name: "refresh", Description: "refresh all snap packages from cfg.sh", - Exec: foo, + Exec: "apt/update", }, }, }, @@ -72,17 +66,17 @@ var Tasks = []Task{ { Name: "oh-my-zsh", Description: "install Oh-My-Zsh famework", - Exec: foo, + Exec: "apt/update", }, { Name: "zsh-users plugins", Description: "install zsh plugins from cfg.sh", - Exec: foo, + Exec: "apt/update", }, { Name: "spaceship-prompt", Description: "install spaceship-prompt for Zsh", - Exec: foo, + Exec: "apt/update", }, }, }, @@ -97,12 +91,12 @@ var Tasks = []Task{ { Name: "sync", Description: "sync all bash dotfiles from cfg.sh", - Exec: foo, + Exec: "apt/update", }, { Name: "rc", Description: "update the .bashrc or .zshrc file", - Exec: foo, + Exec: "apt/update", }, }, }, @@ -118,12 +112,12 @@ var Tasks = []Task{ { Name: "docker", Description: "install Docker and Docker Compose", - Exec: foo, + Exec: "apt/update", }, { Name: "kubernetes", Description: "install Kubectl and Helm", - Exec: foo, + Exec: "apt/update", }, }, }, @@ -139,17 +133,17 @@ var Tasks = []Task{ { Name: "extensions", Description: "install all VSCode extensions from cfg.sh", - Exec: foo, + Exec: "apt/update", }, { Name: "theme", Description: "install VSCode theme from cfg.sh", - Exec: foo, + Exec: "apt/update", }, { Name: "settings", Description: "sync settings to VSCode from cfg.sh", - Exec: foo, + Exec: "apt/update", }, }, }, diff --git a/scripts/apt/update.sh b/scripts/apt/update.sh new file mode 100755 index 0000000..663f170 --- /dev/null +++ b/scripts/apt/update.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +echo " +" +sudo apt upgrade -y +# sudo tree \ No newline at end of file From aef28db0d1f92a0b8a1b7b3948692b1bd6e64a6b Mon Sep 17 00:00:00 2001 From: blyndusk Date: Fri, 15 Oct 2021 10:59:29 +0200 Subject: [PATCH 2/4] rename(scripts): make scripts --- scripts/{ => make}/setup.sh | 0 scripts/{ => make}/start.sh | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename scripts/{ => make}/setup.sh (100%) rename scripts/{ => make}/start.sh (100%) diff --git a/scripts/setup.sh b/scripts/make/setup.sh similarity index 100% rename from scripts/setup.sh rename to scripts/make/setup.sh diff --git a/scripts/start.sh b/scripts/make/start.sh similarity index 100% rename from scripts/start.sh rename to scripts/make/start.sh From c15333f51370632ea21aba0647d84c909734b90b Mon Sep 17 00:00:00 2001 From: blyndusk Date: Fri, 15 Oct 2021 11:00:55 +0200 Subject: [PATCH 3/4] add(bash): #2 - apt install + update --- config/.env.sample | 5 ----- config/config.sh | 17 +++++++++++++++++ scripts/apt/install.sh | 11 +++++++++++ scripts/apt/update.sh | 13 +++++++++---- scripts/colors.sh | 26 ++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 9 deletions(-) delete mode 100644 config/.env.sample create mode 100644 config/config.sh create mode 100755 scripts/apt/install.sh create mode 100644 scripts/colors.sh diff --git a/config/.env.sample b/config/.env.sample deleted file mode 100644 index a31a2d3..0000000 --- a/config/.env.sample +++ /dev/null @@ -1,5 +0,0 @@ -DB_NAME=db -DB_USER=root -DB_PASSWORD=root -DB_HOST=postgres -DB_PORT=5432 \ No newline at end of file diff --git a/config/config.sh b/config/config.sh new file mode 100644 index 0000000..0529799 --- /dev/null +++ b/config/config.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +export __APT=( + "apt-transport-https" + "ca-certificates" + "git" + "htop" + "make" + "nodejs" + "shellcheck" + "software-properties-common" + "tmux" + "tree" + "xclip" + "zsh" + "ppppp" +) diff --git a/scripts/apt/install.sh b/scripts/apt/install.sh new file mode 100755 index 0000000..c06242a --- /dev/null +++ b/scripts/apt/install.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# shellcheck source=/dev/null +source "$PWD"/scripts/colors.sh +source "$PWD"/config/config.sh + +fyellow "[-] starting..." ; sleep 1 + +sudo apt install "${__APT[*]}" -y + +fgreen "[-] done !" ; sleep 1 \ No newline at end of file diff --git a/scripts/apt/update.sh b/scripts/apt/update.sh index 663f170..4d92d71 100755 --- a/scripts/apt/update.sh +++ b/scripts/apt/update.sh @@ -1,6 +1,11 @@ #!/bin/bash -echo " -" -sudo apt upgrade -y -# sudo tree \ No newline at end of file +# shellcheck source=/dev/null +source "$PWD"/scripts/colors.sh + +fyellow "[-] starting..." ; sleep 1 + +sudo apt update -y +sudo apt upgrade -y + +fgreen "[-] done !" ; sleep 1 \ No newline at end of file diff --git a/scripts/colors.sh b/scripts/colors.sh new file mode 100644 index 0000000..26c18eb --- /dev/null +++ b/scripts/colors.sh @@ -0,0 +1,26 @@ +#!/bin/bash +fdefault() { echo -e "\e[39m$1\e[0m"; } +fblack() { echo -e "\e[30m$1\e[0m"; } +fred() { echo -e "\e[31m$1\e[0m"; } +fgreen() { echo -e "\e[32m$1\e[0m"; } +fyellow() { echo -e "\e[33m$1\e[0m"; } +fblue() { echo -e "\e[34m$1\e[0m"; } +fmagenta() { echo -e "\e[35m$1\e[0m"; } +fcyan() { echo -e "\e[36m$1\e[0m"; } + +bdefault() { echo -e "\e[49m$1\e[0m"; } +bblack() { echo -e "\e[40m$1\e[0m"; } +bred() { echo -e "\e[41m$1\e[0m"; } +bgreen() { echo -e "\e[42m$1\e[0m"; } +byellow() { echo -e "\e[43m$1\e[0m"; } +bblue() { echo -e "\e[44m$1\e[0m"; } +bmagenta() { echo -e "\e[45m$1\e[0m"; } +bcyan() { echo -e "\e[46m$1\e[0m"; } + +red() { echo -e "\e[1;30;41m$1\e[0m"; } +green() { echo -e "\e[1;30;42m$1\e[0m"; } +yellow() { echo -e "\e[1;30;43m$1\e[0m"; } +blue() { echo -e "\e[1;30;44m$1\e[0m"; } +magenta() { echo -e "\e[1;30;45m$1\e[0m"; } +cyan() { echo -e "\e[1;30;46m$1\e[0m"; } +neg() { echo -e "\e[7$1\e[0m"; } From 311c1ea036fa839c398e30872f182a18ebcbcba3 Mon Sep 17 00:00:00 2001 From: blyndusk Date: Fri, 15 Oct 2021 11:01:21 +0200 Subject: [PATCH 4/4] add(prompt): #2 - recursive prompt --- Makefile | 2 +- cmd/flamin-go/main.go | 23 ++++++++++++++++------- internal/core/tasks.go | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 9f08cb8..8a128e5 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ help: @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' setup-env: ## Copy sample files - bash scripts/setup.sh + bash scripts/make/setup.sh run: ## Up the docker-compose without cache or orphans go run cmd/flamin-go/main.go diff --git a/cmd/flamin-go/main.go b/cmd/flamin-go/main.go index 876b433..ca55901 100644 --- a/cmd/flamin-go/main.go +++ b/cmd/flamin-go/main.go @@ -1,7 +1,9 @@ package main import ( + "bytes" "fmt" + "io" "os" "os/exec" "strings" @@ -14,15 +16,18 @@ import ( func execAction(file string) { pwd, _ := os.Getwd() + actionFile := fmt.Sprintf("%s/scripts/%s.sh", pwd, file) + cmd := exec.Command("/bin/bash", actionFile) - cmd, err := exec.Command("/bin/bash", fmt.Sprintf("%s/scripts/%s.sh", pwd, file)).Output() - fmt.Print(chalk.Green, "Starting...", chalk.Reset) - helpers.ExitOnError("sh", err) - output := string(cmd) - fmt.Print(output) -} + var stdoutBuf, stderrBuf bytes.Buffer + cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf) + cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf) -func main() { + err := cmd.Run() + helpers.ExitOnError("cmd.Run() failed with", err) + +} +func run() { fmt.Print("\033[H\033[2J") searcher := func(input string, index int) bool { @@ -75,5 +80,9 @@ func main() { helpers.ExitOnError("confirmPrompt failed:", err) if result == "" || result == "Y" || result == "y" { execAction(action.Exec) + run() } } +func main() { + run() +} diff --git a/internal/core/tasks.go b/internal/core/tasks.go index 912eee1..0ceebdf 100644 --- a/internal/core/tasks.go +++ b/internal/core/tasks.go @@ -25,7 +25,7 @@ var Tasks = []Task{ { Name: "install", Description: "install all apt packages from cfg.sh", - Exec: "apt/update", + Exec: "apt/install", }, { Name: "update",