/
root.go
70 lines (63 loc) · 1.42 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
package cmd
import (
"fmt"
"os"
"github.com/it-akumi/vlto/config"
"github.com/it-akumi/vlto/project"
"github.com/spf13/cobra"
)
// flags
var cfgFilePath string
var format string
var httpsProxy string
var rootCmd = &cobra.Command{
Use: "vlto",
Short: "vlto shows velocity of your projects of Toggl",
Version: "0.0",
Run: func(cmd *cobra.Command, args []string) {
// --proxy option overrides an environment variable HTTPS_PROXY.
// Since a child process cannot change environment variables of its parent process,
// it's not necessary to rewrite it to original value.
if err := os.Setenv("HTTPS_PROXY", httpsProxy); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := project.Show(format); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(
&cfgFilePath,
"config",
"",
"config file (default is $HOME/.config/vlto.toml)",
)
rootCmd.PersistentFlags().StringVar(
&format,
"format",
"",
fmt.Sprintf(`the output format "%s" or "%s"`, project.TableFormat, project.JsonFormat),
)
rootCmd.PersistentFlags().StringVar(
&httpsProxy,
"proxy",
"",
"the URL of an environment variable HTTPS_PROXY",
)
}
func initConfig() {
if err := config.Init(cfgFilePath); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}