commit 996abb26818bfc78d42e9ae899b4bf75b954e10d Author: Sarah Jamie Lewis Date: Sun Mar 1 16:28:04 2020 -0800 initial diff --git a/2pset.go b/2pset.go new file mode 100644 index 0000000..fbaafc5 --- /dev/null +++ b/2pset.go @@ -0,0 +1,39 @@ +package chaos + +import "fmt" + +type TPSet struct { + add *GSet + remove *GSet +} + +func NewTPSet() *TPSet { + return &TPSet{add: NewGSet(), remove: NewGSet()} +} + +func (tpset TPSet) Lookup(element string) bool { + return tpset.add.Lookup(element) && !tpset.remove.Lookup(element) +} + +func (tpset *TPSet) Add(element string) { + tpset.add.Add(element) +} + +func (tpset *TPSet) Remove(element string) { + tpset.remove.Add(element) +} + +func (tpset TPSet) Compare(set *TPSet) bool { + return tpset.add.Compare(set.add) && tpset.remove.Compare(set.remove) +} + +func (tpset *TPSet) Merge(set *TPSet) *TPSet { + ret := new(TPSet) + ret.add = tpset.add.Merge(set.add) + ret.remove = tpset.remove.Merge(set.remove) + return ret +} + +func (tpset TPSet) String() string { + return fmt.Sprintf("add: %v, remove: %v", tpset.add, tpset.remove) +} diff --git a/2pset_test.go b/2pset_test.go new file mode 100644 index 0000000..533e78a --- /dev/null +++ b/2pset_test.go @@ -0,0 +1,30 @@ +package chaos + +import "testing" + +func TestNewTPSet(t *testing.T) { + a := NewTPSet() + b := NewTPSet() + + a.Add("1") + a.Add("2") + a.Add("3") + a.Add("4") + b.Merge(a) + + a.Remove("2") + b.Remove("3") + a.Merge(b) + b.Merge(a) + + if a.Compare(b) == false { + t.Fatalf("a and b should be the same: a:%v b:%v", a, b) + } + + if a.Lookup("3") != false || b.Lookup("3") != false { + t.Fatalf("3 should not be in a %v or b %v", a.Lookup("3"), b.Lookup("3")) + } + + t.Logf("a %v", a) + t.Logf("b %v", b) +} diff --git a/cmd/chaos/chaos.go b/cmd/chaos/chaos.go new file mode 100644 index 0000000..838e3e6 --- /dev/null +++ b/cmd/chaos/chaos.go @@ -0,0 +1,86 @@ +package main + +import ( + "encoding/json" + "fmt" + "git.openprivacy.ca/sarah/chaos" + "io/ioutil" + "os" + "path" +) + +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 + } + gset := chaos.NewGSet() + json, _ := json.Marshal(gset) + ioutil.WriteFile(path, json, 0644) + } + } +} + +func cmd_add(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) + if err != nil { + fmt.Printf("Cannot parse \"%v\" as G-Set", args[0]) + return + } + gset.Add(args[1]) + fmt.Printf("Current %v\n", gset) + json, _ := json.Marshal(gset) + ioutil.WriteFile(path, json, 0644) + } else { + fmt.Printf("Chaos structure \"%v\" does not exist\n", args[1]) + return + } +} + +func cmd_lookup(args []string) { + if len(args) == 2 { + 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) + if err != nil { + fmt.Printf("Cannot parse \"%v\" as G-Set", args[0]) + return + } + fmt.Printf("%v", gset.Lookup(args[1])) + } else { + fmt.Printf("Chaos structure \"%v\" does not exist\n", args[0]) + return + } + } else { + fmt.Printf("Usage: chaos lookup \n") + } +} + +func main() { + if len(os.Args) >= 2 { + switch os.Args[1] { + case "init": + 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]) + } + os.Exit(0) + } + os.Exit(1) +} diff --git a/gset.go b/gset.go new file mode 100644 index 0000000..ee077a4 --- /dev/null +++ b/gset.go @@ -0,0 +1,35 @@ +package chaos + +type GSet struct { + Elements map[string]bool `json: "Elements"` +} + +func NewGSet() *GSet { + return &GSet{Elements: make(map[string]bool)} +} + +func (gs *GSet) Add(element string) { + gs.Elements[element] = true +} + +func (gs *GSet) Lookup(element string) bool { + _, exists := gs.Elements[element] + return exists +} + +func (gs *GSet) Compare(gset *GSet) bool { + for k := range gs.Elements { + hasK := gset.Lookup(k) + if !hasK { + return false + } + } + return true +} + +func (gs *GSet) Merge(gset *GSet) *GSet { + for k := range gset.Elements { + gs.Add(k) + } + return gs +} diff --git a/gset_test.go b/gset_test.go new file mode 100644 index 0000000..f7427e5 --- /dev/null +++ b/gset_test.go @@ -0,0 +1,43 @@ +package chaos + +import "testing" + +func TestNewGSet(t *testing.T) { + a := NewGSet() + b := NewGSet() + + a.Add("1") + a.Add("2") + a.Add("3") + a.Add("4") + + a.Add("1") + a.Add("2") + a.Add("3") + a.Add("4") + + b.Add("1") + b.Add("2") + b.Add("3") + b.Add("4") + + a.Add("5") + + if b.Compare(a) == false { + t.Fatalf("b %v should be a subset of a %v", a, b) + } + + t.Logf("a: %v", a) + t.Logf("b: %v", b) + + b.Merge(a) + a.Merge(b) + + if b.Compare(a) == false { + t.Fatalf("b %v should be a subset of a %v", a, b) + } + + t.Logf("a: %v", a) + t.Logf("b: %v", b) + +}