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"+
52 err := mainFetch(args[1])
63 err := mainConvert(args[1], args[2], args[3])
74 func mainFetch(cfgPath string) error {
75 cfg, err := LoadConfig(cfgPath)
79 state, err := LoadState(cfg.StatePath)
83 err = handleFiles(cfg, state)
87 // NOTE: Make sure to call WriteState() only if there were no
88 // errors (see WriteState() and README)
89 err = WriteState(cfg.StatePath, state)
96 func mainConvert(typ, srcPath, dstPath string) error {
98 err := t.UnmarshalText([]byte(typ))
103 src, err := ioutil.ReadFile(srcPath)
108 if t == FileTypePlain {
110 } else if t == FileTypePasswd {
111 pws, err := ParsePasswds(bytes.NewReader(src))
115 err = SerializePasswds(&x, pws)
119 } else if t == FileTypeGroup {
120 grs, err := ParseGroups(bytes.NewReader(src))
124 err = SerializeGroups(&x, grs)
129 return fmt.Errorf("unsupported file type %v", t)
132 // We must create the file first or deployFile() will abort
133 f, err := os.Create(dstPath)
139 err = deployFile(&File{