Skip to content

Commit

Permalink
feat: load rules from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
doron-cohen committed Sep 16, 2020
1 parent c78f13f commit ddcfce7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.14
require (
github.com/google/go-cmp v0.2.0
github.com/spf13/cobra v1.0.0
gopkg.in/yaml.v2 v2.3.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,6 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
4 changes: 4 additions & 0 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package action

type Action struct {
}
38 changes: 25 additions & 13 deletions internal/action/rules.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
package action

import (
"io/ioutil"
"log"

"github.com/doron-cohen/antidot/internal/dotfile"
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v2"
)

type Rule struct {
dotfile *dotfile.Dotfile
ignore bool
Name string
Description string
Dotfile *dotfile.Dotfile
Ignore bool
}

var rules []Rule
type RulesConfig struct {
Version int
Rules []Rule
}

var rulesConfig RulesConfig

func init() {
rules = make([]Rule, 1)
rules = append(rules,
Rule{
dotfile: dotfile.NewDotfile(".ssh", true),
ignore: true,
},
)
filename := "rules.yaml"
rulesBytes, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Failed to read rules file %s: #%v", filename, err)
}
err = yaml.Unmarshal(rulesBytes, &rulesConfig)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
log.Printf("Rule %v", rulesConfig)
}

func MatchActions(dotfile *dotfile.Dotfile) {
for _, rule := range rules {
if cmp.Equal(dotfile, rule.dotfile) {
for _, rule := range rulesConfig.Rules {
if cmp.Equal(dotfile, rule.Dotfile) {
log.Printf("Matched rule %s with dotfile %s", rule, dotfile)
if rule.ignore {
if rule.Ignore {
log.Printf("Ignoring dotfile %s", dotfile.Name)
}
break
Expand Down
2 changes: 1 addition & 1 deletion internal/dotfile/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type Dotfile struct {
Name string
IsDir bool
IsDir bool `yaml:"is_dir"`
}

func NewDotfile(name string, isDir bool) *Dotfile {
Expand Down
8 changes: 8 additions & 0 deletions rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 1
rules:
- name: ssh_dir
description: SSH directory
dotfile:
name: .ssh
is_dir: true
ignore: true

0 comments on commit ddcfce7

Please sign in to comment.