Templates

This commit is contained in:
Sarah Jamie Lewis 2020-03-01 17:36:12 -08:00
parent 996abb2681
commit 30cc850845
5 changed files with 98 additions and 24 deletions

1
2pset.cst Normal file
View File

@ -0,0 +1 @@
{"Structures":{"add":{"Elements":{}},"remove":{"Elements":{}}},"Commands":{"add":["add"],"remove":["remove"]},"LookupIn":["add"],"LookupNotIn":["remove"]}

View File

@ -11,33 +11,36 @@ import (
func cmd_new(args []string) {
if len(args) == 2 {
switch args[0] {
case "gset":
path := path.Join(".", ".chaos", args[1])
if _, err := os.Stat(path); err == nil {
fmt.Printf("Chaos structure \"%v\" already exists\n", args[1])
return
template_path := path.Join(args[0] + ".cst")
if _, err := os.Stat(template_path); err == nil {
data, _ := ioutil.ReadFile(template_path)
template := chaos.NewTemplate()
err := json.Unmarshal(data, template)
if err == nil {
odata, _ := json.Marshal(template)
tpath := path.Join(".", ".chaos", args[1])
ioutil.WriteFile(tpath, odata, 0644)
} else {
fmt.Printf("Could not parse template for %v", args[0])
}
gset := chaos.NewGSet()
json, _ := json.Marshal(gset)
ioutil.WriteFile(path, json, 0644)
} else {
fmt.Printf("No template found for %v", args[0])
}
}
}
func cmd_add(args []string) {
func cmd(cmd string, args []string) {
path := path.Join(".", ".chaos", args[0])
if _, err := os.Stat(path); err == nil {
data, _ := ioutil.ReadFile(path)
gset := chaos.NewGSet()
err := json.Unmarshal(data, gset)
template := chaos.NewTemplate()
err := json.Unmarshal(data, template)
if err != nil {
fmt.Printf("Cannot parse \"%v\" as G-Set", args[0])
return
fmt.Printf("could not execute \"%v\" on %v", cmd, args[0])
}
gset.Add(args[1])
fmt.Printf("Current %v\n", gset)
json, _ := json.Marshal(gset)
template.ExecCmd(cmd, args[1])
json, _ := json.Marshal(template)
ioutil.WriteFile(path, json, 0644)
} else {
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[1])
@ -50,13 +53,13 @@ func cmd_lookup(args []string) {
path := path.Join(".", ".chaos", args[0])
if _, err := os.Stat(path); err == nil {
data, _ := ioutil.ReadFile(path)
gset := chaos.NewGSet()
err := json.Unmarshal(data, gset)
template := chaos.NewTemplate()
err := json.Unmarshal(data, template)
if err != nil {
fmt.Printf("Cannot parse \"%v\" as G-Set", args[0])
fmt.Printf("Cannot parse \"%v\"", args[0])
return
}
fmt.Printf("%v", gset.Lookup(args[1]))
fmt.Printf("%v", template.Lookup(args[1]))
} else {
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[0])
return
@ -73,12 +76,10 @@ func main() {
os.Mkdir("./.chaos", 0755)
case "new":
cmd_new(os.Args[2:])
case "add":
cmd_add(os.Args[2:])
case "lookup":
cmd_lookup(os.Args[2:])
default:
fmt.Printf("Unknown command %v", os.Args[1])
cmd(os.Args[1], os.Args[2:])
}
os.Exit(0)
}

1
gset.cst Normal file
View File

@ -0,0 +1 @@
{"Structures":{"add":{"Elements":{}}},"Commands":{"add":["add"]},"LookupIn":["add"],"LookupNotIn":[]}

44
template.go Normal file
View File

@ -0,0 +1,44 @@
package chaos
type Template struct {
Structures map[string]*GSet
// eg. 2P-Set "add": "add", "remove":"remove"
Commands map[string][]string
LookupIn []string
LookupNotIn []string
}
func NewTemplate() *Template {
return &Template{Structures: make(map[string]*GSet), Commands: make(map[string][]string)}
}
func (t *Template) InitStructures(structures ...string) {
for _, structure := range structures {
t.Structures[structure] = NewGSet()
}
}
func (t *Template) NewCommand(cmd string, structures ...string) {
t.Commands[cmd] = structures
}
func (t *Template) ExecCmd(cmd string, element string) {
for _, structure := range t.Commands[cmd] {
t.Structures[structure].Add(element)
}
}
func (t *Template) Lookup(element string) bool {
for _, structure := range t.LookupIn {
if t.Structures[structure].Lookup(element) == false {
return false
}
}
for _, structure := range t.LookupNotIn {
if t.Structures[structure].Lookup(element) == true {
return false
}
}
return true
}

27
template_test.go Normal file
View File

@ -0,0 +1,27 @@
package chaos
import (
"encoding/json"
"testing"
)
func TestNewTemplate(t *testing.T) {
template := NewTemplate()
template.InitStructures("add", "remove")
template.NewCommand("add", "add")
template.NewCommand("remove", "remove")
template.LookupIn = []string{"add"}
template.LookupNotIn = []string{"remove"}
data, _ := json.Marshal(template)
t.Logf("Template: %s\n", data)
template.ExecCmd("add", "1")
if template.Lookup("1") == false {
t.Fatalf("Looking up 1 should have succeeded %v", template)
}
template.ExecCmd("remove", "1")
if template.Lookup("1") {
t.Fatalf("Looking up 1 should have failed")
}
}