diff --git a/Makefile b/Makefile index fbf2ef2..1585eed 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,10 @@ build: run: @go run $(SRC_LOCATION) --config-file $(PWD)/config.json --slack-token $(SLACK_TOKEN) +.PHONY: run-version +run: + @go run $(SRC_LOCATION) --version + .PHONY: clean clean: echo "Removing any existing go-binaries" diff --git a/cmd/bashbot/bashbot.go b/cmd/bashbot/bashbot.go index 7b0d017..5949cf5 100644 --- a/cmd/bashbot/bashbot.go +++ b/cmd/bashbot/bashbot.go @@ -28,6 +28,7 @@ var help bool var getVersion bool var configFile string var slackToken string +var installVendorDependenciesFlag bool var sendMessageChannel string var sendMessageText string var sendMessageEphemeral bool @@ -94,6 +95,15 @@ type Parameter struct { Source string `json:"source,omitempty"` } +type Dependencies struct { + Dependencies []Dependency `json:"dependencies"` +} + +type Dependency struct { + Name string `json:"name"` + Install string `json:"install"` +} + type Channel struct { Id string `json:"id"` Created int `json:"created"` @@ -190,6 +200,36 @@ func getAdmin() Admin { return Admins.Admins[0] } +func installVendorDependencies() bool { + log.Debug("installVendorDependencies()") + jsonFile, err := os.Open(configFile) + if err != nil { + log.Error(err) + } + defer jsonFile.Close() + + byteValue, _ := ioutil.ReadAll(jsonFile) + var Dependencies Dependencies + json.Unmarshal(byteValue, &Dependencies) + + for i := 0; i < len(Dependencies.Dependencies); i++ { + log.Info(Dependencies.Dependencies[i].Name) + + words := strings.Fields(Dependencies.Dependencies[i].Install) + var tcmd []string + + for index, element := range words { + log.Debug(strconv.Itoa(index) + ": " + element) + tcmd = append(tcmd, element) + } + cmd := []string{"bash", "-c", "pushd vendor && " + strings.Join(tcmd, " ") + " && popd"} + log.Debug(strings.Join(cmd, " ")) + ret := shellOut(cmd) + log.Info(ret) + } + return true +} + func stringInSlice(a string, list []string) bool { for _, b := range list { if b == a { @@ -622,11 +662,12 @@ to run bash commands or scripts based on a configuration file. func main() { flag.StringVar(&configFile, "config-file", "", "[REQUIRED] Filepath to config.json file") flag.StringVar(&slackToken, "slack-token", "", "[REQUIRED] Slack token used to authenticate with api") + flag.BoolVar(&installVendorDependenciesFlag, "install-vendor-dependencies", false, "Cycle through dependencies array in config file to install extra dependencies") flag.StringVar(&sendMessageChannel, "send-message-channel", "", "Send stand-alone slack message to this channel (requires -send-message-text)") flag.StringVar(&sendMessageText, "send-message-text", "", "Send stand-alone slack message (requires -send-message-channel)") flag.BoolVar(&sendMessageEphemeral, "send-message-ephemeral", false, "Send stand-alone ephemeral slack message to a specific user (requires -send-message-channel -send-message-text and -send-message-user)") flag.StringVar(&sendMessageUser, "send-message-user", "", "Send stand-alone ephemeral slack message to this slack user (requires -send-message-channel -send-message-text and -send-message-ephemeral)") - flag.StringVar(&logLevel, "log-level", "info", "Log level to display (info,debug,warn,error)") + flag.StringVar(&logLevel, "log-level", "info", "Log elevel to display (info,debug,warn,error)") flag.StringVar(&logFormat, "log-format", "text", "Display logs as json or text") flag.BoolVar(&help, "help", false, "Help/usage information") flag.BoolVar(&getVersion, "version", false, "Get current version") @@ -641,6 +682,7 @@ func main() { fmt.Println("bashbot-" + operatingSystem + "-" + systemArchitecture + "\t" + Version) os.Exit(0) } + initLog(logLevel, logFormat) if configFile == "" { usage() @@ -661,6 +703,14 @@ func main() { os.Exit(1) } + if installVendorDependenciesFlag { + if !installVendorDependencies() { + log.Error("Failed to install dependencies") + os.Exit(1) + } + os.Exit(0) + } + admin = getAdmin() api = slack.New(slackToken) diff --git a/entrypoint.sh b/entrypoint.sh index 67d23d8..22c492e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -19,16 +19,19 @@ if [ -z "$SLACK_TOKEN" ]; then echo "SLACK_TOKEN is not set. Please set it and try again." exit 1 fi -# Install vendor dependencies -pushd scripts - ./get-vendor-dependencies.sh $BASHBOT_CONFIG_FILEPATH ../vendor -popd +mkdir -p vendor # If the log-level doesn't exist, set it to 'info' LOG_LEVEL=${LOG_LEVEL:-info} # If the log-format doesn't exist, set it to 'text' LOG_FORMAT=${LOG_FORMAT:-text} +# Run install-vendor-dependencies path +bashbot \ + --config-file "$BASHBOT_CONFIG_FILEPATH" \ + --slack-token "$SLACK_TOKEN" \ + --install-vendor-dependencies + # Run Bashbot binary passing the config file and the Slack token bashbot \ --config-file "$BASHBOT_CONFIG_FILEPATH" \ diff --git a/scripts/get-vendor-dependencies.sh b/scripts/get-vendor-dependencies.sh deleted file mode 100755 index 4adba0a..0000000 --- a/scripts/get-vendor-dependencies.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# shellcheck disable=SC2086 -set -eou pipefail - -config_file="${1:-}" -vendor_dir="${2:-}" - -if [[ -z "$config_file" ]]; then - echo "Must pass config file as first argument" - exit 0 -fi -if ! [[ -f "$config_file" ]]; then - echo "Must pass config file as first argument." - exit 0 -fi -if [[ -z "$vendor_dir" ]]; then - echo "Must pass vendor directory as second argument" - exit 0 -fi -if ! [[ -d "$vendor_dir" ]]; then - echo "Must pass vendor directory as second argument" - exit 0 -fi -cd $vendor_dir && rm -rf * -echo "Installing Dependencies..." -jq -c '.dependencies[]' $config_file -for dep in $(jq -r '.dependencies[] | @base64' $config_file); do - echo "---------------------" - _jq() { - echo ${dep} | base64 -d | jq -r ${1} - } - - this_name=$(_jq '.name') - this_install=$(_jq '.install') - - echo "$this_name" - echo "$this_install" - eval $this_install -done -echo "Dependencies installed."