You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.5 KiB
116 lines
2.5 KiB
package config
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type ConfigEntries struct {
|
|
SlackAuthToken string
|
|
SlackAppToken string
|
|
Actions []ActionScript
|
|
}
|
|
|
|
type ActionScript struct {
|
|
Name string
|
|
DisplayName string
|
|
Path string
|
|
}
|
|
|
|
var Config ConfigEntries
|
|
|
|
// var Actions []ActionScript
|
|
|
|
func init() {
|
|
log.Println("Initializing App Config settings")
|
|
envFile := os.Getenv("SLACKBOT_ENV_FILE")
|
|
if envFile != "" {
|
|
log.Printf("Reading env vars from [%s]", envFile)
|
|
} else {
|
|
envFile = ".env"
|
|
}
|
|
|
|
godotenv.Load(envFile)
|
|
Config.SlackAuthToken = os.Getenv("SLACK_AUTH_TOKEN")
|
|
Config.SlackAppToken = os.Getenv("SLACK_APP_TOKEN")
|
|
actionsLoaded := initalizeDefinedActions()
|
|
if actionsLoaded == 0 {
|
|
log.Println("No actions found in the environment variables")
|
|
os.Exit(1)
|
|
}
|
|
log.Println("Finished loading config. Actions found:", actionsLoaded)
|
|
log.Println("Actions:", Config.Actions)
|
|
}
|
|
|
|
// Each action is defined on an env variable named `BOT_ACTION_1` `BOT_ACTION_2`
|
|
// the index starts with 1 and each must contain three comma separated values
|
|
// `Name,Display Name,Path`
|
|
// For example:
|
|
// `poweron,Encender Server,/some/script.sh`
|
|
//
|
|
// Each action name must be unique
|
|
func initalizeDefinedActions() int {
|
|
log.Println("initalizeDefinedActions")
|
|
var count = 0
|
|
var value string
|
|
var exists bool
|
|
|
|
actionNames := make(map[string]bool)
|
|
for {
|
|
envVarName := fmt.Sprintf("BOT_ACTION_%d", count+1)
|
|
log.Println("Checking for env var:", envVarName)
|
|
value, exists = os.LookupEnv(envVarName)
|
|
if !exists {
|
|
break
|
|
}
|
|
log.Println("Action found:", value)
|
|
action := actionStringToActionScript(value)
|
|
|
|
exists = actionNames[action.Name]
|
|
if exists {
|
|
log.Println("The action", action.Name, "is already defined.")
|
|
os.Exit(1)
|
|
} else {
|
|
actionNames[action.Name] = true
|
|
}
|
|
|
|
_, err := os.Stat(action.Path)
|
|
if err != nil {
|
|
log.Println("The specified path", action.Path, " does not exist.", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Actions = append(Actions, action)
|
|
Config.Actions = append(Config.Actions, action)
|
|
count++
|
|
}
|
|
return count
|
|
}
|
|
|
|
func actionStringToActionScript(actionString string) ActionScript {
|
|
var action ActionScript
|
|
var csvArr []string
|
|
var err error
|
|
r := csv.NewReader(strings.NewReader(actionString))
|
|
for {
|
|
csvArr, err = r.Read()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
// log.Fatal(err)
|
|
} else {
|
|
action.Name = csvArr[0]
|
|
action.DisplayName = csvArr[1]
|
|
action.Path = csvArr[2]
|
|
}
|
|
}
|
|
|
|
return action
|
|
}
|
|
|