chaos/cmd/chaos/chaos.go

87 lines
1.8 KiB
Go
Raw Normal View History

2020-03-02 00:28:04 +00:00
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 <structure> <key>\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)
}