-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcommands.go
132 lines (100 loc) · 3.75 KB
/
commands.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
package main
import (
"fmt"
"log"
"github.com/spf13/cobra"
)
func (fs *fitbitSync) rootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "fitbit-sync",
}
cmd.PersistentFlags().StringVarP(&fs.configDir, "config-dir", "c", configDir(), "Configuration directory")
cmd.PersistentFlags().StringVarP(&fs.configFile, "config-file", "f", "fitbit.json", "Configuration file inside the directory")
cmd.AddCommand(fs.initCmd())
cmd.AddCommand(fs.showCmd())
cmd.AddCommand(fs.syncCmd())
return cmd
}
func (fs *fitbitSync) initCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize the Fitbit oauth configuration",
RunE: func(cmd *cobra.Command, args []string) error {
if err := fs.loadConfig(); err != nil {
return fmt.Errorf("could not load config: %w", err)
}
fs.initClient()
if err := fs.getToken(); err != nil {
return fmt.Errorf("could not get token: %w", err)
}
fmt.Println("Configuration saved. You should now be able to use the other commands.")
return nil
},
}
cmd.Flags().StringVarP(&fs.bind, "redirect-bind", "r", "http://localhost:8080", "IP and port to listen for redirect request")
cmd.Flags().StringVarP(&fs.cfg.FitbitConfig.ClientID, "client-id", "i", "", "Fitbit client ID")
cmd.Flags().StringVarP(&fs.cfg.FitbitConfig.ClientSecret, "client-secret", "s", "", "Fitbit client secret")
cmd.Flags().StringVarP(&fs.cfg.FitbitConfig.UserID, "user-id", "u", "", "Fitbit user ID")
for _, flag := range []string{"client-id", "client-secret", "user-id"} {
if err := cmd.MarkFlagRequired(flag); err != nil {
log.Fatal(err)
}
}
return cmd
}
func (fs *fitbitSync) showCmd() *cobra.Command {
var days int
cmd := &cobra.Command{
Use: "show",
Short: "Show the Fitbit activities",
RunE: func(cmd *cobra.Command, args []string) error {
if err := fs.loadConfig(); err != nil {
return fmt.Errorf("could not load config: %w", err)
}
if err := fs.reinitClient(); err != nil {
return fmt.Errorf("could not initialize Fitbit client: %w", err)
}
fs.showProfile()
if err := fs.showActivities(days); err != nil {
return fmt.Errorf("could not show activities: %w", err)
}
return nil
},
}
cmd.Flags().IntVarP(&days, "days", "d", 7, "Number of days to show")
return cmd
}
func (fs *fitbitSync) syncCmd() *cobra.Command {
var days int
cmd := &cobra.Command{
Use: "sync",
Short: "Sync the Fitbit activities to your Workout Tracker",
RunE: func(cmd *cobra.Command, args []string) error {
if err := fs.loadConfig(); err != nil {
return fmt.Errorf("could not load config: %w", err)
}
fs.WorkoutConfig.CopyFrom(&fs.cfg.WorkoutConfig)
if fs.WorkoutConfig.persist {
fs.cfg.WorkoutConfig = fs.WorkoutConfig
if err := fs.saveConfig(); err != nil {
return fmt.Errorf("could not save config: %w", err)
}
}
if err := fs.reinitClient(); err != nil {
return fmt.Errorf("could not initialize Fitbit client: %w", err)
}
fs.showProfile()
fs.syncActivities(days)
return nil
},
}
cmd.Flags().IntVarP(&days, "days", "d", 7, "Number of days to show")
cmd.Flags().StringVarP(&fs.WorkoutConfig.URL, "url", "u", "", "Workout Tracker root URL")
cmd.Flags().StringVarP(&fs.WorkoutConfig.APIKey, "key", "k", "", "Workout Tracker API key")
cmd.Flags().BoolVar(&fs.WorkoutConfig.syncWeight, "weight", true, "Sync weight from Fitbit")
cmd.Flags().BoolVar(&fs.WorkoutConfig.syncHeight, "height", true, "Sync height from Fitbit")
cmd.Flags().BoolVar(&fs.WorkoutConfig.syncSteps, "steps", true, "Sync steps from Fitbit")
cmd.Flags().BoolVar(&fs.WorkoutConfig.syncActivities, "activities", true, "Sync activities from Fitbit")
cmd.Flags().BoolVarP(&fs.WorkoutConfig.persist, "persist", "p", false, "Persist Workout Tracker configuration")
return cmd
}