forked from jfrog/artifactory-cli-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
136 lines (121 loc) · 3.82 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package commands
import (
"os"
"fmt"
"bytes"
"strings"
"syscall"
"io/ioutil"
"encoding/json"
"github.com/eyalbe4/artifactory-cli-go/utils"
"github.com/eyalbe4/artifactory-cli-go/Godeps/_workspace/src/golang.org/x/crypto/ssh/terminal"
)
func Config(details *utils.ArtifactoryDetails, interactive, shouldEncPassword bool) {
if interactive {
if details.Url == "" {
print("Artifactory URL: ")
fmt.Scanln(&details.Url)
}
if strings.Index(details.Url, "ssh://") == 0 || strings.Index(details.Url, "SSH://") == 0 {
readSshKeyPathFromConsole(details)
} else {
readCredentialsFromConsole(details)
}
}
details.Url = utils.AddTrailingSlashIfNeeded(details.Url)
if shouldEncPassword {
details = encryptPassword(details)
}
writeConfFile(details)
}
func readSshKeyPathFromConsole(details *utils.ArtifactoryDetails) {
if details.SshKeyPath == "" {
print("SSH key file path: ")
fmt.Scanln(&details.SshKeyPath)
}
if !utils.IsFileExists(details.SshKeyPath) {
fmt.Println("Warning: Could not find SSH key file at: " + details.SshKeyPath)
}
}
func readCredentialsFromConsole(details *utils.ArtifactoryDetails) {
if details.User == "" {
print("User: ")
fmt.Scanln(&details.User)
}
if details.Password == "" {
print("Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
details.Password = string(bytePassword)
utils.CheckError(err)
}
}
func ShowConfig() {
details := readConfFile()
if details.Url != "" {
fmt.Println("Url: " + details.Url)
}
if details.User != "" {
fmt.Println("User: " + details.User)
}
if details.Password != "" {
fmt.Println("Password: " + details.Password)
}
if details.SshKeyPath != "" {
fmt.Println("SSH key file path: " + details.SshKeyPath)
}
}
func ClearConfig() {
writeConfFile(new(utils.ArtifactoryDetails))
}
func GetConfig() *utils.ArtifactoryDetails {
return readConfFile()
}
func encryptPassword(details *utils.ArtifactoryDetails) *utils.ArtifactoryDetails {
if details.Password == "" {
return details
}
response, encPassword := utils.GetEncryptedPasswordFromArtifactory(details)
switch response.StatusCode {
case 409:
utils.Exit(utils.ExitCodeError, "\nYour Artifactory server is not configured to encrypt passwords.\n" +
"You may use \"art config --enc-password=false\"")
case 200:
details.Password = encPassword
default:
utils.Exit(utils.ExitCodeError, "\nArtifactory response: " + response.Status)
}
return details
}
func getConFilePath() string {
userDir := utils.GetHomeDir()
if userDir == "" {
utils.Exit(utils.ExitCodeError, "Couldn't find home directory. Make sure your HOME environment variable is set.")
}
confPath := userDir + "/.jfrog/"
os.MkdirAll(confPath ,0777)
return confPath + "art-cli.conf"
}
func writeConfFile(details *utils.ArtifactoryDetails) {
confFilePath := getConFilePath()
if !utils.IsFileExists(confFilePath) {
out, err := os.Create(confFilePath)
utils.CheckError(err)
defer out.Close()
}
b, err := json.Marshal(&details)
utils.CheckError(err)
var content bytes.Buffer
err = json.Indent(&content, b, "", " ")
utils.CheckError(err)
ioutil.WriteFile(confFilePath,[]byte(content.String()), 0x777)
}
func readConfFile() *utils.ArtifactoryDetails {
confFilePath := getConFilePath()
details := new(utils.ArtifactoryDetails)
if !utils.IsFileExists(confFilePath) {
return details
}
content := utils.ReadFile(confFilePath)
json.Unmarshal(content, &details)
return details
}