Skip to content

Commit

Permalink
Avoid ugly Kptfile indentation on get/update
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Jul 30, 2020
1 parent ea678d3 commit 755b0b8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
50 changes: 25 additions & 25 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func TestKptGetSet(t *testing.T) {
replacements: map[string][]string{
"deploy.yaml": {"replicas: 5", "replicas: 7"},
"Kptfile": {
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
},
},
},
Expand All @@ -61,21 +61,21 @@ func TestKptGetSet(t *testing.T) {
replacements: map[string][]string{
"deploy.yaml": {"replicas: 5", "replicas: 7"},
"Kptfile": {
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
},
},
},
{name: "tag", tag: "v0.1.0", dataset: testutil.HelloWorldSet,
replacements: map[string][]string{
"deploy.yaml": {"replicas: 5", "replicas: 7"},
"Kptfile": {
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
},
},
},
Expand All @@ -84,10 +84,10 @@ func TestKptGetSet(t *testing.T) {
replacements: map[string][]string{
"deploy.yaml": {"replicas: 5", "replicas: 7"},
"Kptfile": {
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
},
},
},
Expand All @@ -96,11 +96,11 @@ func TestKptGetSet(t *testing.T) {
replacements: map[string][]string{
"deploy.yaml": {"replicas: 5", "replicas: 7"},
"Kptfile": {
` setBy: package-default
value: "5"`,
` setBy: foo
value: "7"
isSet: true`,
` setBy: package-default
value: "5"`,
` setBy: foo
value: "7"
isSet: true`,
},
},
},
Expand All @@ -116,10 +116,10 @@ func TestKptGetSet(t *testing.T) {
` app: hello
foo: bar`},
"Kptfile": {
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
` setBy: package-default
value: "5"`,
` value: "7"
isSet: true`,
},
},
},
Expand Down
14 changes: 13 additions & 1 deletion internal/kptfile/kptfileutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,20 @@ func WriteFile(dir string, k kptfile.KptFile) error {
return err
}

// convert to rNode and back to string to make indentation consistent
// with rest of the yaml serialization to avoid unwanted diffs
rNode, err := yaml.Parse(string(b))
if err != nil {
return err
}

kptFileStr, err := rNode.String()
if err != nil {
return err
}

// fyi: perm is ignored if the file already exists
return ioutil.WriteFile(filepath.Join(dir, kptfile.KptFileName), b, 0600)
return ioutil.WriteFile(filepath.Join(dir, kptfile.KptFileName), []byte(kptFileStr), 0600)
}

// ReadFileStrict reads a Kptfile for a package and validates that it contains required
Expand Down
8 changes: 1 addition & 7 deletions internal/util/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,5 @@ func (c *Command) upsertKptfile(spec *git.RepoSpec) error {
Git: c.Git,
}
kpgfile.Upstream.Git.Commit = commit

// update the KptFile with the new values
contents, err := yaml.Marshal(kpgfile)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(c.Destination, kptfile.KptFileName), contents, 0600)
return kptfileutil.WriteFile(c.Destination, kpgfile)
}

0 comments on commit 755b0b8

Please sign in to comment.