-
Notifications
You must be signed in to change notification settings - Fork 214
/
utils.go
128 lines (127 loc) · 3.39 KB
/
utils.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
package main
// func createDirIfNotExist(dir string) error {
// if _, err := os.Stat(dir); os.IsNotExist(err) {
// err = os.MkdirAll(dir, 0755)
// if err != nil {
// return err
// }
// }
// return nil
// }
//
// func getSpaceCloudDirectory() string {
// return fmt.Sprintf("%s/.space-cloud", getHomeDirectory())
// }
//
// func getHomeDirectory() string {
// if runtime.GOOS == "windows" {
// home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
// if home == "" {
// home = os.Getenv("USERPROFILE")
// }
// return home
// }
// return os.Getenv("HOME")
// }
//
// func getSpaceCLIDirectory() string {
// return fmt.Sprintf("%s/cli", getSpaceCloudDirectory())
// }
//
// // createFileIfNotExist creates a file with the provided content if it doesn't already exists
// func createFileIfNotExist(path, content string) error {
// if _, err := os.Stat(path); os.IsNotExist(err) {
// return ioutil.WriteFile(path, []byte(content), 0755)
// }
// return nil
// }
//
// func getSpaceCLIConfigPath() string {
// return fmt.Sprintf("%s/config.json", getSpaceCLIDirectory())
// }
//
// // getLatestVersion retrieves the latest Space Cloud version based on the current version
// func getLatestVersion() (*cliVersionDoc, error) {
// // Create a db object
// db := api.New("spacecloud", "localhost:4122", false).DB("db")
//
// // Create a context
// ctx := context.Background()
//
// var result *spaceApiTypes.Response
// var err error
// result, err = db.Get("cli_version").Sort("-version_code").Limit(1).Apply(ctx)
// if err != nil {
// return nil, err
// }
// r := make([]*cliVersionDoc, 0)
// if err := result.Unmarshal(&r); err != nil {
// return nil, err
// }
// doc := new(cliVersionDoc)
// for _, val := range r {
// if val.VersionCode > doc.VersionCode {
// doc.VersionNo = val.VersionNo
// doc.VersionCode = val.VersionCode
// doc.ID = val.ID
// }
// }
// return doc, nil
// }
//
// func readVersionConfig() (*cliVersionDoc, error) {
// file, err := ioutil.ReadFile(fmt.Sprintf("%s/config.json", getSpaceCLIDirectory()))
// if err != nil {
// return nil, err
// }
//
// doc := new(cliVersionDoc)
// err = json.Unmarshal([]byte(file), doc)
// return doc, err
// }
//
// func downloadPlugin(latestVersion *cliVersionDoc) error {
//
// url := fmt.Sprintf("http://localhost:5000/cmd_%s.so", latestVersion.VersionNo)
// filepath := fmt.Sprintf("%s/cmd_%s.so", getSpaceCLIDirectory(), latestVersion.VersionNo)
//
// // Create the file
// out, err := os.Create(filepath)
// if err != nil {
// return err
// }
// defer func() { _ = out.Close() }()
//
// // Get the data
// resp, err := http.Get(url)
// if err != nil {
// return err
// }
// defer func() { _ = resp.Body.Close() }()
//
// // Write the body to file
// _, err = io.Copy(out, resp.Body)
// if err != nil {
// return err
// }
// docs := &cliVersionDoc{
// VersionNo: latestVersion.VersionNo,
// VersionCode: latestVersion.VersionCode,
// ID: latestVersion.ID,
// }
// file, _ := json.Marshal(docs)
// err = ioutil.WriteFile(fmt.Sprintf("%s/config.json", getSpaceCLIDirectory()), file, 0644)
// return err
// }
//
// func logError(message string, err error) error {
// // Log with error if provided
// if err != nil {
// logrus.WithField("error", err.Error()).Errorln(message)
// } else {
// logrus.Errorln(message)
// }
//
// // Return the error message
// return errors.New(message)
// }