implement default output filename construction

pull/2/head
Jean-Marc MEESSEN 4 years ago
parent 9c903913ac
commit 2dbb376b35

@ -25,6 +25,7 @@ import (
var outputFilename string var outputFilename string
var isWwff bool var isWwff bool
var isOverwrite bool
// adifCmd is executed when choosing the adif option (load and generate adif file) // adifCmd is executed when choosing the adif option (load and generate adif file)
var adifCmd = &cobra.Command{ var adifCmd = &cobra.Command{
@ -42,17 +43,19 @@ func init() {
rootCmd.AddCommand(adifCmd) rootCmd.AddCommand(adifCmd)
adifCmd.PersistentFlags().BoolVarP(&isWwff,"wwff", "w", false, "Generates an ADIF file ready to be uploaded on WWFF") adifCmd.PersistentFlags().BoolVarP(&isWwff,"wwff", "w", false, "Generates an ADIF file ready to be uploaded on WWFF")
adifCmd.PersistentFlags().BoolVarP(&isOverwrite,"overWrite", "", false, "Overwrites the output file if it exisits")
adifCmd.PersistentFlags().StringVarP(&outputFilename, "output", "o", "", "Output filename") adifCmd.PersistentFlags().StringVarP(&outputFilename, "output", "o", "", "Output filename")
} }
func processAdifCommand(){ func processAdifCommand(){
verifiedOutputFilename, wasOK := buildOutputFilename(outputFilename,inputFilename,false) verifiedOutputFilename, wasOK := buildOutputFilename(outputFilename,inputFilename,isOverwrite)
fmt.Println("adif called") fmt.Println("adif called")
fmt.Println("Inputfile: ", inputFilename) fmt.Println("Inputfile: ", inputFilename)
fmt.Println("OutputFile: ", outputFilename) fmt.Println("OutputFile: ", outputFilename)
fmt.Println("computed output: ", verifiedOutputFilename) fmt.Println("computed output: ", verifiedOutputFilename)
fmt.Println("Output wasOK: ", wasOK) fmt.Println("Output wasOK: ", wasOK)
fmt.Println("wwff: ", isWwff) fmt.Println("wwff: ", isWwff)
fmt.Println("isOverwrite: ", isOverwrite)
//loadFile() //loadFile()
} }

@ -20,6 +20,7 @@ import (
"os" "os"
//"log" //"log"
"fmt" "fmt"
"path/filepath"
) )
// does the target file exist? // does the target file exist?
@ -35,6 +36,14 @@ func buildOutputFilename(output string, input string, overwrite bool) (outputFil
return "", false return "", false
} }
//No output was provided, let's create one from the input file
if output == "" {
extension := filepath.Ext(input)
outputRootPart := input[0:len(input)-len(extension)]
output = outputRootPart + ".adi"
fmt.Println("No output provided, defaulting to \""+output+ "\"" )
}
//an output was provided by the user //an output was provided by the user
if output != "" { if output != "" {
info, err := os.Stat(output) info, err := os.Stat(output)
@ -53,9 +62,6 @@ func buildOutputFilename(output string, input string, overwrite bool) (outputFil
return "", false return "", false
} }
//if output is not null, check if file exits
//if it exists, check the flag
//if output
return outputFilename, true return outputFilename, true
} }

@ -53,10 +53,25 @@ func Test_buildOutputFilename(t *testing.T) {
"", false, "", false,
}, },
{ {
"Output exisit but no overwrite", "Output exist but no overwrite",
args{input: "a file", output: testFile, overwrite: false}, args{input: "a file", output: testFile, overwrite: false},
"", false, "", false,
}, },
{
"no output, input provided with extention",
args{input: "/test/data/file.txt", output: "", overwrite: false},
"/test/data/file.adi", true,
},
{
"no output, input provided without extention",
args{input: "/test/data/file", output: "", overwrite: false},
"/test/data/file.adi", true,
},
{
"no output, input provided, enfing with a point",
args{input: "/test/data/file.", output: "", overwrite: false},
"/test/data/file.adi", true,
},
} }
teardownTestCase := setupTestCase(t) teardownTestCase := setupTestCase(t)

@ -66,8 +66,8 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.FLEcli.yaml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.FLEcli.yaml)")
rootCmd.PersistentFlags().StringVarP(&inputFilename, "input", "i", "", "FLE formatted input file") rootCmd.PersistentFlags().StringVarP(&inputFilename, "input", "i", "", "FLE formatted input file (mandatory)")
rootCmd.MarkFlagRequired("input") rootCmd.MarkPersistentFlagRequired("input")
} }
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.

Loading…
Cancel
Save