forked from aptly-dev/aptly
/
main.go
75 lines (62 loc) · 1.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/gonuts/commander"
"github.com/smira/aptly/cmd"
"github.com/smira/aptly/utils"
"os"
"path/filepath"
)
func fatal(err error) {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
func loadConfig(command *commander.Command) error {
var err error
configLocation := command.Flag.Lookup("config").Value.String()
if configLocation != "" {
err = utils.LoadConfig(configLocation, &utils.Config)
if err != nil {
return err
}
} else {
configLocations := []string{
filepath.Join(os.Getenv("HOME"), ".aptly.conf"),
"/etc/aptly.conf",
}
for _, configLocation := range configLocations {
err = utils.LoadConfig(configLocation, &utils.Config)
if err == nil {
break
}
if !os.IsNotExist(err) {
fatal(fmt.Errorf("error loading config file %s: %s", configLocation, err))
}
}
if err != nil {
fmt.Printf("Config file not found, creating default config at %s\n\n", configLocations[0])
utils.SaveConfig(configLocations[0], &utils.Config)
}
}
return nil
}
func main() {
command := cmd.RootCommand()
err := command.Flag.Parse(os.Args[1:])
if err != nil {
fatal(err)
}
err = loadConfig(command)
if err != nil {
fatal(err)
}
err = cmd.InitContext(command)
if err != nil {
fatal(err)
}
defer cmd.ShutdownContext()
err = command.Dispatch(command.Flag.Args())
if err != nil {
fatal(err)
}
}