-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_app.go
53 lines (46 loc) · 1.02 KB
/
link_app.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
package client
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/mdp/qrterminal/v3"
"github.com/your-overtime/cli/internal/conf"
)
func (c *Client) LinkApp() error {
co, err := conf.LoadConfig()
if err != nil {
return err
}
var token string
newToken := true
prompt := &survey.Confirm{
Message: "Do you like to create a new token",
}
survey.AskOne(prompt, &newToken)
if newToken {
now := time.Now()
t, err := c.CreateToken(fmt.Sprintf("APP %s", now), false)
if err != nil {
return err
}
token = fmt.Sprintf("token %s", t.Token)
println("New token created")
} else {
token = co.Token
}
qc := qrterminal.Config{
Level: qrterminal.M,
Writer: os.Stdout,
BlackChar: qrterminal.BLACK,
WhiteChar: qrterminal.WHITE,
QuietZone: 4,
}
payload, err := json.Marshal(map[string]string{"url": co.Host, "authheader": token, "desc": co.DefaultActivityDesc})
if err != nil {
return err
}
qrterminal.GenerateWithConfig(string(payload), qc)
return nil
}