This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
118 lines (95 loc) · 2.73 KB
/
config.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
116
117
118
package config
import (
"fmt"
"github.com/danieljoos/wincred"
"github.com/gen2brain/dlgs"
"net/url"
"strings"
"time"
)
const credentialTarget string = "bittray:conf"
// AskConfig interactively asks configuration information from the user
func AskConfig() (ok bool) {
dlgs.Warning("Bittray", "You're new here! Let's get you set up. You'll need to provide your Bitbucket username, password and URL.")
username, ok, _ := askUser()
if !ok {
return ok
}
address, ok, _ := askURL()
if !ok {
return ok
}
StoreConfig(username, address)
return ok
}
// GetConfig retrieves the persisted configuration from the WCM
func GetConfig() (user string, url string) {
cred, err := wincred.GetGenericCredential(credentialTarget)
if err == nil {
return cred.UserName, string(cred.CredentialBlob)
}
return "", ""
}
// StoreConfig persists the configuration to the WCM
func StoreConfig(username string, url string) {
cred := wincred.NewGenericCredential(credentialTarget)
cred.UserName = strings.TrimSpace(username)
cred.CredentialBlob = []byte(strings.TrimSpace(url))
err := cred.Write()
if err != nil {
panic(err)
}
return
}
func askUser() (user string, ok bool, err error) {
for user == "" {
user, ok, err = dlgs.Entry("Username", "Your BitBucket username:", "")
if user == "" && ok {
dlgs.Error("Username missing", "Ok, so, without your username I can't log you in.\n\nTry again...")
} else {
return user, ok, err
}
}
return user, ok, err
}
func askURL() (pURL string, ok bool, err error) {
for pURL == "" {
pURL, ok, err = dlgs.Entry("Bitbucket URL", "Your Bitbucket URL:", "http://host.domain.com:7990")
_, parsingErr := url.ParseRequestURI(pURL)
if parsingErr != nil && ok {
dlgs.Error("Bad URL Format", "Sorry, the url you provide must be exactly of the format provided below, with a port and no trailing slash.\n\nPlease retry.")
pURL = ""
} else {
return pURL, ok, err
}
}
return pURL, ok, err
}
// AskPass interactively asks the user for their Bitbucket password
func AskPass() (pass string, ok bool, err error) {
for pass == "" {
pass, ok, err = dlgs.Password("Bitbucket Password", "Your Bitbucket password:")
if pass == "" || !ok {
return "", ok, err
}
}
return pass, ok, err
}
// DestroyConfig removes the persisted configuration in the WCM
func DestroyConfig() {
cred, err := wincred.GetGenericCredential(credentialTarget)
if err != nil {
fmt.Println(err)
return
}
err = cred.Delete()
if err != nil {
fmt.Print(err)
}
}
// GetPollingInterval returns polling interval configured by the `-poll=n` flag or a default
func GetPollingInterval() time.Duration {
//pollPtr := flag.Int("poll", 15, "Polling interval in seconds")
//flag.Parse()
return time.Second * time.Duration(20)
}