1 changed files with 67 additions and 6 deletions
@ -1,24 +1,85 @@ |
|||||
package config |
package config |
||||
|
|
||||
import ( |
import ( |
||||
|
"fmt" |
||||
"os" |
"os" |
||||
|
"encoding/csv" |
||||
|
"strings" |
||||
|
"io" |
||||
"github.com/joho/godotenv" |
"github.com/joho/godotenv" |
||||
) |
) |
||||
|
|
||||
type ConfigEntries struct { |
type ConfigEntries struct { |
||||
SlackAuthToken string |
SlackAuthToken string |
||||
SlackAppToken string |
SlackAppToken string |
||||
ActionScript1 string |
Actions []ActionScript |
||||
ActionScript2 string |
} |
||||
|
|
||||
|
type ActionScript struct { |
||||
|
Name string |
||||
|
DisplayName string |
||||
|
Path string |
||||
} |
} |
||||
|
|
||||
var Config ConfigEntries |
var Config ConfigEntries |
||||
|
// var Actions []ActionScript
|
||||
|
|
||||
func init() { |
func init() { |
||||
// fmt.Println("init de config!")
|
fmt.Println("Initializing App Config settings") |
||||
// Load Env variables from .dot file
|
// TODO: Add if defined app will read from .env
|
||||
godotenv.Load(".env") |
godotenv.Load(".env") |
||||
Config.SlackAuthToken = os.Getenv("SLACK_AUTH_TOKEN") |
Config.SlackAuthToken = os.Getenv("SLACK_AUTH_TOKEN") |
||||
Config.SlackAppToken = os.Getenv("SLACK_APP_TOKEN") |
Config.SlackAppToken = os.Getenv("SLACK_APP_TOKEN") |
||||
|
actionsLoaded := initalizeDefinedActions() |
||||
|
fmt.Println("Finished loading config. Actions found:", actionsLoaded) |
||||
|
fmt.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`
|
||||
|
func initalizeDefinedActions() int { |
||||
|
fmt.Println("initalizeDefinedActions") |
||||
|
var count = 1 |
||||
|
var value string |
||||
|
var exists bool |
||||
|
for { |
||||
|
envVarName := fmt.Sprintf("BOT_ACTION_%d", count) |
||||
|
fmt.Println("Checking for env var:", envVarName) |
||||
|
value, exists = os.LookupEnv(envVarName) |
||||
|
if !exists { |
||||
|
fmt.Println("Not fonud, giving up") |
||||
|
break |
||||
|
} |
||||
|
fmt.Println("Action found:", value) |
||||
|
action := actionStringToActionScript(value) |
||||
|
// 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 |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue