Skip to content

Commit 11cdb25

Browse files
committed
Move functions that are copied from kubernetes/helm to separate file
1 parent a2fd3f6 commit 11cdb25

File tree

2 files changed

+177
-160
lines changed

2 files changed

+177
-160
lines changed

helm.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package main
2+
3+
// This file contains functions that where blatantly copied from
4+
// https://github.wdf.sap.corp/kubernetes/helm
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"io/ioutil"
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
14+
"github.com/ghodss/yaml"
15+
"google.golang.org/grpc"
16+
"k8s.io/helm/cmd/helm/helmpath"
17+
"k8s.io/helm/cmd/helm/strvals"
18+
"k8s.io/helm/pkg/downloader"
19+
)
20+
21+
/////////////// Source: cmd/helm/install.go /////////////////////////
22+
23+
type valueFiles []string
24+
25+
func (v *valueFiles) String() string {
26+
return fmt.Sprint(*v)
27+
}
28+
29+
func (v *valueFiles) Type() string {
30+
return "valueFiles"
31+
}
32+
33+
func (v *valueFiles) Set(value string) error {
34+
for _, filePath := range strings.Split(value, ",") {
35+
*v = append(*v, filePath)
36+
}
37+
return nil
38+
}
39+
40+
func locateChartPath(name, version string, verify bool, keyring string) (string, error) {
41+
name = strings.TrimSpace(name)
42+
version = strings.TrimSpace(version)
43+
if fi, err := os.Stat(name); err == nil {
44+
abs, err := filepath.Abs(name)
45+
if err != nil {
46+
return abs, err
47+
}
48+
if verify {
49+
if fi.IsDir() {
50+
return "", errors.New("cannot verify a directory")
51+
}
52+
if _, err := downloader.VerifyChart(abs, keyring); err != nil {
53+
return "", err
54+
}
55+
}
56+
return abs, nil
57+
}
58+
if filepath.IsAbs(name) || strings.HasPrefix(name, ".") {
59+
return name, fmt.Errorf("path %q not found", name)
60+
}
61+
62+
crepo := filepath.Join(helmpath.Home(homePath()).Repository(), name)
63+
if _, err := os.Stat(crepo); err == nil {
64+
return filepath.Abs(crepo)
65+
}
66+
67+
dl := downloader.ChartDownloader{
68+
HelmHome: helmpath.Home(homePath()),
69+
Out: os.Stdout,
70+
Keyring: keyring,
71+
}
72+
if verify {
73+
dl.Verify = downloader.VerifyAlways
74+
}
75+
76+
filename, _, err := dl.DownloadTo(name, version, ".")
77+
if err == nil {
78+
lname, err := filepath.Abs(filename)
79+
if err != nil {
80+
return filename, err
81+
}
82+
return lname, nil
83+
}
84+
85+
return filename, fmt.Errorf("file %q not found", name)
86+
}
87+
88+
// Merges source and destination map, preferring values from the source map
89+
func mergeValues(dest map[string]interface{}, src map[string]interface{}) map[string]interface{} {
90+
for k, v := range src {
91+
// If the key doesn't exist already, then just set the key to that value
92+
if _, exists := dest[k]; !exists {
93+
dest[k] = v
94+
continue
95+
}
96+
nextMap, ok := v.(map[string]interface{})
97+
// If it isn't another map, overwrite the value
98+
if !ok {
99+
dest[k] = v
100+
continue
101+
}
102+
// If the key doesn't exist already, then just set the key to that value
103+
if _, exists := dest[k]; !exists {
104+
dest[k] = nextMap
105+
continue
106+
}
107+
// Edge case: If the key exists in the destination, but isn't a map
108+
destMap, isMap := dest[k].(map[string]interface{})
109+
// If the source map has a map for this key, prefer it
110+
if !isMap {
111+
dest[k] = v
112+
continue
113+
}
114+
// If we got to this point, it is a map in both, so merge them
115+
dest[k] = mergeValues(destMap, nextMap)
116+
}
117+
return dest
118+
}
119+
120+
/////////////// Source: cmd/helm/upgrade.go /////////////////////////
121+
122+
func (d *diffCmd) vals() ([]byte, error) {
123+
base := map[string]interface{}{}
124+
125+
// User specified a values files via -f/--values
126+
for _, filePath := range d.valueFiles {
127+
currentMap := map[string]interface{}{}
128+
bytes, err := ioutil.ReadFile(filePath)
129+
if err != nil {
130+
return []byte{}, err
131+
}
132+
133+
if err := yaml.Unmarshal(bytes, &currentMap); err != nil {
134+
return []byte{}, fmt.Errorf("failed to parse %s: %s", filePath, err)
135+
}
136+
// Merge with the previous map
137+
base = mergeValues(base, currentMap)
138+
}
139+
140+
// User specified a value via --set
141+
for _, value := range d.values {
142+
if err := strvals.ParseInto(value, base); err != nil {
143+
return []byte{}, fmt.Errorf("failed parsing --set data: %s", err)
144+
}
145+
}
146+
147+
return yaml.Marshal(base)
148+
}
149+
150+
/////////////// Source: cmd/helm/helm.go ////////////////////////////
151+
152+
func checkArgsLength(argsReceived int, requiredArgs ...string) error {
153+
expectedNum := len(requiredArgs)
154+
if argsReceived != expectedNum {
155+
arg := "arguments"
156+
if expectedNum == 1 {
157+
arg = "argument"
158+
}
159+
return fmt.Errorf("This command needs %v %s: %s", expectedNum, arg, strings.Join(requiredArgs, ", "))
160+
}
161+
return nil
162+
}
163+
164+
func homePath() string {
165+
return os.Getenv("HELM_HOME")
166+
}
167+
168+
func prettyError(err error) error {
169+
if err == nil {
170+
return nil
171+
}
172+
// This is ridiculous. Why is 'grpc.rpcError' not exported? The least they
173+
// could do is throw an interface on the lib that would let us get back
174+
// the desc. Instead, we have to pass ALL errors through this.
175+
return errors.New(grpc.ErrorDesc(err))
176+
}

main.go

Lines changed: 1 addition & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
6-
"io/ioutil"
75
"os"
8-
"path/filepath"
9-
"strings"
106

11-
"github.com/ghodss/yaml"
127
"github.com/spf13/cobra"
13-
"google.golang.org/grpc"
14-
"k8s.io/helm/cmd/helm/helmpath"
15-
"k8s.io/helm/cmd/helm/strvals"
16-
"k8s.io/helm/pkg/downloader"
178
"k8s.io/helm/pkg/helm"
189

1910
"github.com/databus23/helm-diff/manifest"
@@ -40,23 +31,6 @@ type diffCmd struct {
4031
values []string
4132
}
4233

43-
type valueFiles []string
44-
45-
func (v *valueFiles) String() string {
46-
return fmt.Sprint(*v)
47-
}
48-
49-
func (v *valueFiles) Type() string {
50-
return "valueFiles"
51-
}
52-
53-
func (v *valueFiles) Set(value string) error {
54-
for _, filePath := range strings.Split(value, ",") {
55-
*v = append(*v, filePath)
56-
}
57-
return nil
58-
}
59-
6034
func main() {
6135

6236
diff := diffCmd{}
@@ -82,6 +56,7 @@ func main() {
8256
return diff.run()
8357
},
8458
}
59+
8560
f := cmd.Flags()
8661
f.BoolP("version", "v", false, "show version")
8762
f.VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
@@ -126,137 +101,3 @@ func (d *diffCmd) run() error {
126101

127102
return nil
128103
}
129-
130-
func (d *diffCmd) vals() ([]byte, error) {
131-
base := map[string]interface{}{}
132-
133-
// User specified a values files via -f/--values
134-
for _, filePath := range d.valueFiles {
135-
currentMap := map[string]interface{}{}
136-
bytes, err := ioutil.ReadFile(filePath)
137-
if err != nil {
138-
return []byte{}, err
139-
}
140-
141-
if err := yaml.Unmarshal(bytes, &currentMap); err != nil {
142-
return []byte{}, fmt.Errorf("failed to parse %s: %s", filePath, err)
143-
}
144-
// Merge with the previous map
145-
base = mergeValues(base, currentMap)
146-
}
147-
148-
// User specified a value via --set
149-
for _, value := range d.values {
150-
if err := strvals.ParseInto(value, base); err != nil {
151-
return []byte{}, fmt.Errorf("failed parsing --set data: %s", err)
152-
}
153-
}
154-
155-
return yaml.Marshal(base)
156-
}
157-
158-
func prettyError(err error) error {
159-
if err == nil {
160-
return nil
161-
}
162-
// This is ridiculous. Why is 'grpc.rpcError' not exported? The least they
163-
// could do is throw an interface on the lib that would let us get back
164-
// the desc. Instead, we have to pass ALL errors through this.
165-
return errors.New(grpc.ErrorDesc(err))
166-
}
167-
168-
func checkArgsLength(argsReceived int, requiredArgs ...string) error {
169-
expectedNum := len(requiredArgs)
170-
if argsReceived != expectedNum {
171-
arg := "arguments"
172-
if expectedNum == 1 {
173-
arg = "argument"
174-
}
175-
return fmt.Errorf("This command needs %v %s: %s", expectedNum, arg, strings.Join(requiredArgs, ", "))
176-
}
177-
return nil
178-
}
179-
180-
func locateChartPath(name, version string, verify bool, keyring string) (string, error) {
181-
name = strings.TrimSpace(name)
182-
version = strings.TrimSpace(version)
183-
if fi, err := os.Stat(name); err == nil {
184-
abs, err := filepath.Abs(name)
185-
if err != nil {
186-
return abs, err
187-
}
188-
if verify {
189-
if fi.IsDir() {
190-
return "", errors.New("cannot verify a directory")
191-
}
192-
if _, err := downloader.VerifyChart(abs, keyring); err != nil {
193-
return "", err
194-
}
195-
}
196-
return abs, nil
197-
}
198-
if filepath.IsAbs(name) || strings.HasPrefix(name, ".") {
199-
return name, fmt.Errorf("path %q not found", name)
200-
}
201-
202-
crepo := filepath.Join(helmpath.Home(homePath()).Repository(), name)
203-
if _, err := os.Stat(crepo); err == nil {
204-
return filepath.Abs(crepo)
205-
}
206-
207-
dl := downloader.ChartDownloader{
208-
HelmHome: helmpath.Home(homePath()),
209-
Out: os.Stdout,
210-
Keyring: keyring,
211-
}
212-
if verify {
213-
dl.Verify = downloader.VerifyAlways
214-
}
215-
216-
filename, _, err := dl.DownloadTo(name, version, ".")
217-
if err == nil {
218-
lname, err := filepath.Abs(filename)
219-
if err != nil {
220-
return filename, err
221-
}
222-
return lname, nil
223-
}
224-
225-
return filename, fmt.Errorf("file %q not found", name)
226-
}
227-
228-
func homePath() string {
229-
return os.Getenv("HELM_HOME")
230-
}
231-
232-
// Merges source and destination map, preferring values from the source map
233-
func mergeValues(dest map[string]interface{}, src map[string]interface{}) map[string]interface{} {
234-
for k, v := range src {
235-
// If the key doesn't exist already, then just set the key to that value
236-
if _, exists := dest[k]; !exists {
237-
dest[k] = v
238-
continue
239-
}
240-
nextMap, ok := v.(map[string]interface{})
241-
// If it isn't another map, overwrite the value
242-
if !ok {
243-
dest[k] = v
244-
continue
245-
}
246-
// If the key doesn't exist already, then just set the key to that value
247-
if _, exists := dest[k]; !exists {
248-
dest[k] = nextMap
249-
continue
250-
}
251-
// Edge case: If the key exists in the destination, but isn't a map
252-
destMap, isMap := dest[k].(map[string]interface{})
253-
// If the source map has a map for this key, prefer it
254-
if !isMap {
255-
dest[k] = v
256-
continue
257-
}
258-
// If we got to this point, it is a map in both, so merge them
259-
dest[k] = mergeValues(destMap, nextMap)
260-
}
261-
return dest
262-
}

0 commit comments

Comments
 (0)