-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (93 loc) · 2.62 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
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"os"
"strings"
cli "github.com/jawher/mow.cli"
log "github.com/sirupsen/logrus"
"github.com/yottta/configbuddy.v2/backup"
"github.com/yottta/configbuddy.v2/executor"
"github.com/yottta/configbuddy.v2/model"
"github.com/yottta/configbuddy.v2/parser"
)
const (
appSystemCode = "configbuddy"
appDescription = "App done for installing my dotfiles"
)
func main() {
app := initApp()
err := app.Run(os.Args)
if err != nil {
log.Errorf("App could not start, error=[%s]\n", err)
os.Exit(1)
}
}
func initApp() *cli.Cli {
app := cli.App(appSystemCode, appDescription)
configs := app.Strings(cli.StringsOpt{
Name: "configs c",
Value: []string{},
Desc: "The path for config files",
})
backupActivated := app.Bool(cli.BoolOpt{
Name: "backup b",
Value: false,
Desc: "Boolean saying if the backup should be performed or not. If you want backup to directory, specify -p too",
})
backupDirectory := app.String(cli.StringOpt{
Name: "backup-path p",
Value: "",
Desc: "Path of the folder where the backup will be performed",
})
loggingLevel := app.String(cli.StringOpt{
Name: "log-level l",
Value: "info",
Desc: getLoggingFlagDescription(),
})
app.Action = func() {
initLogging(*loggingLevel)
log.Infof("configbuddy started")
args := &model.Arguments{
Configs: *configs,
BackupDirectory: *backupDirectory,
BackupActivated: *backupActivated,
}
backupService, err := backup.NewBackupService(args)
if err != nil {
log.WithError(err).Error("could not create the backup instance")
return
}
parse, err := parser.NewParser()
if err != nil {
log.WithError(err).Error("could not create the parser instance")
return
}
err = executor.StartConfiguring(args, parse, backupService)
if err != nil {
log.WithError(err).Error("error during configuration process")
return
}
}
return app
}
func initLogging(loggingLevel string) {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.TextFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
logLevel, err := log.ParseLevel(strings.ToLower(loggingLevel))
if err != nil {
panic(err)
}
log.SetLevel(logLevel)
log.Infof("logging level set to %s", strings.ToLower(loggingLevel))
}
func getLoggingFlagDescription() string {
var levelsAsString []string
for _, lvl := range log.AllLevels {
levelsAsString = append(levelsAsString, lvl.String())
}
return fmt.Sprintf("the logging level. valid values: %s", strings.Join(levelsAsString, ","))
}