forked from juruen/rmapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (73 loc) · 1.58 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
package main
import (
"flag"
"fmt"
"os"
"github.com/juruen/rmapi/api"
"github.com/juruen/rmapi/config"
"github.com/juruen/rmapi/log"
"github.com/juruen/rmapi/shell"
"github.com/juruen/rmapi/version"
)
const AUTH_RETRIES = 3
func parseOfflineCommands(cmd []string) bool {
if len(cmd) == 0 {
return false
}
switch cmd[0] {
case "reset":
configFile, err := config.ConfigPath()
if err != nil {
log.Error.Fatalln(err)
}
if err := os.Remove(configFile); err != nil {
log.Error.Fatalln(err)
}
return true
case "version":
fmt.Println(version.Version)
return true
}
return false
}
func main() {
ni := flag.Bool("ni", false, "not interactive (prevents asking for code)")
flag.Usage = func() {
fmt.Println(`
help detailed commands, but the user needs to be logged in
Offline Commands:
version prints the version
reset removes the config file `)
flag.PrintDefaults()
}
flag.Parse()
otherFlags := flag.Args()
if parseOfflineCommands(otherFlags) {
return
}
var ctx api.ApiCtx
var err error
var userInfo *api.UserInfo
for i := 0; i < AUTH_RETRIES; i++ {
authCtx := api.AuthHttpCtx(i > 0, *ni)
userInfo, err = api.ParseToken(authCtx.Tokens.UserToken)
if err != nil {
log.Trace.Println(err)
continue
}
ctx, err = api.CreateApiCtx(authCtx, userInfo.SyncVersion)
if err != nil {
log.Trace.Println(err)
} else {
break
}
}
if err != nil {
log.Error.Fatal("failed to build documents tree, last error: ", err)
}
err = shell.RunShell(ctx, userInfo, otherFlags)
if err != nil {
log.Error.Println("Error: ", err)
os.Exit(1)
}
}