This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetectChanges.go
107 lines (93 loc) · 2.27 KB
/
detectChanges.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
)
type deployment struct {
Force []string `json:"force,omitempty"`
Kustom []string `json:"kustomize,omitempty"`
}
type uniqueDir struct {
name []string
}
var outputJson []deployment
func main() {
if len(os.Args) > 1 {
var arr []string
var uniqueDirectories uniqueDir
dataJson := os.Args[1]
_ = json.Unmarshal([]byte(dataJson), &arr)
for _, path := range arr {
directory := filepath.Dir(path)
uniqueDirectories.name = append(uniqueDirectories.name, directory)
}
// Get only unique paths
uniqueDirectories.name = unique(uniqueDirectories)
// Setup Args from Paths
outputArgs, err := uniqueDirectories.setupArgs()
if err != nil {
log.Fatal(err)
}
// Setup Json Structure
//uniqueJson, err := uniqueDirectories.setupJson()
//if err != nil {
// log.Fatal(err)
//}
output, _ := json.Marshal(outputArgs)
fmt.Printf("%v", string(output))
}
}
func unique(intSlice uniqueDir) []string {
keys := make(map[string]string)
var list []string
for _, entry := range intSlice.name {
if _, value := keys[entry]; !value {
keys[entry] = entry
list = append(list, entry)
}
}
return list
}
func (paths uniqueDir) setupArgs() ([]string, error){
var localSlice []string
workDir, err := os.Getwd()
if err != nil {
return nil, err
}
for _, path := range paths.name {
kustomizeFile := filepath.Join(workDir, path, "kustomization.yaml")
if _, err = os.Stat(kustomizeFile); err == nil {
localSlice = append(localSlice, "-k " + path)
} else {
localSlice = append(localSlice, "-f " + path)
}
}
return localSlice, nil
}
// Json Setup
//func (paths uniqueDir) setupJson() ([]deployment, error) {
// var force []string
// var kustom []string
// workDir, err := os.Getwd()
// if err != nil {
// return nil, err
// }
//
// for _, path := range paths.name {
// kustomizeFile := filepath.Join(workDir, path, "kustomization.yaml")
//
// if _, err = os.Stat(kustomizeFile); err == nil {
// kustom = append(kustom, "-k " + path)
// } else {
// force = append(force, "-f " + path)
// }
// }
// outputJson = append(outputJson, deployment{Force: force, Kustom: kustom})
// return outputJson, nil
//}
//func (jsonValue deployment) testJson() (string, error) {
// if outputJson
//}