]> ruderich.org/simon Gitweb - nsscash/nsscash.git/commitdiff
nsscash: add "convert" sub-command
authorSimon Ruderich <simon@ruderich.org>
Sat, 8 Jun 2019 10:51:32 +0000 (12:51 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sat, 8 Jun 2019 10:51:32 +0000 (12:51 +0200)
Permits manually converting a file without having to fetch it from a
webserver.

main.go

diff --git a/main.go b/main.go
index 05451172164f3783ca6a2469c1b6952b2997e20b..cef6c61d41bfe9dfef0c8995ab25cf8b4762d7c9 100644 (file)
--- a/main.go
+++ b/main.go
 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 <config>\n"+
+                               "usage: %[1]s [options] convert <type> <src> <dst>\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()