1 // Main file for nsscash
3 // Copyright (C) 2019 Simon Ruderich
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.
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.
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/>.
31 fmt.Fprintf(os.Stderr,
32 "usage: %[1]s [options] fetch <config>\n"+
33 "usage: %[1]s [options] convert <type> <src> <dst>\n"+
60 mainConvert(args[1], args[2], args[3])
68 func mainFetch(cfgPath string) {
69 cfg, err := LoadConfig(cfgPath)
73 state, err := LoadState(cfg.StatePath)
77 err = handleFiles(cfg, state)
81 // NOTE: Make sure to call WriteState() only if there were no
82 // errors (see WriteState() and README)
83 err = WriteState(cfg.StatePath, state)
89 func mainConvert(typ, srcPath, dstPath string) {
91 err := t.UnmarshalText([]byte(typ))
96 src, err := ioutil.ReadFile(srcPath)
101 if t == FileTypePlain {
103 } else if t == FileTypePasswd {
104 pws, err := ParsePasswds(bytes.NewReader(src))
108 err = SerializePasswds(&x, pws)
112 } else if t == FileTypeGroup {
113 grs, err := ParseGroups(bytes.NewReader(src))
117 err = SerializeGroups(&x, grs)
122 log.Fatalf("unsupported file type %v", t)
125 // We must create the file first or deployFile() will abort
126 f, err := os.Create(dstPath)
132 err = deployFile(&File{