]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - main.go
nsscash: split main() into separate functions
[nsscash/nsscash.git] / main.go
1 // Main file for nsscash
2
3 // Copyright (C) 2019  Simon Ruderich
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU Affero General Public License for more details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 package main
19
20 import (
21         "bytes"
22         "flag"
23         "fmt"
24         "io/ioutil"
25         "log"
26         "os"
27 )
28
29 func main() {
30         flag.Usage = func() {
31                 fmt.Fprintf(os.Stderr,
32                         "usage: %[1]s [options] fetch <config>\n"+
33                                 "usage: %[1]s [options] convert <type> <src> <dst>\n"+
34                                 "",
35                         os.Args[0])
36                 flag.PrintDefaults()
37         }
38         flag.Parse()
39
40         args := flag.Args()
41         if len(args) == 0 {
42                 flag.Usage()
43                 os.Exit(1)
44         }
45
46         switch args[0] {
47         case "fetch":
48                 if len(args) != 2 {
49                         break
50                 }
51
52                 mainFetch(args[1])
53                 return
54
55         case "convert":
56                 if len(args) != 4 {
57                         break
58                 }
59
60                 mainConvert(args[1], args[2], args[3])
61                 return
62         }
63
64         flag.Usage()
65         os.Exit(1)
66 }
67
68 func mainFetch(cfgPath string) {
69                 cfg, err := LoadConfig(cfgPath)
70                 if err != nil {
71                         log.Fatal(err)
72                 }
73                 state, err := LoadState(cfg.StatePath)
74                 if err != nil {
75                         log.Fatal(err)
76                 }
77                 err = handleFiles(cfg, state)
78                 if err != nil {
79                         log.Fatal(err)
80                 }
81                 // NOTE: Make sure to call WriteState() only if there were no
82                 // errors (see WriteState() and README)
83                 err = WriteState(cfg.StatePath, state)
84                 if err != nil {
85                         log.Fatal(err)
86                 }
87 }
88
89 func mainConvert(typ, srcPath, dstPath string) {
90                 var t FileType
91                 err := t.UnmarshalText([]byte(typ))
92                 if err != nil {
93                         log.Fatal(err)
94                 }
95
96                 src, err := ioutil.ReadFile(srcPath)
97                 if err != nil {
98                         log.Fatal(err)
99                 }
100                 var x bytes.Buffer
101                 if t == FileTypePlain {
102                         x.Write(src)
103                 } else if t == FileTypePasswd {
104                         pws, err := ParsePasswds(bytes.NewReader(src))
105                         if err != nil {
106                                 log.Fatal(err)
107                         }
108                         err = SerializePasswds(&x, pws)
109                         if err != nil {
110                                 log.Fatal(err)
111                         }
112                 } else if t == FileTypeGroup {
113                         grs, err := ParseGroups(bytes.NewReader(src))
114                         if err != nil {
115                                 log.Fatal(err)
116                         }
117                         err = SerializeGroups(&x, grs)
118                         if err != nil {
119                                 log.Fatal(err)
120                         }
121                 } else {
122                         log.Fatalf("unsupported file type %v", t)
123                 }
124
125                 // We must create the file first or deployFile() will abort
126                 f, err := os.Create(dstPath)
127                 if err != nil {
128                         log.Fatal(err)
129                 }
130                 f.Close()
131
132                 err = deployFile(&File{
133                         Type: t,
134                         Url:  srcPath,
135                         Path: dstPath,
136                         body: x.Bytes(),
137                 })
138                 if err != nil {
139                         log.Fatal(err)
140                 }
141 }