Templates
This commit is contained in:
parent
996abb2681
commit
30cc850845
|
@ -0,0 +1 @@
|
||||||
|
{"Structures":{"add":{"Elements":{}},"remove":{"Elements":{}}},"Commands":{"add":["add"],"remove":["remove"]},"LookupIn":["add"],"LookupNotIn":["remove"]}
|
|
@ -11,33 +11,36 @@ import (
|
||||||
|
|
||||||
func cmd_new(args []string) {
|
func cmd_new(args []string) {
|
||||||
if len(args) == 2 {
|
if len(args) == 2 {
|
||||||
switch args[0] {
|
template_path := path.Join(args[0] + ".cst")
|
||||||
case "gset":
|
if _, err := os.Stat(template_path); err == nil {
|
||||||
path := path.Join(".", ".chaos", args[1])
|
data, _ := ioutil.ReadFile(template_path)
|
||||||
if _, err := os.Stat(path); err == nil {
|
template := chaos.NewTemplate()
|
||||||
fmt.Printf("Chaos structure \"%v\" already exists\n", args[1])
|
err := json.Unmarshal(data, template)
|
||||||
return
|
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()
|
} else {
|
||||||
json, _ := json.Marshal(gset)
|
fmt.Printf("No template found for %v", args[0])
|
||||||
ioutil.WriteFile(path, json, 0644)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmd_add(args []string) {
|
func cmd(cmd string, args []string) {
|
||||||
path := path.Join(".", ".chaos", args[0])
|
path := path.Join(".", ".chaos", args[0])
|
||||||
if _, err := os.Stat(path); err == nil {
|
if _, err := os.Stat(path); err == nil {
|
||||||
data, _ := ioutil.ReadFile(path)
|
data, _ := ioutil.ReadFile(path)
|
||||||
gset := chaos.NewGSet()
|
template := chaos.NewTemplate()
|
||||||
err := json.Unmarshal(data, gset)
|
err := json.Unmarshal(data, template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Cannot parse \"%v\" as G-Set", args[0])
|
fmt.Printf("could not execute \"%v\" on %v", cmd, args[0])
|
||||||
return
|
|
||||||
}
|
}
|
||||||
gset.Add(args[1])
|
template.ExecCmd(cmd, args[1])
|
||||||
fmt.Printf("Current %v\n", gset)
|
json, _ := json.Marshal(template)
|
||||||
json, _ := json.Marshal(gset)
|
|
||||||
ioutil.WriteFile(path, json, 0644)
|
ioutil.WriteFile(path, json, 0644)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[1])
|
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])
|
path := path.Join(".", ".chaos", args[0])
|
||||||
if _, err := os.Stat(path); err == nil {
|
if _, err := os.Stat(path); err == nil {
|
||||||
data, _ := ioutil.ReadFile(path)
|
data, _ := ioutil.ReadFile(path)
|
||||||
gset := chaos.NewGSet()
|
template := chaos.NewTemplate()
|
||||||
err := json.Unmarshal(data, gset)
|
err := json.Unmarshal(data, template)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Cannot parse \"%v\" as G-Set", args[0])
|
fmt.Printf("Cannot parse \"%v\"", args[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("%v", gset.Lookup(args[1]))
|
fmt.Printf("%v", template.Lookup(args[1]))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[0])
|
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[0])
|
||||||
return
|
return
|
||||||
|
@ -73,12 +76,10 @@ func main() {
|
||||||
os.Mkdir("./.chaos", 0755)
|
os.Mkdir("./.chaos", 0755)
|
||||||
case "new":
|
case "new":
|
||||||
cmd_new(os.Args[2:])
|
cmd_new(os.Args[2:])
|
||||||
case "add":
|
|
||||||
cmd_add(os.Args[2:])
|
|
||||||
case "lookup":
|
case "lookup":
|
||||||
cmd_lookup(os.Args[2:])
|
cmd_lookup(os.Args[2:])
|
||||||
default:
|
default:
|
||||||
fmt.Printf("Unknown command %v", os.Args[1])
|
cmd(os.Args[1], os.Args[2:])
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"Structures":{"add":{"Elements":{}}},"Commands":{"add":["add"]},"LookupIn":["add"],"LookupNotIn":[]}
|
|
@ -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
|
||||||
|
}
|
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue