From 01d938ad14224aa2c84f8814ab955bb99f6f5b34 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sat, 8 Jun 2019 12:51:32 +0200 Subject: [PATCH] nsscash: add "convert" sub-command Permits manually converting a file without having to fetch it from a webserver. --- main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/main.go b/main.go index 0545117..cef6c61 100644 --- a/main.go +++ b/main.go @@ -18,8 +18,10 @@ package main import ( + "bytes" "flag" "fmt" + "io/ioutil" "log" "os" ) @@ -28,6 +30,7 @@ func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "usage: %[1]s [options] fetch \n"+ + "usage: %[1]s [options] convert \n"+ "", os.Args[0]) flag.PrintDefaults() @@ -63,6 +66,49 @@ func main() { log.Fatal(err) } return + + case "convert": + if len(args) != 4 { + break + } + + var t FileType + err := t.UnmarshalText([]byte(args[1])) + if err != nil { + log.Fatal(err) + } + + src, err := ioutil.ReadFile(args[2]) + if err != nil { + log.Fatal(err) + } + pws, err := ParsePasswds(bytes.NewReader(src)) + if err != nil { + log.Fatal(err) + } + var x bytes.Buffer + err = SerializePasswds(&x, pws) + if err != nil { + log.Fatal(err) + } + + // We must create the file first or deployFile() will abort + f, err := os.Create(args[3]) + if err != nil { + log.Fatal(err) + } + f.Close() + + err = deployFile(&File{ + Type: t, + Url: args[2], + Path: args[3], + body: x.Bytes(), + }) + if err != nil { + log.Fatal(err) + } + return } flag.Usage() -- 2.45.2