X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=main.go;h=6322157f2ca9564d6523606095ee0967fd2d8daa;hb=33e06a7ef39ce42d9933ca9106a453daa7eb58ac;hp=b10f57e55b92776a18ff5dfa76fb7ed25cc5b39c;hpb=839f07d7b3130efc613d7d3fa8ed71a7d8d5fd7f;p=nsscash%2Fnsscash.git diff --git a/main.go b/main.go index b10f57e..6322157 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,24 @@ func main() { break } - cfg, err := LoadConfig(args[1]) + mainFetch(args[1]) + return + + case "convert": + if len(args) != 4 { + break + } + + mainConvert(args[1], args[2], args[3]) + return + } + + flag.Usage() + os.Exit(1) +} + +func mainFetch(cfgPath string) { + cfg, err := LoadConfig(cfgPath) if err != nil { log.Fatal(err) } @@ -61,24 +78,22 @@ func main() { if err != nil { log.Fatal(err) } - err = WriteStateIfChanged(cfg.StatePath, state) + // NOTE: Make sure to call WriteState() only if there were no + // errors (see WriteState() and README) + err = WriteState(cfg.StatePath, state) if err != nil { log.Fatal(err) } - return - - case "convert": - if len(args) != 4 { - break - } +} +func mainConvert(typ, srcPath, dstPath string) { var t FileType - err := t.UnmarshalText([]byte(args[1])) + err := t.UnmarshalText([]byte(typ)) if err != nil { log.Fatal(err) } - src, err := ioutil.ReadFile(args[2]) + src, err := ioutil.ReadFile(srcPath) if err != nil { log.Fatal(err) } @@ -108,7 +123,7 @@ func main() { } // We must create the file first or deployFile() will abort - f, err := os.Create(args[3]) + f, err := os.Create(dstPath) if err != nil { log.Fatal(err) } @@ -116,16 +131,11 @@ func main() { err = deployFile(&File{ Type: t, - Url: args[2], - Path: args[3], + Url: srcPath, + Path: dstPath, body: x.Bytes(), }) if err != nil { log.Fatal(err) } - return - } - - flag.Usage() - os.Exit(1) }