1 // Main file for nsscash
3 // Copyright (C) 2019-2020 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/>.
29 "github.com/google/renameio"
34 fmt.Fprintf(os.Stderr,
35 "usage: %[1]s [options] fetch <config>\n"+
36 "usage: %[1]s [options] convert <type> <src> <dst>\n"+
55 err := mainFetch(args[1])
66 err := mainConvert(args[1], args[2], args[3])
77 func mainFetch(cfgPath string) error {
78 cfg, err := LoadConfig(cfgPath)
82 state, err := LoadState(cfg.StatePath)
86 err = handleFiles(cfg, state)
90 // NOTE: Make sure to call WriteState() only if there were no
91 // errors (see WriteState() and README)
92 err = WriteState(cfg.StatePath, state)
99 func mainConvert(typ, srcPath, dstPath string) error {
101 err := t.UnmarshalText([]byte(typ))
106 src, err := ioutil.ReadFile(srcPath)
111 if t == FileTypePlain {
113 } else if t == FileTypePasswd {
114 pws, err := ParsePasswds(bytes.NewReader(src))
118 err = SerializePasswds(&x, pws)
122 } else if t == FileTypeGroup {
123 grs, err := ParseGroups(bytes.NewReader(src))
127 err = SerializeGroups(&x, grs)
132 return fmt.Errorf("unsupported file type %v", t)
135 // We must create the file first or deployFile() will abort; this is
136 // ugly because deployFile() already performs an atomic replacement
137 // but the simplest solution with the least duplicate code
138 f, err := renameio.TempFile(filepath.Dir(dstPath), dstPath)
144 err = deployFile(&File{
154 return f.CloseAtomicallyReplace()