mirror of
https://github.com/on4kjm/FLEcli.git
synced 2025-01-31 06:51:02 +01:00
WWFF ADIF output
This commit is contained in:
parent
cf653c36fc
commit
55e96d88f2
5 changed files with 66 additions and 14 deletions
25
cmd/adif.go
25
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()
|
||||
if isLoadedOK {
|
||||
//check if we have the necessary information for the type
|
||||
|
||||
writeAdif(verifiedOutputFilename, loadedLogFile)
|
||||
//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
|
||||
}
|
||||
}
|
||||
|
||||
outputAdif(verifiedOutputFilename, loadedLogFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue