-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (112 loc) · 2.85 KB
/
main.go
File metadata and controls
137 lines (112 loc) · 2.85 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
urllib "net/url"
"os"
"os/exec"
"runtime"
"github.com/ddliu/go-httpclient"
"github.com/urfave/cli/v2"
)
type Configuration struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
}
var ClientId string
var ClientSecret string
func main() {
httpclient.Defaults(httpclient.Map{
"Content-Type": "",
})
app := &cli.App{
Name: "quire-cli",
Description: "Lo Agency Quire CLI",
Authors: []*cli.Author{
{
Name: "Aien Saidi",
Email: "aien@lo.agency",
},
},
Version: "0.0.2",
Commands: []*cli.Command{
GitCommand,
{
Name: "authorize",
Usage: "Authorize the app to use Quire Boards",
Action: func(c *cli.Context) error {
var err error
var cmd *exec.Cmd
var url = fmt.Sprintf("https://quire.io/oauth?client_id=%s&redirect_uri=http://localhost:1992", ClientId)
switch runtime.GOOS {
case "linux":
cmd = exec.Command("xdg-open", url)
case "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
case "darwin":
cmd = exec.Command("open", url)
default:
return fmt.Errorf("unsupported platform")
}
err = cmd.Start()
if err != nil {
return err
}
m := http.NewServeMux()
s := http.Server{Addr: ":1992", Handler: m}
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
keys, ok := r.URL.Query()["code"]
if !ok || len(keys[0]) < 1 {
_, _ = fmt.Fprintf(w, "url param 'code' is missing")
return
}
// Query()["key"] will return an array of items,
// we only want the single item.
key := keys[0]
formData := urllib.Values{
"grant_type": {"authorization_code"},
"code": {key},
"client_id": {ClientId},
"client_secret": {ClientSecret},
}
resp, err := http.PostForm("https://quire.io/oauth/token", formData)
if err != nil {
log.Fatalln(err)
}
var result Configuration
/*
{
"access_token":"ACCESS_TOKEN",
"token_type":"bearer",
"expires_in":2592000,
"refresh_token":"REFRESH_TOKEN"
}
*/
json.NewDecoder(resp.Body).Decode(&result)
err = SaveConfig(result)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
log.Printf("configurations are saved, you can now use the extension!\ntoken: %s\n", key)
_ = s.Shutdown(context.Background())
return
})
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}