-
Notifications
You must be signed in to change notification settings - Fork 63
/
use.go
173 lines (156 loc) · 4.53 KB
/
use.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gmeghnag/omc/cmd/helpers"
"github.com/gmeghnag/omc/types"
"github.com/gmeghnag/omc/vars"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func useContext(path string, omcConfigFile string, idFlag string) {
//if path != "" {
// if !filepath.IsAbs(path) {
// fmt.Println("error: \"" + path + "\" is not an absolute path.")
// os.Exit(1)
// }
//}
// read json omcConfigFile
if path != "" {
_path, err := findMustGatherIn(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
l := strings.Split(_path, "/")
path = strings.Join(l[0:(len(l)-1)], "/")
path = strings.TrimSuffix(path, "/")
}
file, _ := ioutil.ReadFile(omcConfigFile)
omcConfigJson := types.Config{}
_ = json.Unmarshal([]byte(file), &omcConfigJson)
config := types.Config{}
var contexts []types.Context
var NewContexts []types.Context
contexts = omcConfigJson.Contexts
var found bool
var ctxId string
for _, c := range contexts {
if c.Id == idFlag || c.Path == path {
NewContexts = append(NewContexts, types.Context{Id: c.Id, Path: c.Path, Current: "*", Project: c.Project})
found = true
} else {
NewContexts = append(NewContexts, types.Context{Id: c.Id, Path: c.Path, Current: "", Project: c.Project})
}
}
if !found {
if idFlag != "" {
NewContexts = append(NewContexts, types.Context{Id: idFlag, Path: path, Current: "*", Project: "default"})
} else {
ctxId = helpers.RandString(8)
NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: "default"})
}
}
config.Contexts = NewContexts
config.Id = idFlag
if !found {
if idFlag != "" {
config.Id = idFlag
} else {
config.Id = ctxId
}
}
file, _ = json.MarshalIndent(config, "", " ")
_ = ioutil.WriteFile(omcConfigFile, file, 0644)
}
func findMustGatherIn(path string) (string, error) {
numDirs := 0
dirName := ""
retPath := strings.TrimSuffix(path, "/")
var retErr error
timeStampFound := false
namespacesFolderFound := false
files, err := ioutil.ReadDir(path)
if err != nil {
return "", err
}
for _, file := range files {
if file.IsDir() {
dirName = file.Name()
numDirs = numDirs + 1
if file.Name() == "namespaces" {
namespacesFolderFound = true
}
}
if !file.IsDir() && file.Name() == "timestamp" {
timeStampFound = true
}
}
if namespacesFolderFound {
return retPath + "/", retErr
}
if timeStampFound && (numDirs > 1 || numDirs == 0) {
return path, fmt.Errorf("Expected one directory in path: \"%s\", found: %s.", path, strconv.Itoa(numDirs))
}
if !timeStampFound && numDirs == 1 {
retPath, retErr = findMustGatherIn(path + "/" + dirName)
}
return retPath + "/", retErr
}
// useCmd represents the use command
var UseCmd = &cobra.Command{
Use: "use",
Short: "Select the must-gather to use",
Long: `
Select the must-gather to use.
If the must-gather does not exists it will be added as default to the managed must-gahters.
Use the command 'omc get mg' to see them all.`,
Run: func(cmd *cobra.Command, args []string) {
idFlag, _ := cmd.Flags().GetString("id")
path := ""
if len(args) == 0 && idFlag == "" {
fmt.Printf("must-gather: \"%s\"\nnamespace: \"%s\"\n", vars.MustGatherRootPath, vars.Namespace)
os.Exit(0)
}
if len(args) > 1 {
fmt.Println("Expect one arguemnt, found: ", len(args))
os.Exit(1)
}
if len(args) == 1 {
path = args[0]
if strings.HasSuffix(path, "/") {
path = strings.TrimRight(path, "/")
}
if strings.HasSuffix(path, "\\") {
path = strings.TrimRight(path, "\\")
}
path, _ = filepath.Abs(path)
isDir, _ := helpers.IsDirectory(path)
if !isDir {
fmt.Println("Error: " + path + " is not a directory.")
os.Exit(1)
}
}
useContext(path, viper.ConfigFileUsed(), idFlag)
},
}
func init() {
UseCmd.Flags().StringVarP(&vars.Id, "id", "i", "", "Id string for the must-gather to use. If two must-gather has the same id the first one will be used.")
}