-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
94 lines (78 loc) · 2.25 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/RedHatInsights/host-metering/config"
"github.com/RedHatInsights/host-metering/daemon"
"github.com/RedHatInsights/host-metering/logger"
)
func main() {
configPath := flag.String("config", config.DefaultConfigPath, "Configuration file path")
flag.NewFlagSet("help", flag.ExitOnError)
flag.NewFlagSet("daemon", flag.ExitOnError)
flag.NewFlagSet("once", flag.ExitOnError)
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("Error: no subcommand specified")
printUsage()
return
}
command := args[0]
switch command {
case "help":
printUsage()
case "daemon", "once":
cfg := config.NewConfig()
var logMessages strings.Builder
logMessages.WriteString("Updating config from config file...\n")
err := cfg.UpdateFromConfigFile(*configPath)
if err != nil {
logMessages.WriteString(fmt.Sprintf("Failed to process file: %v\n", err.Error()))
}
logMessages.WriteString("Updating config from environment variables...\n")
err = cfg.UpdateFromEnvVars()
if err != nil {
logMessages.WriteString(fmt.Sprintf("Failed to process variables: %v\n", err.Error()))
}
// initialize the logger according to the given configuration
err = logger.InitLogger(cfg.LogPath, cfg.LogLevel, cfg.InstanceID)
if err != nil {
logger.Debugf("Error initializing logger: %s\n", err.Error())
}
//Now that the logger is configured, we can report configuration state.
logger.Infoln(logMessages.String())
//print out the configuration
logger.Infoln(cfg.String())
cv := config.NewConfigValidator(cfg)
err = cv.Validate()
if err != nil {
logger.Errorf("Invalid configuration: %v\n", err.Error())
os.Exit(2)
}
d, err := daemon.NewDaemon(cfg)
if err != nil {
logger.Errorf("Failed to create daemon: %v\n", err.Error())
os.Exit(1)
}
if command == "once" {
d.RunOnce()
return
}
d.Run()
default:
fmt.Println("Error: unknown subcommand", command)
printUsage()
}
}
func printUsage() {
fmt.Println("Usage: host-metering [OPTIONS] SUBCOMMAND")
fmt.Println("Options:")
flag.PrintDefaults()
fmt.Println("Subcommands:")
fmt.Println(" daemon Run in daemon mode")
fmt.Println(" once Execute once")
fmt.Println(" help Print this help message")
}