WWFF ADIF output

pull/2/head
Jean-Marc MEESSEN 4 years ago
parent cf653c36fc
commit 55e96d88f2

@ -49,22 +49,35 @@ func init() {
func processAdifCommand() { func processAdifCommand() {
verifiedOutputFilename, wasOK := buildOutputFilename(outputFilename, inputFilename, isOverwrite) verifiedOutputFilename, filenameWasOK := 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 filenameWasOK: ", filenameWasOK)
fmt.Println("wwff: ", isWwff) fmt.Println("wwff: ", isWwff)
fmt.Println("isOverwrite: ", isOverwrite) fmt.Println("isOverwrite: ", isOverwrite)
// if the output file could not be parsed correctly do noting // if the output file could not be parsed correctly do noting
if wasOK { if filenameWasOK {
loadedLogFile, isLoadedOK := loadFile() loadedLogFile, isLoadedOK := loadFile()
//TODO: move this in a function so that it can be more easily tested
if isLoadedOK { if isLoadedOK {
if len(loadedLogFile) == 0 {
fmt.Println("No useful data read. Aborting...")
return
}
//check if we have the necessary information for the type //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)
} }
} }
} }

@ -58,9 +58,11 @@ func buildOutputFilename(output string, input string, overwrite bool) (outputFil
if overwrite { if overwrite {
//user accepted to overwrite the file //user accepted to overwrite the file
return output, true return output, true
} } else {
fmt.Println("File already exists. Use --overwrite flag if necessary.")
return "", false return "", false
} }
}
return outputFilename, true return outputFilename, true
} }

@ -1,7 +1,9 @@
package cmd package cmd
import ( import (
"bufio"
"fmt" "fmt"
"os"
"strings" "strings"
) )
@ -21,12 +23,17 @@ See the License for the specific language governing permissions and
limitations under the License. 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 //convert the log data to an in-memory ADIF file
// TODO: write the array list to 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) { func buildAdif(fullLog []LogLine) (adifList []string) {
//Print the fixed header //Print the fixed header
adifList = append(adifList, "ADIF Export for Fast Log Entry by DF3CB") adifList = append(adifList, "ADIF Export for Fast Log Entry by DF3CB")
@ -63,6 +70,37 @@ func buildAdif(fullLog []LogLine) (adifList []string) {
return adifList 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) { func adifElement(elementName, elementValue string) (element string) {
return fmt.Sprintf("<%s:%d>%s ", strings.ToUpper(elementName), len(elementValue), elementValue) 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)
}
}

@ -35,7 +35,6 @@ import (
var cfgFile string var cfgFile string
var inputFilename string var inputFilename string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "FLEcli", Use: "FLEcli",

Loading…
Cancel
Save