-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (66 loc) · 1.36 KB
/
main.go
File metadata and controls
80 lines (66 loc) · 1.36 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/deploys-app/api/client"
"golang.org/x/oauth2/google"
"github.com/deploys-app/deploys/internal/runner"
)
func main() {
args := os.Args
if len(args) <= 1 {
help()
return
}
var (
token = os.Getenv("DEPLOYS_TOKEN")
authUser = os.Getenv("DEPLOYS_AUTH_USER")
authPass = os.Getenv("DEPLOYS_AUTH_PASS")
endpoint = os.Getenv("DEPLOYS_ENDPOINT")
)
apiClient := &client.Client{
Endpoint: endpoint,
HTTPClient: &http.Client{
Timeout: 15 * time.Second,
},
}
if apiClient.Auth == nil && authUser != "" && authPass != "" {
apiClient.Auth = func(r *http.Request) {
r.SetBasicAuth(authUser, authPass)
}
}
if apiClient.Auth == nil && token == "" {
token, _ = getDefaultToken()
}
if apiClient.Auth == nil && token != "" {
apiClient.Auth = func(r *http.Request) {
r.Header.Set("Authorization", "Bearer "+token)
}
}
rn := runner.Runner{
API: apiClient,
Output: os.Stdout,
}
err := rn.Run(args[1:]...)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func getDefaultToken() (string, error) {
cred, err := google.FindDefaultCredentials(context.Background())
if err != nil {
return "", err
}
tk, err := cred.TokenSource.Token()
if err != nil {
return "", err
}
return tk.AccessToken, nil
}
func help() {
fmt.Println("deploys.app cli")
}