Skip to content

Commit

Permalink
Move get-vendor-dependencies functionality from shell script into bas…
Browse files Browse the repository at this point in the history
…hbot go code (#33)
  • Loading branch information
mathew-fleisch authored Aug 5, 2021
1 parent ee05093 commit 8894a1b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 45 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 51 additions & 1 deletion cmd/bashbot/bashbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -641,6 +682,7 @@ func main() {
fmt.Println("bashbot-" + operatingSystem + "-" + systemArchitecture + "\t" + Version)
os.Exit(0)
}

initLog(logLevel, logFormat)
if configFile == "" {
usage()
Expand All @@ -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)

Expand Down
11 changes: 7 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
40 changes: 0 additions & 40 deletions scripts/get-vendor-dependencies.sh

This file was deleted.

0 comments on commit 8894a1b

Please sign in to comment.