-
Notifications
You must be signed in to change notification settings - Fork 3
/
root.go
93 lines (74 loc) · 1.88 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
package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/cp-tools/cpt/pkg/conf"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "cpt",
Short: "Lightweight cli tool for competitive programming!",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Initialize global configurations.
initGlobalConf()
// Set verbose text coloring.
color.NoColor = !cnf.GetBool("ui.stdoutColor")
},
Version: "v0.16.3",
TraverseChildrenHooks: true,
}
var (
rootDir string
confDir string
checkerDir string
cnf *conf.Conf
)
// Execute adds all child commands to the root command and
// sets flags appropriately. Called by main.main()
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfDir)
}
// Determine and set configDir path.
func initConfDir() {
dir, err := os.UserConfigDir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
rootDir = filepath.Join(dir, "cp-tools")
confDir = filepath.Join(rootDir, "cpt")
if err := os.MkdirAll(confDir, os.ModePerm); err != nil {
log.Fatalf("error creating config folder: %v", err)
}
checkerDir = filepath.Join(rootDir, "cpt-checker")
if err := os.MkdirAll(checkerDir, os.ModePerm); err != nil {
log.Fatalf("error creating checker folder: %v", err)
}
}
func initGlobalConf() {
// Load global configuration.
cnf = conf.New("global")
cnf.SetDefault("ui.stdoutColor", true)
cnfFilePath := filepath.Join(confDir, "cpt.yaml")
cnf.LoadFile(cnfFilePath)
// Load checker configuration.
cnf = conf.New("checker").SetParent(cnf)
cnfFilePath = filepath.Join(checkerDir, "checker.yaml")
cnf.LoadFile(cnfFilePath)
}
/*
First comes global conf.
Next comes all global, non main confs.
Then sub module (codeforces, atcoder) conf.
Lastly, local (folder) conf.
global --> checker --> .... --> local
*/