-
Notifications
You must be signed in to change notification settings - Fork 785
/
cli.go
157 lines (139 loc) · 3.69 KB
/
cli.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ksync
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/jenkins-x/jx/v2/pkg/packages"
"github.com/jenkins-x/jx/v2/pkg/util"
)
var commit string
var binary = "ksync"
// CLI implements the command ksync commands contained in KSyncer.
type CLI struct {
Runner util.Commander
}
// NewCLI creates a new KsyncCLI instance configured to use the provided ksync CLI in the given current working directory
func NewCLI() (*CLI, error) {
path, err := util.JXBinLocation()
if err != nil {
return nil, err
}
runner := &util.Command{
Name: fmt.Sprintf("%s/ksync", path),
}
cli := &CLI{
Runner: runner,
}
return cli, nil
}
// isInstalled checks if the binary is installed
func isInstalled(binary string) (bool, error) {
flag, err := packages.ShouldInstallBinary(binary)
return flag, err
}
// getTag returns the latest full tag from github given the owner and repo, and installs
func getTag(owner, repo string) (string, string, error) {
latestTag, err := util.GetLatestFullTagFromGithub(owner, repo)
if err != nil {
return "", "", err
}
latestVersion := *latestTag.Name
// This will return the shortsha, the first 7 characters associated with the git commit
latestSha := (*latestTag.Commit.SHA)[:7]
return latestSha, latestVersion, nil
}
// install downloads and installs the ksync package
func install(latestVersion, binDir, fileName string) error {
clientURL := fmt.Sprintf("https://github.com/ksync/ksync/releases/download/%s/ksync_%s_%s", latestVersion, runtime.GOOS, runtime.GOARCH)
if runtime.GOOS == "windows" {
clientURL += ".exe"
}
fullPath := filepath.Join(binDir, fileName)
tmpFile := fullPath + ".tmp"
err := packages.DownloadFile(clientURL, tmpFile)
if err != nil {
return err
}
err = util.RenameFile(tmpFile, fullPath)
if err != nil {
return err
}
err = os.Chmod(fullPath, 0755)
if err != nil {
return err
}
return nil
}
// Version prints out the version of the local component of ksync
func (kcli *CLI) Version() (string, error) {
args := []string{"version"}
kcli.Runner.SetArgs(args)
out, _ := kcli.Runner.RunWithoutRetry()
return out, nil
}
// Init installs the server side component of ksync - the daemonsets
func (kcli *CLI) Init(flags ...string) (string, error) {
args := []string{"init"}
args = append(args, flags...)
kcli.Runner.SetArgs(args)
out, err := kcli.Runner.RunWithoutRetry()
if err != nil {
return "", err
}
return out, nil
}
// Clean removes the ksync pods
func (kcli *CLI) Clean() (string, error) {
args := []string{"clean"}
kcli.Runner.SetArgs(args)
out, err := kcli.Runner.RunWithoutRetry()
if err != nil {
return "", err
}
return out, nil
}
// InstallKSync installs ksync, it returns the sha of the latest commit
func InstallKSync() (string, error) {
binDir, err := util.JXBinLocation()
if err != nil {
return "", err
}
download, err := isInstalled(binary)
if err != nil || !download {
// Exec `ksync` to find the version
kcli, err := NewCLI()
if err != nil {
return "", err
}
res, err := kcli.Version()
if err != nil {
return "", err
}
commit = getCommitShaFromVersion(res)
if commit != "" {
return commit, nil
}
return "", fmt.Errorf("unable to find version of ksync")
}
latestSha, latestVersion, err := getTag("ksync", "ksync")
if err != nil {
return "", err
}
err = install(latestVersion, binDir, packages.BinaryWithExtension(binary))
if err != nil {
return "", err
}
return latestSha, nil
}
func getCommitShaFromVersion(result string) string {
lines := strings.Split(result, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "Git Commit:") {
commit = strings.TrimSpace(strings.TrimPrefix(line, "Git Commit:"))
}
}
return commit
}