From bcdae469792155b65469857f922fc46a01c61acb Mon Sep 17 00:00:00 2001 From: Jean-Marc MEESSEN Date: Sat, 27 Jun 2020 22:53:23 +0200 Subject: [PATCH] start output file processing --- cmd/adif.go | 6 ++-- cmd/adif_filename.go | 70 ++++++++++++++++++++++++++++++++++++ cmd/adif_filename_test.go | 76 +++++++++++++++++++++++++++++++++++++++ cmd/adif_write.go | 21 +++++++++++ cmd/load.go | 6 ++-- 5 files changed, 172 insertions(+), 7 deletions(-) create mode 100644 cmd/adif_filename.go create mode 100644 cmd/adif_filename_test.go create mode 100644 cmd/adif_write.go diff --git a/cmd/adif.go b/cmd/adif.go index 517cd6b..6fe1a46 100644 --- a/cmd/adif.go +++ b/cmd/adif.go @@ -17,11 +17,9 @@ limitations under the License. */ import ( - "fmt" "github.com/spf13/cobra" -// "log" - + // "log" //"strings" ) @@ -34,7 +32,7 @@ var adifCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { fmt.Println("adif called") - fmt.Println("Inputfile: ",inputFilename) + fmt.Println("Inputfile: ", inputFilename) loadFile() }, } diff --git a/cmd/adif_filename.go b/cmd/adif_filename.go new file mode 100644 index 0000000..49eb06b --- /dev/null +++ b/cmd/adif_filename.go @@ -0,0 +1,70 @@ +package cmd + +/* +Copyright © 2020 Jean-Marc Meessen, ON4KJM + +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. +*/ + +import ( + "os" + //"log" + "fmt" +) + +// does the target file exist? +// is the file defined +// remove the extention + +//returning "" is considered as invalid +func buildOutputFilename(output string, input string, overwrite bool) (outputFilename string, wasOK bool) { + outputFilename = "" + + //validate that input is populated (should never happen if properly called) + if input == "" { + return "", false + } + + //an output was provided by the user + if output != "" { + info, err := os.Stat(output) + if os.IsNotExist(err) { + return output, true + } + //It exisits but is a directory + if info.IsDir() { + fmt.Println("Error: specified output exists and is a directory") + return "", false + } + if overwrite { + //user accepted to overwrite the file + return output, true + } + return "", false + } + + //if output is not null, check if file exits + //if it exists, check the flag + //if output + return outputFilename, true +} + +// fileExists checks if a file exists and is not a directory before we +// try using it to prevent further errors. +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/cmd/adif_filename_test.go b/cmd/adif_filename_test.go new file mode 100644 index 0000000..582f269 --- /dev/null +++ b/cmd/adif_filename_test.go @@ -0,0 +1,76 @@ +package cmd + +import ( + "os" + "testing" +) + +const testDir string = "test_dir" +const testFile string = "test.adi" + +func setupTestCase(t *testing.T) func(t *testing.T) { + t.Log("setup test case") + //create test directory + os.Mkdir(testDir, os.FileMode(0522)) + //create test file + f, _ := os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) + defer f.Close() + + return func(t *testing.T) { + t.Log("teardown test case") + //delete test directory + os.Remove(testDir) + //detete test file + os.Remove(testFile) + } +} + +func Test_buildOutputFilename(t *testing.T) { + type args struct { + output string + input string + overwrite bool + } + tests := []struct { + name string + args args + wantOutputFilename string + wantWasOK bool + }{ + { + "input file not provided", + args{input: "", output: "xxx", overwrite: false}, + "", false, + }, + { + "Output file does not exist", + args{input: "a file", output: "output.adi", overwrite: false}, + "output.adi", true, + }, + { + "Output name is a directory", + args{input: "a file", output: testDir, overwrite: false}, + "", false, + }, + { + "Output exisit but no overwrite", + args{input: "a file", output: testFile, overwrite: false}, + "", false, + }, + } + + teardownTestCase := setupTestCase(t) + defer teardownTestCase(t) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotOutputFilename, gotWasOK := buildOutputFilename(tt.args.output, tt.args.input, tt.args.overwrite) + if gotOutputFilename != tt.wantOutputFilename { + t.Errorf("buildOutputFilename() gotOutputFilename = %v, want %v", gotOutputFilename, tt.wantOutputFilename) + } + if gotWasOK != tt.wantWasOK { + t.Errorf("buildOutputFilename() gotWasOK = %v, want %v", gotWasOK, tt.wantWasOK) + } + }) + } +} diff --git a/cmd/adif_write.go b/cmd/adif_write.go new file mode 100644 index 0000000..68fcd39 --- /dev/null +++ b/cmd/adif_write.go @@ -0,0 +1,21 @@ +package cmd + +/* +Copyright © 2020 Jean-Marc Meessen, ON4KJM + +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. +*/ + +// does the target file exist? +// is the file defined +// remove the extention diff --git a/cmd/load.go b/cmd/load.go index b472575..5ba2200 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -72,9 +72,9 @@ func loadFile() { txtlines = append(txtlines, scanner.Text()) } - if error := scanner.Err(); error != nil { - log.Fatal(error) - } + if error := scanner.Err(); error != nil { + log.Fatal(error) + } file.Close()