diff --git a/cmd/skaffold/app/flags/filepath.go b/cmd/skaffold/app/flags/filepath.go index f26619d7726..e101f023520 100644 --- a/cmd/skaffold/app/flags/filepath.go +++ b/cmd/skaffold/app/flags/filepath.go @@ -17,94 +17,38 @@ limitations under the License. package flags import ( - "fmt" "os" ) -// InputFilepath represents a input file command line argument -// InputFilepath type makes sure the file exists. -type InputFilepath filepathFlag - -// OutputFilepath represents a output file command line argument. -// OutputFilepath make sure the path can be written to. -type OutputFilepath filepathFlag - -func NewInputFilepathFlag(value string) *InputFilepath { - return &InputFilepath{ - path: value, - shouldExist: true, - } -} - -func (f *InputFilepath) Usage() string { - return "Path to a input file." -} - -func (f *InputFilepath) Type() string { - return fmt.Sprintf("%T", f) -} - -func (f *InputFilepath) Set(value string) error { - return nil -} - -func (f *InputFilepath) String() string { - return f.path -} - -func NewOutputFilepathFlag(value string) *OutputFilepath { - return &OutputFilepath{ - path: value, - canWrite: true, - } -} - -func (f *OutputFilepath) Type() string { - return fmt.Sprintf("%T", f) -} - -func (f *OutputFilepath) Usage() string { - return "Path to an output filepath." -} - -func (f *OutputFilepath) Set(value string) error { - return nil -} - -func (f *OutputFilepath) String() string { - return f.path -} - type filepathFlag struct { path string shouldExist bool - canWrite bool } -func (f *filepathFlag) Set(value string) error { - //if +func (f *filepathFlag) SetIfValid(value string) error { + copy := f + copy.path = value + if err := copy.isValid(); err != nil { + return err + } + f = copy return nil } -func newFilepathFlag(value string, exists bool, canWrite bool) *filepathFlag { - return &filepathFlag{ +func newFilepathFlag(value string, exists bool) filepathFlag { + return filepathFlag{ path: value, shouldExist: exists, - canWrite: canWrite, } } func (f filepathFlag) isValid() error { - if f.shouldExist { - if _, err := os.Stat(f.path); os.IsNotExist(err) { - return err - } - } else if f.canWrite { - _, err := os.Create(f.path) - defer os.Remove(f.path) - if err != nil { - return err - } + if !f.shouldExist { + // Currently no validation implemented for output files. + return nil + } + if _, err := os.Stat(f.path); os.IsNotExist(err) { + return err } return nil } diff --git a/cmd/skaffold/app/flags/filepath_test.go b/cmd/skaffold/app/flags/filepath_test.go deleted file mode 100644 index 8d12596cf02..00000000000 --- a/cmd/skaffold/app/flags/filepath_test.go +++ /dev/null @@ -1,51 +0,0 @@ -/* -Copyright 2019 The Skaffold Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package flags - -import ( - "testing" -) - -func TestNewInputFile(t *testing.T) { - - flag := NewInputFilepath("test.out") -} - -func TestInputFileFlagSet(t *testing.T) { - flag := NewInputFilepath("test.out") - if err := flag.Set(rawTemplate); err != nil { - t.Errorf("Error setting flag value: %s", err) - } - - if err := flag.Set("{{start}} bad template"); err == nil { - t.Errorf("Expected error setting flag but got none.") - } -} - -func TestTemplateString(t *testing.T) { - flag := NewTemplateFlag(rawTemplate, nil) - if rawTemplate != flag.String() { - t.Errorf("Flag String() does not match. Expected %s, Actual %s", rawTemplate, flag.String()) - } -} - -func TestTemplateType(t *testing.T) { - flag := &TemplateFlag{} - if flag.Type() != expectedFlagType { - t.Errorf("Flag returned wrong type. Expected %s, Actual %s", expectedFlagType, flag.Type()) - } -} diff --git a/cmd/skaffold/app/flags/inputfilepath.go b/cmd/skaffold/app/flags/inputfilepath.go new file mode 100644 index 00000000000..9619f3e2a0e --- /dev/null +++ b/cmd/skaffold/app/flags/inputfilepath.go @@ -0,0 +1,51 @@ +/* +Copyright 2019 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "fmt" +) + +// InputFilepath represents a input file command line argument +// InputFilepath type makes sure the file exists. +type InputFilepath struct { + filepathFlag +} + +func NewInputFilepath(value string) *InputFilepath { + return &InputFilepath{ + filepathFlag{ + path: value, + shouldExist: true, + }} +} + +func (f *InputFilepath) Usage() string { + return "Path to an input file." +} + +func (f *InputFilepath) Type() string { + return fmt.Sprintf("%T", f) +} + +func (f *InputFilepath) Set(value string) error { + return f.SetIfValid(value) +} + +func (f *InputFilepath) String() string { + return f.path +} diff --git a/cmd/skaffold/app/flags/inputfilepath_test.go b/cmd/skaffold/app/flags/inputfilepath_test.go new file mode 100644 index 00000000000..c0ea7c04d0d --- /dev/null +++ b/cmd/skaffold/app/flags/inputfilepath_test.go @@ -0,0 +1,72 @@ +/* +Copyright 2019 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "testing" + + "github.com/GoogleContainerTools/skaffold/testutil" +) + +func TestNewInputFile(t *testing.T) { + flag := NewInputFilepath("test.in") + expectedFlag := InputFilepath{filepathFlag{ + path: "test.in", + shouldExist: true, + }} + if *flag != expectedFlag { + t.Errorf("expected %s, actual %s", &expectedFlag, flag) + } +} + +func TestInputFileFlagSet(t *testing.T) { + dir, cleanUp := testutil.NewTempDir(t) + defer cleanUp() + filename := "exists.in" + dir.Write(filename, "some input") + + var tests = []struct { + description string + filename string + shouldErr bool + }{ + { + description: "should not error when file is present", + filename: dir.Path(filename), + }, + { + description: "should error when file is present", + filename: "does_not_exist.in", + shouldErr: true, + }, + } + + for _, test := range tests { + flag := NewInputFilepath("") + err := flag.Set(test.filename) + expectedFlag := NewInputFilepath(test.filename) + testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, expectedFlag.String(), flag.String()) + } +} + +func TestInputFilepathType(t *testing.T) { + flag := NewInputFilepath("test.in") + expectedFlagType := "*flags.InputFilepath" + if flag.Type() != expectedFlagType { + t.Errorf("Flag returned wrong type. Expected %s, Actual %s", expectedFlagType, flag.Type()) + } +} diff --git a/cmd/skaffold/app/flags/outputfilepath.go b/cmd/skaffold/app/flags/outputfilepath.go new file mode 100644 index 00000000000..ab4290d0719 --- /dev/null +++ b/cmd/skaffold/app/flags/outputfilepath.go @@ -0,0 +1,49 @@ +/* +Copyright 2019 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "fmt" +) + +// OutputFilepath represents a output file command line argument. +// OutputFilepath currently does not provide any validation. +type OutputFilepath struct { + filepathFlag +} + +func NewOutputFilepath(value string) *OutputFilepath { + return &OutputFilepath{filepathFlag{ + path: value, + }} +} + +func (f *OutputFilepath) Type() string { + return fmt.Sprintf("%T", f) +} + +func (f *OutputFilepath) Usage() string { + return "Path to an output filepath." +} + +func (f *OutputFilepath) Set(value string) error { + return f.SetIfValid(value) +} + +func (f *OutputFilepath) String() string { + return f.path +} diff --git a/cmd/skaffold/app/flags/outputfilepath_test.go b/cmd/skaffold/app/flags/outputfilepath_test.go new file mode 100644 index 00000000000..c232b0f1351 --- /dev/null +++ b/cmd/skaffold/app/flags/outputfilepath_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2019 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "testing" +) + +func TestNewOutputFilepathType(t *testing.T) { + flag := NewOutputFilepath("test.out") + expectedFlag := OutputFilepath{filepathFlag{ + path: "test.out", + }} + if *flag != expectedFlag { + t.Errorf("expected %s, actual %s", &expectedFlag, flag) + } +} + +func TestOutputFileFlagSet(t *testing.T) { + flag := NewOutputFilepath("") + if err := flag.Set("test.out"); err != nil { + t.Errorf("Error setting flag value: %s", err) + } +}