Skip to content

Commit

Permalink
Adding yaml support for config and rule files
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcquillan committed Jan 29, 2018
1 parent 2cb4746 commit 3cef39b
Show file tree
Hide file tree
Showing 22 changed files with 9,742 additions and 57 deletions.
17 changes: 14 additions & 3 deletions core/config.go
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/hexbotio/hex/models"
"gopkg.in/yaml.v2"
)

// Config func
Expand Down Expand Up @@ -51,9 +52,19 @@ func Config(config *models.Config, version string) {
if err != nil {
log.Fatal("ERROR: Config File Read - ", err)
}
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal("ERROR: Config File Unmarshal - ", err)
configType := fileType(os.Args[1])
if configType == "json" {
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal("ERROR: Config File json Unmarshal - ", err)
}
} else if configType == "yaml" {
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Fatal("ERROR: Config File yaml Unmarshal - ", err)
}
} else {
log.Fatal("ERROR: Config File Unknown Type")
}
}

Expand Down
55 changes: 46 additions & 9 deletions core/rules.go
Expand Up @@ -10,17 +10,16 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/hexbotio/hex/models"
"gopkg.in/yaml.v2"
)

var fileFilter = ".json"

func Rules(rules *map[string]models.Rule, config models.Config) {
if config.RulesDir != "" {
if DirExists(config.RulesDir) {
go watchRules(config, rules)
ruleList := []string{}
err := filepath.Walk(config.RulesDir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && strings.HasSuffix(f.Name(), fileFilter) {
if !f.IsDir() && isConfig(f.Name()) {
ruleList = append(ruleList, path)
}
return nil
Expand All @@ -40,15 +39,15 @@ func Rules(rules *map[string]models.Rule, config models.Config) {

func addRule(ruleFile string, rules map[string]models.Rule, config models.Config) {
if _, exists := rules[ruleFile]; !exists {
if strings.HasSuffix(ruleFile, fileFilter) {
if isConfig(ruleFile) {
config.Logger.Info("Loading Rule " + ruleFile)
rules[ruleFile] = readRule(ruleFile, config)
}
}
}

func reloadRule(ruleFile string, rules map[string]models.Rule, config models.Config) {
if strings.HasSuffix(ruleFile, fileFilter) {
if isConfig(ruleFile) {
config.Logger.Info("Reloading Rule " + ruleFile)
rules[ruleFile] = readRule(ruleFile, config)
}
Expand Down Expand Up @@ -78,10 +77,22 @@ func readRule(ruleFile string, config models.Config) (rule models.Rule) {
rule.Active = false
return rule
}
err = json.Unmarshal(file, &rule)
if err != nil {
config.Logger.Error("Add Rule Unmarshal " + ruleFile + " - " + err.Error())
rule.Active = false
ruleType := fileType(ruleFile)
if ruleType == "json" {
err = json.Unmarshal(file, &rule)
if err != nil {
config.Logger.Error("Add Rule json Unmarshal " + ruleFile + " - " + err.Error())
rule.Active = false
return rule
}
} else if ruleType == "yaml" {
err = yaml.Unmarshal(file, &rule)
if err != nil {
config.Logger.Error("Add Rule yaml Unmarshal " + ruleFile + " - " + err.Error())
rule.Active = false
return rule
}
} else {
return rule
}
for i := 0; i < len(rule.Actions); i++ {
Expand Down Expand Up @@ -133,3 +144,29 @@ func watchRules(config models.Config, rules *map[string]models.Rule) {
<-done

}

func isConfig(file string) bool {
if strings.HasSuffix(file, ".json") {
return true
}
if strings.HasSuffix(file, ".yaml") {
return true
}
if strings.HasSuffix(file, ".yml") {
return true
}
return false
}

func fileType(file string) string {
if strings.HasSuffix(file, ".json") {
return "json"
}
if strings.HasSuffix(file, ".yaml") {
return "yaml"
}
if strings.HasSuffix(file, ".yml") {
return "yaml"
}
return ""
}
16 changes: 8 additions & 8 deletions models/action.go
Expand Up @@ -2,12 +2,12 @@ package models

// Action Struct
type Action struct {
Type string `json:"type"`
Command string `json:"command"`
HideOutput bool `json:"hide_output"`
OutputToVar bool `json:"output_to_var"`
OutputFailOnly bool `json:"output_fail_only"`
RunOnFail bool `json:"run_on_fail"`
LastConfig bool `json:"last_config"`
Config map[string]string `json:"config"`
Type string `json:"type" yaml:"type"`
Command string `json:"command" yaml:"command"`
HideOutput bool `json:"hide_output" yaml:"hide_output"`
OutputToVar bool `json:"output_to_var" yaml:"output_to_var"`
OutputFailOnly bool `json:"output_fail_only" yaml:"output_fail_only"`
RunOnFail bool `json:"run_on_fail" yaml:"run_on_fail"`
LastConfig bool `json:"last_config" yaml:"last_config"`
Config map[string]string `json:"config" yaml:"config"`
}
42 changes: 21 additions & 21 deletions models/config.go
Expand Up @@ -8,25 +8,25 @@ import (
type Config struct {
Version string
Logger hclog.Logger
Admins string `json:"admins"`
ACL string `json:"acl"`
PluginsDir string `json:"plugins_dir"`
RulesDir string `json:"rules_dir"`
LogFile string `json:"log_file"`
Debug bool `json:"debug"`
Trace bool `json:"trace"`
Quiet bool `json:"quiet"`
BotName string `json:"bot_name"`
CLI bool `json:"cli"`
Auditing bool `json:"auditing"`
AuditingFile string `json:"auditing_file"`
Slack bool `json:"slack"`
SlackToken string `json:"slack_token"`
SlackIcon string `json:"slack_icon"`
SlackDebug bool `json:"slack_debug"`
Scheduler bool `json:"scheduler"`
Webhook bool `json:"webhook"`
WebhookPort int `json:"webhook_port"`
Command string `json:"command"`
Vars map[string]string `json:"vars"`
Admins string `json:"admins" yaml:"admins"`
ACL string `json:"acl" yaml:"acl"`
PluginsDir string `json:"plugins_dir" yaml:"plugins_dir"`
RulesDir string `json:"rules_dir" yaml:"rules_dir"`
LogFile string `json:"log_file" yaml:"log_file"`
Debug bool `json:"debug" yaml:"debug"`
Trace bool `json:"trace" yaml:"trace"`
Quiet bool `json:"quiet" yaml:"quiet"`
BotName string `json:"bot_name" yaml:"bot_name"`
CLI bool `json:"cli" yaml:"cli"`
Auditing bool `json:"auditing" yaml:"auditing"`
AuditingFile string `json:"auditing_file" yaml:"auditing_file"`
Slack bool `json:"slack" yaml:"slack"`
SlackToken string `json:"slack_token" yaml:"slack_token"`
SlackIcon string `json:"slack_icon" yaml:"slack_icon"`
SlackDebug bool `json:"slack_debug" yaml:"slack_debug"`
Scheduler bool `json:"scheduler" yaml:"scheduler"`
Webhook bool `json:"webhook" yaml:"webhook"`
WebhookPort int `json:"webhook_port" yaml:"webhook_port"`
Command string `json:"command" yaml:"command"`
Vars map[string]string `json:"vars" yaml:"vars"`
}
32 changes: 16 additions & 16 deletions models/rules.go
Expand Up @@ -3,20 +3,20 @@ package models
// Rule Struct
type Rule struct {
Id string
Name string `json:"rule"`
Match string `json:"match"`
Schedule string `json:"schedule"`
URL string `json:"url"`
ACL string `json:"acl"`
Channel string `json:"channel"`
Format bool `json:"format"`
Threaded bool `json:"threaded"`
OutputFailOnly bool `json:"output_fail_only"`
OutputOnChange bool `json:"output_on_change"`
GroupOutput bool `json:"group_output"`
Help string `json:"help"`
Hide bool `json:"hide"`
Active bool `json:"active"`
Debug bool `json:"debug"`
Actions []Action `json:"actions"`
Name string `json:"rule" yaml:"rule"`
Match string `json:"match" yaml:"match"`
Schedule string `json:"schedule" yaml:"schedule"`
URL string `json:"url" yaml:"url"`
ACL string `json:"acl" yaml:"acl"`
Channel string `json:"channel" yaml:"channel"`
Format bool `json:"format" yaml:"format"`
Threaded bool `json:"threaded" yaml:"threaded"`
OutputFailOnly bool `json:"output_fail_only" yaml:"output_fail_only"`
OutputOnChange bool `json:"output_on_change" yaml:"output_on_change"`
GroupOutput bool `json:"group_output" yaml:"group_output"`
Help string `json:"help" yaml:"help"`
Hide bool `json:"hide" yaml:"hide"`
Active bool `json:"active" yaml:"active"`
Debug bool `json:"debug" yaml:"debug"`
Actions []Action `json:"actions" yaml:"actions"`
}

0 comments on commit 3cef39b

Please sign in to comment.