forked from aptly-dev/aptly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (82 loc) · 1.64 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"github.com/gonuts/commander"
"github.com/smira/aptly/cmd"
"github.com/smira/aptly/utils"
"os"
"path/filepath"
)
var (
returnCode = 0
errorMessage string
)
func fatal(err error) {
errorMessage = fmt.Sprintf("ERROR: %s\n", err)
returnCode = 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))
return nil
}
}
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() {
defer func() {
if errorMessage != "" {
fmt.Print(errorMessage)
}
if returnCode != 0 {
os.Exit(returnCode)
}
}()
command := cmd.RootCommand()
err := command.Flag.Parse(os.Args[1:])
if err != nil {
fatal(err)
return
}
err = loadConfig(command)
if err != nil {
fatal(err)
return
}
if returnCode != 0 {
return
}
err = cmd.InitContext(command)
if err != nil {
fatal(err)
return
}
defer cmd.ShutdownContext()
err = command.Dispatch(command.Flag.Args())
if err != nil {
fatal(err)
return
}
}