Skip to content

Commit 43c3f40

Browse files
committed
feat: introduce --output option for "fix" cmd
- introduced --output/-o (mut.exclusive with --overwrite) - add more examples - add more integration tests Fixes #6456. Signed-off-by: Ahmet Alp Balkan <[email protected]>
1 parent a9d7152 commit 43c3f40

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

cmd/skaffold/app/cmd/fix.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,41 @@ import (
3232
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml"
3333
)
3434

35-
var toVersion string
35+
var (
36+
toVersion string
37+
fixOutputPath string
38+
)
3639

3740
func NewCmdFix() *cobra.Command {
3841
return NewCmd("fix").
3942
WithDescription("Update old configuration to a newer schema version").
4043
WithExample("Update \"skaffold.yaml\" in the current folder to the latest version", "fix").
4144
WithExample("Update \"skaffold.yaml\" in the current folder to version \"skaffold/v1\"", "fix --version skaffold/v1").
45+
WithExample("Update \"skaffold.yaml\" in the current folder in-place", "fix --overwrite").
46+
WithExample("Update \"skaffold.yaml\" and write the output to a new file", "fix --output skaffold.new.yaml").
4247
WithCommonFlags().
4348
WithFlags([]*Flag{
4449
{Value: &overwrite, Name: "overwrite", DefValue: false, Usage: "Overwrite original config with fixed config"},
4550
{Value: &toVersion, Name: "version", DefValue: latestV1.Version, Usage: "Target schema version to upgrade to"},
51+
{Value: &fixOutputPath, Name: "output", Shorthand: "o", DefValue: "", Usage: "File to write the changed config (instead of standard output)"},
4652
}).
4753
NoArgs(doFix)
4854
}
4955

5056
func doFix(_ context.Context, out io.Writer) error {
51-
return fix(out, opts.ConfigurationFile, toVersion, overwrite)
57+
if overwrite && fixOutputPath != "" {
58+
return fmt.Errorf("--overwrite and --output/-o cannot be used together")
59+
}
60+
var toFile string
61+
if fixOutputPath != "" {
62+
toFile = fixOutputPath
63+
} else if overwrite {
64+
toFile = opts.ConfigurationFile
65+
}
66+
return fix(out, opts.ConfigurationFile, toFile, toVersion)
5267
}
5368

54-
func fix(out io.Writer, configFile string, toVersion string, overwrite bool) error {
69+
func fix(out io.Writer, configFile, outFile string, toVersion string) error {
5570
parsedCfgs, err := schema.ParseConfig(configFile)
5671
if err != nil {
5772
return err
@@ -97,11 +112,11 @@ func fix(out io.Writer, configFile string, toVersion string, overwrite bool) err
97112
if err != nil {
98113
return fmt.Errorf("marshaling new config: %w", err)
99114
}
100-
if overwrite {
101-
if err := ioutil.WriteFile(configFile, newCfg, 0644); err != nil {
115+
if outFile != "" {
116+
if err := ioutil.WriteFile(outFile, newCfg, 0644); err != nil {
102117
return fmt.Errorf("writing config file: %w", err)
103118
}
104-
output.Default.Fprintf(out, "New config at version %s generated and written to %s\n", toVersion, opts.ConfigurationFile)
119+
output.Default.Fprintf(out, "New config at version %s generated and written to %s\n", toVersion, outFile)
105120
} else {
106121
out.Write(newCfg)
107122
}

cmd/skaffold/app/cmd/fix_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ build:
158158
cfgFile := t.TempFile("config", []byte(test.inputYaml))
159159

160160
var b bytes.Buffer
161-
err := fix(&b, cfgFile, test.targetVersion, false)
161+
err := fix(&b, cfgFile, "", test.targetVersion)
162162

163163
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.output, b.String())
164164
})
165165
}
166166
}
167167

168-
func TestFixOverwrite(t *testing.T) {
168+
func TestFixToFileOverwrite(t *testing.T) {
169169
inputYaml := `apiVersion: skaffold/v1alpha4
170170
kind: Config
171171
build:
@@ -203,7 +203,7 @@ deploy:
203203
cfgFile := t.TempFile("config", []byte(inputYaml))
204204

205205
var b bytes.Buffer
206-
err := fix(&b, cfgFile, latestV1.Version, true)
206+
err := fix(&b, cfgFile, cfgFile, latestV1.Version)
207207

208208
output, _ := ioutil.ReadFile(cfgFile)
209209

integration/fix_test.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,45 @@ limitations under the License.
1717
package integration
1818

1919
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
2023
"testing"
2124

2225
"github.com/GoogleContainerTools/skaffold/integration/skaffold"
26+
"github.com/GoogleContainerTools/skaffold/testutil"
27+
yaml "gopkg.in/yaml.v2"
2328
)
2429

25-
func TestFix(t *testing.T) {
30+
func TestFixExclusiveOptions(t *testing.T) {
2631
MarkIntegrationTest(t, CanRunWithoutGcp)
2732

33+
out := skaffold.Fix().InDir("testdata/fix").RunOrFailOutput(t)
34+
out, err := skaffold.Fix("--overwrite", "--output=ignored").WithConfig("-").InDir("testdata/fix").
35+
WithStdin(out).RunWithCombinedOutput(t)
36+
testutil.CheckError(t, true, err)
37+
testutil.CheckContains(t, "cannot be used together", string(out))
38+
}
39+
40+
func TestFixStdout(t *testing.T) {
41+
MarkIntegrationTest(t, CanRunWithoutGcp)
2842
ns, _ := SetupNamespace(t)
2943

3044
out := skaffold.Fix().InDir("testdata/fix").RunOrFailOutput(t)
31-
3245
skaffold.Run().WithConfig("-").InDir("testdata/fix").InNs(ns.Name).WithStdin(out).RunOrFail(t)
3346
}
47+
48+
func TestFixOutputFile(t *testing.T) {
49+
MarkIntegrationTest(t, CanRunWithoutGcp)
50+
51+
out := skaffold.Fix("--output", filepath.Join("updated.yaml")).InDir("testdata/fix").RunOrFailOutput(t)
52+
testutil.CheckContains(t, "written to updated.yaml", string(out))
53+
defer os.Remove(filepath.Join("testdata", "fix", "updated.yaml"))
54+
55+
f, err := ioutil.ReadFile(filepath.Join("testdata", "fix", "updated.yaml"))
56+
testutil.CheckError(t, false, err)
57+
58+
parsed := make(map[string]interface{})
59+
err = yaml.UnmarshalStrict(f, parsed)
60+
testutil.CheckError(t, false, err)
61+
}

0 commit comments

Comments
 (0)