diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-08-02 08:05:07 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-09-26 21:35:40 +0200 |
commit | 4e4bcb8101848643c49d12b81713b6355e30bf18 (patch) | |
tree | 0700b2a85fc9a2108d6d810100d4b719be2d6f92 | |
parent | da9e27176fd0934d7837077c784ebb1b5e8c98f8 (diff) | |
download | petrific-4e4bcb8101848643c49d12b81713b6355e30bf18.tar.gz petrific-4e4bcb8101848643c49d12b81713b6355e30bf18.tar.bz2 petrific-4e4bcb8101848643c49d12b81713b6355e30bf18.zip |
Add main.go
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | main.go | 107 |
2 files changed, 108 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60ef061 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +petrific @@ -0,0 +1,107 @@ +package main + +import ( + "code.laria.me/petrific/config" + "code.laria.me/petrific/storage" + "flag" + "fmt" + "os" +) + +type subcmd func(args []string) int + +var subcmds = map[string]subcmd{ + "write-dir": notImplementedYet, + "restore-dir": notImplementedYet, + "take-snapshot": notImplementedYet, + "create-snapshot": notImplementedYet, + "list-snapshots": notImplementedYet, +} + +// Global flags +var ( + flagConfPath = flag.String("config", "", "Use this config file instead of the default") + flagStorage = flag.String("storage", "", "Operate on this storage instead of the default one") +) + +var conf config.Config +var objectstore storage.Storage + +func main() { + os.Exit(Main()) +} + +func Main() int { + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: %s [global flags] command\n\nAvailable commands:\n", os.Args[0]) + for cmd := range subcmds { + fmt.Fprintf(os.Stderr, " %s\n", cmd) + } + fmt.Fprintln(os.Stderr, "\nGlobal flags:") + flag.PrintDefaults() + } + flag.Parse() + if !loadConfig() { + return 1 + } + + remaining := flag.Args() + + var cmd subcmd + if len(remaining) > 0 { + cmd = subcmds[remaining[0]] + } + + if cmd == nil { + flag.Usage() + return 1 + } + + return cmd(remaining[1:]) +} + +func loadConfig() bool { + var err error + conf, err = config.LoadConfig(*flagConfPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed reading config: %s\n", err) + return false + } + + storageName := *flagStorage + if storageName == "" { + storageName = conf.DefaultStorage + } + + storageOptions, ok := conf.Storage[storageName] + if !ok { + fmt.Fprintf(os.Stderr, "Storage %s not found\n", storageName) + return false + } + + var method string + if err := storageOptions.Get("method", &method); err != nil { + fmt.Fprintf(os.Stderr, "Failed setting up storage %s: %s\n", storageName, err) + return false + } + + st, ok := storage.StorageTypes[method] + if !ok { + fmt.Fprintf(os.Stderr, "Failed setting up storage %s: Method %s unknown", storageName, method) + return false + } + + s, err := st(conf, storageName) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed setting up storage %s: %s\n", storageName, err) + return false + } + + objectstore = s + return true +} + +func notImplementedYet(_ []string) int { + fmt.Fprintln(os.Stderr, "Not implemented yet") + return 1 +} |