/
root.go
62 lines (52 loc) · 1.31 KB
/
root.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
package cmd
import (
"fmt"
"os"
"github.com/BeryJu/imagik/pkg/config"
"github.com/BeryJu/imagik/pkg/hash"
"github.com/BeryJu/imagik/pkg/server"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "imagik",
Short: "A small Fileserver",
Run: func(cmd *cobra.Command, args []string) {
server := server.New()
server.HashMap = hash.New()
go server.HashMap.RunIndexer()
err := server.Run()
if err != nil {
log.Error(err)
}
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(onInit)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is config.yaml)")
}
// onInit reads in config file and ENV variables if set.
func onInit() {
log.SetFormatter(&log.JSONFormatter{})
config.DefaultConfig()
err := config.LoadConfig(cfgFile)
if err == nil {
log.WithField("config-file", cfgFile).Info("Using config file")
} else {
log.WithField("config-file", cfgFile).WithError(err).Warning("Failed to read config")
}
if config.C.LogFormat != "json" {
log.SetFormatter(&log.TextFormatter{})
}
if config.C.Debug {
log.SetLevel(log.DebugLevel)
}
}