This repository has been archived on 2024-05-11. You can view files and clone it, but cannot push or open issues or pull requests.
Corn-Utility/loaders/json.go
toast c4ddf77a7b
All checks were successful
Build and push container image / build (push) Successful in 41s
Upload the rest of files
2023-10-20 09:08:38 +11:00

62 lines
1.1 KiB
Go

package loaders
import (
"encoding/json"
"fmt"
"os"
)
type Tokens struct {
DeployCommands bool `json:"deployCommands"`
Bot string `json:"bot"`
HookID string `json:"hookId"`
HookToken string `json:"hookToken"`
BotPublicKey string `json:"botPublicKey"`
}
var (
tokens Tokens
invalidToken = "Token not found; Please setup a \"tokens.json\" file in root directory."
)
func LoadJSON(path string, v interface{}) error {
var file, err = os.Open(path)
if err != nil {
fmt.Println(err.Error())
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&v)
if err != nil {
fmt.Println(err.Error())
}
return err
}
func TokenLoader(token string) string {
err := LoadJSON("tokens.json", &tokens)
if err != nil {
return invalidToken
}
switch token {
case "bot":
return tokens.Bot
case "hookId":
return tokens.HookID
case "hookToken":
return tokens.HookToken
case "botPublicKey":
return tokens.BotPublicKey
}
return invalidToken
}
func IsCmdsDeployable() bool {
err := LoadJSON("tokens.json", &tokens)
if err != nil {
return false
}
return tokens.DeployCommands
}