From e902bc5a4c8d9b6120845f08ff9e2887d0d7fc58 Mon Sep 17 00:00:00 2001 From: Semyon Novikov Date: Sun, 22 Nov 2020 19:20:58 +0500 Subject: [PATCH] Initial commit - Add simple structures to handle configuration - Add example configuration - Add acme events echo handler --- .gitignore | 1 + examples/amitm.toml | 10 ++++++++ go.mod | 8 +++++++ go.sum | 5 ++++ internal/config/v1/config.go | 39 +++++++++++++++++++++++++++++++ internal/config/v1/config_test.go | 14 +++++++++++ 6 files changed, 77 insertions(+) create mode 100644 .gitignore create mode 100644 examples/amitm.toml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/config/v1/config.go create mode 100644 internal/config/v1/config_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7533fc3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +amitm \ No newline at end of file diff --git a/examples/amitm.toml b/examples/amitm.toml new file mode 100644 index 0000000..b3bb888 --- /dev/null +++ b/examples/amitm.toml @@ -0,0 +1,10 @@ +[[rules]] +name = "Golang" +glob = "*.go" +action = "put" + +[[rules.pipeline]] +exec = "go fmt $file" + +[[rules.pipeline]] +exec = "echo 'Done formatting $file'" \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3ccb4d7 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.sr.ht/~nvkv/amitm + +go 1.15 + +require ( + 9fans.net/go v0.0.2 + github.com/pelletier/go-toml v1.8.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..93783a1 --- /dev/null +++ b/go.sum @@ -0,0 +1,5 @@ +9fans.net/go v0.0.2 h1:RYM6lWITV8oADrwLfdzxmt8ucfW6UtP9v1jg4qAbqts= +9fans.net/go v0.0.2/go.mod h1:lfPdxjq9v8pVQXUMBCx5EO5oLXWQFlKRQgs1kEkjoIM= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM= +github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= diff --git a/internal/config/v1/config.go b/internal/config/v1/config.go new file mode 100644 index 0000000..960eb51 --- /dev/null +++ b/internal/config/v1/config.go @@ -0,0 +1,39 @@ +package config + +import ( + "github.com/pelletier/go-toml" + "io/ioutil" +) + +type PipelineStep struct { + Exec string +} + +type Rule struct { + Name string + Action string + Glob string + Pipeline []PipelineStep +} + +type Config struct { + actionmap map[string][]*Rule + Rules []Rule +} + +func ReadConfig(path string) (*Config, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + cfg := &Config{} + err = toml.Unmarshal(data, cfg) + if err != nil { + return nil, err + } + cfg.actionmap = make(map[string][]*Rule) + for _, rule := range cfg.Rules { + cfg.actionmap[rule.Action] = append(cfg.actionmap[rule.Action], &rule) + } + return cfg, nil +} diff --git a/internal/config/v1/config_test.go b/internal/config/v1/config_test.go new file mode 100644 index 0000000..1edb167 --- /dev/null +++ b/internal/config/v1/config_test.go @@ -0,0 +1,14 @@ +package config + +import ( + "fmt" + "testing" +) + +func TestReadConfig(t *testing.T) { + cfg, err := ReadConfig("../../../examples/amitm.toml") + fmt.Printf("cfg: %+v\n", cfg.actionmap) + if err != nil { + t.Errorf("Can't read example.toml file: %s", err) + } +} -- 2.30.1