@@ -32,26 +32,41 @@ import (
32
32
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml"
33
33
)
34
34
35
- var toVersion string
35
+ var (
36
+ toVersion string
37
+ fixOutputPath string
38
+ )
36
39
37
40
func NewCmdFix () * cobra.Command {
38
41
return NewCmd ("fix" ).
39
42
WithDescription ("Update old configuration to a newer schema version" ).
40
43
WithExample ("Update \" skaffold.yaml\" in the current folder to the latest version" , "fix" ).
41
44
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" ).
42
47
WithCommonFlags ().
43
48
WithFlags ([]* Flag {
44
49
{Value : & overwrite , Name : "overwrite" , DefValue : false , Usage : "Overwrite original config with fixed config" },
45
50
{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)" },
46
52
}).
47
53
NoArgs (doFix )
48
54
}
49
55
50
56
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 )
52
67
}
53
68
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 {
55
70
parsedCfgs , err := schema .ParseConfig (configFile )
56
71
if err != nil {
57
72
return err
@@ -97,11 +112,11 @@ func fix(out io.Writer, configFile string, toVersion string, overwrite bool) err
97
112
if err != nil {
98
113
return fmt .Errorf ("marshaling new config: %w" , err )
99
114
}
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 {
102
117
return fmt .Errorf ("writing config file: %w" , err )
103
118
}
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 )
105
120
} else {
106
121
out .Write (newCfg )
107
122
}
0 commit comments