11package main
22
33import (
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-
6034func 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