/
operations.go
130 lines (112 loc) · 3.4 KB
/
operations.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
package utils
import (
"fmt"
"net"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/spaceuptech/space-cloud/space-cli/cmd/model"
)
// CheckPortAvailability checks if specified port is available on local machine
func CheckPortAvailability(port, s string) (string, error) {
ln, err := net.Listen("tcp", ":"+port)
if err != nil {
LogInfo(fmt.Sprintf("The port %s is current busy", port))
if err := survey.AskOne(&survey.Input{Message: fmt.Sprintf("Enter %s port", s)}, &port); err != nil {
return "", LogError("error getting port", err)
}
if port == "" {
return "", LogError("Invalid port", err)
}
return CheckPortAvailability(port, s)
}
_ = ln.Close()
return port, nil
}
// RemoveAccount removes account from accounts file
func RemoveAccount(clusterName string) error {
credential, err := GetCredentials()
if err != nil {
return err
}
index := 0
isFound := false
for i, v := range credential.Accounts {
accountName := strings.Split(v.ID, "--")[0]
if accountName == clusterName {
isFound = true
index = i
credential.SelectedAccount = ""
}
}
if !isFound {
return nil
}
copy(credential.Accounts[index:], credential.Accounts[index+1:])
credential.Accounts[len(credential.Accounts)-1] = nil
credential.Accounts = credential.Accounts[:len(credential.Accounts)-1]
if err := GenerateAccountsFile(credential); err != nil {
return err
}
return nil
}
// ChangeSelectedAccount change selected account according to cluster name provided
func ChangeSelectedAccount(clusterName string) error {
credential, err := GetCredentials()
if err != nil {
return err
}
credential.SelectedAccount = ""
for _, v := range credential.Accounts {
// With version (v0.19.0) account id has a clusterName prefix separated by -- (default--someId)
if v.ID == "" {
// this condition occurs when space cli is logged in with a remote server
continue
}
if v.ID == clusterName {
credential.SelectedAccount = v.ID
break
}
}
if credential.SelectedAccount == "" {
return fmt.Errorf("no account found in account.yaml")
}
if err := GenerateAccountsFile(credential); err != nil {
return err
}
return nil
}
//GetSCImageName get the sc image name and add the image prefix when required
func GetSCImageName(imagePrefix, version string, t model.ImageType) string {
if !strings.HasSuffix(imagePrefix, "/") {
imagePrefix += "/"
}
return fmt.Sprintf("%s%s:%s", imagePrefix, t, version)
}
// GetNetworkName provides network name of particular cluster
func GetNetworkName(clusterName string) string {
if clusterName == "default" {
return "space-cloud"
}
return fmt.Sprintf("space-cloud-%s", clusterName)
}
// GetScContainers provides name for space-cloud containers
func GetScContainers(clusterName, name string) string {
if clusterName == "default" {
return fmt.Sprintf("space-cloud-%s", name)
}
return fmt.Sprintf("space-cloud-%s-%s", clusterName, name)
}
// GetDatabaseContainerName provides name for database container
func GetDatabaseContainerName(id, alias string) string {
if id == "default" {
return fmt.Sprintf("space-cloud--addon--db--%s", alias)
}
return fmt.Sprintf("space-cloud-%s--addon--db--%s", id, alias)
}
// GetRegistryContainerName provides name for registry container
func GetRegistryContainerName(clusterName string) string {
if clusterName == "default" {
return "space-cloud--addon--registry"
}
return fmt.Sprintf("space-cloud-%s--addon--registry", clusterName)
}