diff --git a/cmd/adif.go b/cmd/adif.go index 2164a57..db5a099 100644 --- a/cmd/adif.go +++ b/cmd/adif.go @@ -49,22 +49,35 @@ func init() { func processAdifCommand() { - verifiedOutputFilename, wasOK := buildOutputFilename(outputFilename, inputFilename, isOverwrite) + verifiedOutputFilename, filenameWasOK := buildOutputFilename(outputFilename, inputFilename, isOverwrite) fmt.Println("adif called") fmt.Println("Inputfile: ", inputFilename) fmt.Println("OutputFile: ", outputFilename) fmt.Println("computed output: ", verifiedOutputFilename) - fmt.Println("Output wasOK: ", wasOK) + fmt.Println("Output filenameWasOK: ", filenameWasOK) fmt.Println("wwff: ", isWwff) fmt.Println("isOverwrite: ", isOverwrite) // if the output file could not be parsed correctly do noting - if wasOK { + if filenameWasOK { loadedLogFile, isLoadedOK := loadFile() + + //TODO: move this in a function so that it can be more easily tested if isLoadedOK { + if len(loadedLogFile) == 0 { + fmt.Println("No useful data read. Aborting...") + return + } + //check if we have the necessary information for the type + if isWwff { + if loadedLogFile[0].MyWWFF == "" { + fmt.Println("Missing MY-WWFF reference. Aborting...") + return + } + } - writeAdif(verifiedOutputFilename, loadedLogFile) + outputAdif(verifiedOutputFilename, loadedLogFile) } } } diff --git a/cmd/adif_filename.go b/cmd/adif_filename.go index 9095c69..1c1cb41 100644 --- a/cmd/adif_filename.go +++ b/cmd/adif_filename.go @@ -39,9 +39,9 @@ func buildOutputFilename(output string, input string, overwrite bool) (outputFil //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)] + outputRootPart := input[0 : len(input)-len(extension)] output = outputRootPart + ".adi" - fmt.Println("No output provided, defaulting to \""+output+ "\"" ) + fmt.Println("No output provided, defaulting to \"" + output + "\"") } //an output was provided by the user @@ -58,8 +58,10 @@ func buildOutputFilename(output string, input string, overwrite bool) (outputFil if overwrite { //user accepted to overwrite the file return output, true + } else { + fmt.Println("File already exists. Use --overwrite flag if necessary.") + return "", false } - return "", false } return outputFilename, true diff --git a/cmd/adif_write.go b/cmd/adif_write.go index ebb564f..45fbbf9 100644 --- a/cmd/adif_write.go +++ b/cmd/adif_write.go @@ -1,7 +1,9 @@ package cmd import ( + "bufio" "fmt" + "os" "strings" ) @@ -21,12 +23,17 @@ See the License for the specific language governing permissions and limitations under the License. */ -func writeAdif(outputFile string, fullLog []LogLine) { +// outputAdif generates and writes data in ADIF format +func outputAdif(outputFile string, fullLog []LogLine) { - // TODO: create an array of strings first - // TODO: write the array list to file + //convert the log data to an in-memory ADIF file + adifData := buildAdif(fullLog) + + //write to a file + writeAdif(outputFile, adifData) } +// buildAdif creates the adif file in memory ready to be printed func buildAdif(fullLog []LogLine) (adifList []string) { //Print the fixed header adifList = append(adifList, "ADIF Export for Fast Log Entry by DF3CB") @@ -63,6 +70,37 @@ func buildAdif(fullLog []LogLine) (adifList []string) { return adifList } +// writeAdif writes the in-memory adif data to a file +func writeAdif(outputFile string, adifData []string) { + + //TODO: check access rights + f, err := os.Create(outputFile) + checkFileError(err) + + defer f.Close() + + w := bufio.NewWriter(f) + + lineCount := 0 + for _, adifLine := range adifData { + _, err := w.WriteString(adifLine + "\n") + checkFileError(err) + + w.Flush() + checkFileError(err) + lineCount++ + } + fmt.Printf("\nSuccessfully wrote %d lines to file \"%s\"", lineCount, outputFile) +} + +// adifElement generated the ADIF sub-element func adifElement(elementName, elementValue string) (element string) { return fmt.Sprintf("<%s:%d>%s ", strings.ToUpper(elementName), len(elementValue), elementValue) } + +// checkFileError handles file related errors +func checkFileError(e error) { + if e != nil { + panic(e) + } +} diff --git a/cmd/load.go b/cmd/load.go index f071b4b..f052375 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -275,7 +275,7 @@ func loadFile() (filleFullLog []LogLine, isProcessedOK bool) { fmt.Println(errorLogLine) } isProcessedOK = false - }else { + } else { fmt.Println("\nSuccesfuly parsed ", lineCount, " lines.") isProcessedOK = true } diff --git a/cmd/root.go b/cmd/root.go index 865cff9..ee43147 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,7 +35,6 @@ import ( var cfgFile string var inputFilename string - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "FLEcli",