]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/main.go
b20e5cb84ee457ee25ab96ed8ff9ccb07702aa7b
[safcm/safcm.git] / cmd / safcm / main.go
1 // Command line tool to manage remote hosts
2
3 // Copyright (C) 2021-2024  Simon Ruderich
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU 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 General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 package main
19
20 import (
21         "log"
22         "os"
23
24         "ruderich.org/simon/safcm/cmd/safcm/config"
25 )
26
27 func usage() {
28         log.SetFlags(0)
29         log.Fatalf("usage: %[1]s sync [<options>] <host|group...>\n"+
30                 "       %[1]s fixperms\n"+
31                 "       %[1]s version\n"+
32                 "", os.Args[0])
33 }
34
35 func main() {
36         if len(os.Args) < 2 {
37                 usage()
38         }
39
40         var err error
41         switch os.Args[1] {
42         case "sync":
43                 err = MainSync(os.Args)
44         case "fixperms":
45                 if len(os.Args) != 2 {
46                         usage()
47                 }
48                 err = MainFixperms()
49         case "version":
50                 if len(os.Args) != 2 {
51                         usage()
52                 }
53                 err = MainVersion()
54         default:
55                 usage()
56         }
57         if err != nil {
58                 log.Fatal(err)
59         }
60 }
61
62 func LoadBaseFiles() (*config.Config, *config.Hosts, map[string][]string,
63         error) {
64
65         cfg, err := config.LoadConfig()
66         if err != nil {
67                 return nil, nil, nil, err
68         }
69         hosts, err := config.LoadHosts()
70         if err != nil {
71                 return nil, nil, nil, err
72         }
73         groups, err := config.LoadGroups(cfg, hosts)
74         if err != nil {
75                 return nil, nil, nil, err
76         }
77         return cfg, hosts, groups, nil
78 }