forked from zaquestion/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (102 loc) · 2.68 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
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path"
"strings"
"syscall"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
gitconfig "github.com/tcnksm/go-gitconfig"
"github.com/zaquestion/lab/cmd"
lab "github.com/zaquestion/lab/internal/gitlab"
"golang.org/x/crypto/ssh/terminal"
)
// version gets set on releases during build by goreleaser.
var version = "master"
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
cmd.Version = version
home, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
viper.SetConfigName("lab")
viper.SetConfigType("hcl")
confpath := path.Join(home, ".config")
viper.AddConfigPath(".")
viper.AddConfigPath(confpath)
viper.AutomaticEnv()
err = viper.ReadInConfig()
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
host, user, token := legacyLoadConfig()
writeConfig(confpath, host, user, token)
err = viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
}
c := viper.AllSettings()["core"]
config := c.([]map[string]interface{})[0]
lab.Init(
config["host"].(string),
config["user"].(string),
config["token"].(string))
cmd.Execute()
}
func writeConfig(confpath, host, user, token string) {
viper.Set("core.host", host)
viper.Set("core.user", user)
viper.Set("core.token", token)
err := viper.WriteConfigAs(path.Join(confpath, "lab.hcl"))
if err != nil {
log.Fatal(err)
}
}
const defaultGitLabHost = "https://gitlab.com"
// legacyLoadConfig handles all of the credential setup and prompts for user
// input when not present
func legacyLoadConfig() (host, user, token string) {
reader := bufio.NewReader(os.Stdin)
var err error
host, err = gitconfig.Entire("gitlab.host")
if err != nil {
fmt.Printf("Enter default GitLab host (default: %s): ", defaultGitLabHost)
host, err = reader.ReadString('\n')
host = strings.TrimSpace(host)
if err != nil {
log.Fatal(err)
}
if host == "" {
host = defaultGitLabHost
}
}
var errt error
user, err = gitconfig.Entire("gitlab.user")
token, errt = gitconfig.Entire("gitlab.token")
if err != nil {
fmt.Print("Enter default GitLab user: ")
user, err = reader.ReadString('\n')
user = strings.TrimSpace(user)
if err != nil {
log.Fatal(err)
}
if user == "" {
log.Fatal("git config gitlab.user must be set")
}
tokenURL := path.Join(host, "profile/personal_access_tokens")
// If the default user is being set this is the first time lab
// is being run.
if errt != nil {
fmt.Printf("Create a token here: %s\nEnter default GitLab token (scope: api): ", tokenURL)
byteToken, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatal(err)
}
token = strings.TrimSpace(string(byteToken))
}
}
return
}