-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
64 lines (54 loc) · 1.87 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
/*
Copyright © 2024 Angad Behl
*/
package main
import (
"io/fs"
"strings"
"github.com/charmbracelet/log"
"github.com/joho/godotenv"
"github.com/slashtechno/gobackup-github/cmd"
"github.com/slashtechno/gobackup-github/internal"
"github.com/spf13/cobra"
)
func init() {
// Load .env file
if err := godotenv.Load(); err != nil {
log.Info("Failed to load .env file", "error", err)
}
cobra.OnInitialize(initConfig)
}
func initConfig() {
internal.Viper.SetEnvPrefix("GOBACKUP_GITHUB")
// https://github.com/spf13/viper?tab=readme-ov-file#working-with-environment-variables
internal.Viper.AutomaticEnv()
internal.Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// cmd.ConfigFile should always be set since it has a default
if cmd.ConfigFile != "" {
internal.Viper.SetConfigFile(cmd.ConfigFile)
} else {
log.Warn("Default config file flag value not retrievable")
internal.Viper.SetConfigFile("config.yaml")
}
if err := internal.Viper.ReadInConfig(); err == nil {
log.Debug("Configuration file loaded", "file", internal.Viper.ConfigFileUsed())
} else {
// Generate a default .env file null values
if _, ok := err.(*fs.PathError); ok {
log.Debug("Configuration file not found, creating a new one", "file", cmd.ConfigFile)
// Set defaults after binding to flags instead
// internal.Viper.SetDefault("log-level", "info")
// internal.Viper.SetDefault("github-token", "")
// internal.Viper.SetDefault("username", "")
// internal.Viper.SetDefault("interval", "")
if err := internal.Viper.WriteConfigAs(cmd.ConfigFile); err != nil {
log.Fatal("Failed to write configuration file:", err)
}
log.Fatal("Failed to read config file. Created a config file with default values. Please edit the file and run the command again.", "path", cmd.ConfigFile)
}
log.Fatal("Failed to read configurationfile:", "error", err)
}
}
func main() {
cmd.Execute()
}