]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - main.go
nsscash: return error instead of calling log.Fatal()
[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                 err := mainFetch(args[1])
53                 if err != nil {
54                         log.Fatal(err)
55                 }
56                 return
57
58         case "convert":
59                 if len(args) != 4 {
60                         break
61                 }
62
63                 err := mainConvert(args[1], args[2], args[3])
64                 if err != nil {
65                         log.Fatal(err)
66                 }
67                 return
68         }
69
70         flag.Usage()
71         os.Exit(1)
72 }
73
74 func mainFetch(cfgPath string) error {
75                 cfg, err := LoadConfig(cfgPath)
76                 if err != nil {
77                         return err
78                 }
79                 state, err := LoadState(cfg.StatePath)
80                 if err != nil {
81                         return err
82                 }
83                 err = handleFiles(cfg, state)
84                 if err != nil {
85                         return err
86                 }
87                 // NOTE: Make sure to call WriteState() only if there were no
88                 // errors (see WriteState() and README)
89                 err = WriteState(cfg.StatePath, state)
90                 if err != nil {
91                         return err
92                 }
93                 return nil
94 }
95
96 func mainConvert(typ, srcPath, dstPath string) error {
97                 var t FileType
98                 err := t.UnmarshalText([]byte(typ))
99                 if err != nil {
100                         return err
101                 }
102
103                 src, err := ioutil.ReadFile(srcPath)
104                 if err != nil {
105                         return err
106                 }
107                 var x bytes.Buffer
108                 if t == FileTypePlain {
109                         x.Write(src)
110                 } else if t == FileTypePasswd {
111                         pws, err := ParsePasswds(bytes.NewReader(src))
112                         if err != nil {
113                                 return err
114                         }
115                         err = SerializePasswds(&x, pws)
116                         if err != nil {
117                                 return err
118                         }
119                 } else if t == FileTypeGroup {
120                         grs, err := ParseGroups(bytes.NewReader(src))
121                         if err != nil {
122                                 return err
123                         }
124                         err = SerializeGroups(&x, grs)
125                         if err != nil {
126                                 return err
127                         }
128                 } else {
129                         return fmt.Errorf("unsupported file type %v", t)
130                 }
131
132                 // We must create the file first or deployFile() will abort
133                 f, err := os.Create(dstPath)
134                 if err != nil {
135                         return err
136                 }
137                 f.Close()
138
139                 err = deployFile(&File{
140                         Type: t,
141                         Url:  srcPath,
142                         Path: dstPath,
143                         body: x.Bytes(),
144                 })
145                 if err != nil {
146                         return err
147                 }
148                 return nil
149 }