/
root.go
111 lines (93 loc) · 3.59 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
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
111
package cmd
import (
"errors"
"fmt"
"os"
"bitbucket.org/ai69/amoy"
"github.com/1set/gut/yos"
"github.com/1set/gut/ystring"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)
var (
configFile string
fallbackConfigFile string
debugMode bool
logLevel string
logFile string
log *zap.SugaredLogger
)
var (
errNoConfigDevice = errors.New("no registered devices")
)
var (
logoArt = `
██████╗ █████╗ ██████╗ ██╗ ██╗███╗ ███╗███████╗
██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝████╗ ████║██╔════╝
██████╔╝███████║██████╔╝█████╔╝ ██╔████╔██║█████╗
██╔══██╗██╔══██║██╔══██╗██╔═██╗ ██║╚██╔╝██║██╔══╝
██████╔╝██║ ██║██║ ██║██║ ██╗██║ ╚═╝ ██║███████╗
╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝
`
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "barkme",
Short: "Push notifications to iOS devices with Bark",
Long: colorLogoArt + ystring.NewLine +
`A simple tool to manage registered iOS devices with Bark app installed,
and push notifications to the iOS devices via Bark server.
- Bark Client: https://github.com/Finb/Bark
- Bark Server: https://github.com/Finb/bark-server`,
SilenceUsage: true,
SilenceErrors: true,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.barkme.yaml)")
rootCmd.PersistentFlags().StringVar(&logLevel, "log", "info", "minimum enabled logging level")
rootCmd.PersistentFlags().StringVar(&logFile, "logfile", "", "log file path")
rootCmd.PersistentFlags().BoolVar(&debugMode, "debug", false, "debug mode")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
// Set loggers
logger := amoy.NewLogger(logFile, debugMode)
logger.SetLogLevel(logLevel)
log = logger.LoggerSugared().With(zap.Int("pid", os.Getpid()))
if configFile != "" {
// Use config file from the flag.
viper.SetConfigFile(configFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Fatalw("fail to find homedir", zap.Error(err))
}
// Create config directory under user's home directory
dirName := yos.JoinPath(home, ".config", "barkme")
if err := yos.MakeDir(dirName); err != nil {
log.Fatalw("fail to create config dir", zap.Error(err))
}
// Search config in current directory and config directory (without extension).
viper.AddConfigPath(".")
viper.AddConfigPath(dirName)
viper.SetConfigName("config")
viper.SetConfigType("yaml")
// Create if config file doesn't exist
fallbackConfigFile = yos.JoinPath(dirName, "config.yaml")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in, or initialize default one
loadConfig()
}