-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
160 lines (139 loc) · 2.95 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/sachaos/toggl/cache"
"github.com/sachaos/toggl/command"
"github.com/sachaos/toggl/lib"
"github.com/spf13/viper"
"github.com/urfave/cli"
)
var (
ConfigPath = os.Getenv("HOME")
)
const (
ConfigName = ".toggl"
ConfigType = "json"
)
const Name string = "toggl"
var version string
func main() {
cache.New(os.Getenv("HOME") + "/.toggl.cache.json")
cache.Init()
initialize()
cmdApp := command.NewApp(viper.GetString("token"))
app := cli.NewApp()
app.Name = Name
app.Version = version
app.Author = "sachaos"
app.Email = "sakataku7@gmail.com"
app.Usage = "Toggl API CLI Client"
app.Flags = GlobalFlags
app.Commands = []cli.Command{
{
Name: "start",
Usage: "Start time entry",
Action: cmdApp.CmdStart,
Flags: []cli.Flag{
projectIDFlag,
},
},
{
Name: "stop",
Usage: "End time entry",
Action: cmdApp.CmdStop,
Flags: []cli.Flag{},
},
{
Name: "current",
Usage: "Show current time entry",
Action: cmdApp.CmdCurrent,
Flags: []cli.Flag{},
},
{
Name: "workspaces",
Usage: "Show workspaces",
Action: cmdApp.CmdWorkspaces,
},
{
Name: "projects",
Usage: "Show projects on current workspaces",
Action: cmdApp.CmdProjects,
},
{
Name: "local",
Usage: "Set current dir workspace",
Action: CmdLocal,
Flags: []cli.Flag{
projectIDFlag,
},
},
{
Name: "global",
Usage: "Set global workspace",
Action: CmdGlobal,
},
}
app.CommandNotFound = CommandNotFound
if err := app.Run(os.Args); err != nil {
log.Fatalf("Error: %s", err.Error())
os.Exit(1)
}
}
func requireToken() error {
var token string
var workspaces []toggl.Workspace
var err error
count := 0
for count < 3 {
fmt.Printf("Input API Token: ")
fmt.Scan(&token)
client := toggl.NewDefaultClient(token)
workspaces, err = client.FetchWorkspaces()
if err == nil {
viper.Set("token", token)
viper.Set("wid", workspaces[0].ID)
return nil
}
count++
}
panic(fmt.Errorf("Invalid token"))
}
func RootConfigFilePath() string {
return filepath.Join(ConfigPath, ConfigName+"."+ConfigType)
}
func LocalConfigFilePath() string {
return filepath.Join(".", ConfigName+"."+ConfigType)
}
func CreateConfig(path string, hash interface{}) {
buf, _ := json.MarshalIndent(hash, "", " ")
err := ioutil.WriteFile(path, buf, os.ModePerm)
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
func LoadLocalConfig() error {
localFilePath := LocalConfigFilePath()
file, err := os.Open(localFilePath)
if err != nil {
return err
}
viper.MergeConfig(file)
return nil
}
func initialize() {
viper.SetConfigType(ConfigType)
viper.SetConfigName(ConfigName)
viper.AddConfigPath(ConfigPath)
viper.AddConfigPath(".")
viper.ReadInConfig()
LoadLocalConfig()
if !viper.IsSet("token") {
requireToken()
CreateConfig(RootConfigFilePath(), viper.AllSettings())
}
}