mirror of https://github.com/on4kjm/FLEcli.git
parent
b3c169228f
commit
7d903fb18c
@ -0,0 +1,70 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
|
||||||
|
|
||||||
|
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 (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// outputAdif generates and writes data in ADIF format
|
||||||
|
func outputCsv(outputFile string, fullLog []LogLine) {
|
||||||
|
|
||||||
|
//TODO: validate input data for combination
|
||||||
|
|
||||||
|
//convert the log data to an in-memory ADIF file
|
||||||
|
csvData := buildCsv(fullLog)
|
||||||
|
|
||||||
|
//write to a file (re-using function defined to write adif file)
|
||||||
|
writeFile(outputFile, csvData)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildAdif creates the adif file in memory ready to be printed
|
||||||
|
func buildCsv(fullLog []LogLine) (csvList []string) {
|
||||||
|
|
||||||
|
// V2,ON4KJM/P,ON/ON-001,24/05/20,1310,14MHz,CW,S57LC
|
||||||
|
|
||||||
|
for _, logLine := range fullLog {
|
||||||
|
var csvLine strings.Builder
|
||||||
|
csvLine.WriteString("V2,")
|
||||||
|
csvLine.WriteString(fmt.Sprintf("%s,", logLine.MyCall))
|
||||||
|
csvLine.WriteString(fmt.Sprintf("%s,", logLine.MySOTA))
|
||||||
|
csvLine.WriteString(fmt.Sprintf("%s,", csvDate(logLine.Date)))
|
||||||
|
csvLine.WriteString(fmt.Sprintf("%s,", logLine.Time))
|
||||||
|
// adifLine.WriteString(adifElement("BAND", logLine.Band))
|
||||||
|
csvLine.WriteString(fmt.Sprintf("%s,",logLine.Mode))
|
||||||
|
csvLine.WriteString(fmt.Sprintf("%s", logLine.Call))
|
||||||
|
|
||||||
|
csvList = append(csvList, csvLine.String())
|
||||||
|
}
|
||||||
|
return csvList
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//adifDate converts a date in YYYY-MM-DD format to YYYYMMDD
|
||||||
|
func csvDate(inputDate string) (outputDate string) {
|
||||||
|
const FLEdateFormat = "2006-01-02"
|
||||||
|
date, err := time.Parse(FLEdateFormat, inputDate)
|
||||||
|
//error should never happen
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const CSVdateFormat = "02/01/06"
|
||||||
|
return date.Format(CSVdateFormat)
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_csvDate(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
inputDate string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wantOutputDate string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"Happy case",
|
||||||
|
args{inputDate: "2020-07-13"},
|
||||||
|
"13/07/20",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if gotOutputDate := csvDate(tt.args.inputDate); gotOutputDate != tt.wantOutputDate {
|
||||||
|
t.Errorf("csvDate() = %v, want %v", gotOutputDate, tt.wantOutputDate)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_buildCsv(t *testing.T) {
|
||||||
|
sampleFilledLog1 := []LogLine{
|
||||||
|
{MyCall: "ON4KJM/P", Call: "S57LC", Date: "2020-05-24", Time: "1310", Band: "20m", Frequency: "14.045", Mode: "CW", RSTsent: "599", RSTrcvd: "599", MySOTA: "ON/ON-001", Operator: "ON4KJM", Nickname: "ONFF-0259-1"},
|
||||||
|
{MyCall: "ON4KJM/P", Call: "ON4LY", Date: "2020-05-24", Time: "1312", Band: "20m", Mode: "CW", RSTsent: "559", RSTrcvd: "599", MySOTA: "ON/ON-001", Operator: "ON4KJM"},
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedOutput1 := []string{
|
||||||
|
"V2,ON4KJM/P,ON/ON-001,24/05/20,1310,14MHz,CW,S57LC",
|
||||||
|
"V2,ON4KJM/P,ON/ON-001,24/05/20,1312,14MHz,CW,ON4LY",
|
||||||
|
}
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
fullLog []LogLine
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wantCsvList []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"Happy case",
|
||||||
|
args{fullLog: sampleFilledLog1},
|
||||||
|
expectedOutput1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if gotCsvList := buildCsv(tt.args.fullLog); !reflect.DeepEqual(gotCsvList, tt.wantCsvList) {
|
||||||
|
t.Errorf("buildCsv() = %v, want %v", gotCsvList, tt.wantCsvList)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue