diff --git a/cmd/adif_write.go b/cmd/adif_write.go index 90802ec..52aa4f1 100644 --- a/cmd/adif_write.go +++ b/cmd/adif_write.go @@ -1,13 +1,5 @@ package cmd -import ( - "bufio" - "fmt" - "os" - "strings" - "time" -) - /* Copyright © 2020 Jean-Marc Meessen, ON4KJM @@ -24,6 +16,14 @@ See the License for the specific language governing permissions and limitations under the License. */ +import ( + "bufio" + "fmt" + "os" + "strings" + "time" +) + // outputAdif generates and writes data in ADIF format func outputAdif(outputFile string, fullLog []LogLine, isWWFF bool, isSOTA bool) { @@ -33,7 +33,7 @@ func outputAdif(outputFile string, fullLog []LogLine, isWWFF bool, isSOTA bool) adifData := buildAdif(fullLog, isWWFF, isSOTA) //write to a file - writeAdif(outputFile, adifData) + writeFile(outputFile, adifData) } // buildAdif creates the adif file in memory ready to be printed @@ -83,8 +83,8 @@ func buildAdif(fullLog []LogLine, isWWFF bool, isSOTA bool) (adifList []string) return adifList } -// writeAdif writes the in-memory adif data to a file -func writeAdif(outputFile string, adifData []string) { +// writeFile writes the in-memory data (lines) to a file +func writeFile(outputFile string, adifData []string) { //TODO: check access rights f, err := os.Create(outputFile) diff --git a/cmd/csv.go b/cmd/csv.go index 659a20d..b52ad2b 100644 --- a/cmd/csv.go +++ b/cmd/csv.go @@ -57,14 +57,14 @@ func processCsvCommand() { // if the output file could not be parsed correctly do noting 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 len(loadedLogFile) == 0 { - // fmt.Println("No useful data read. Aborting...") - // return - // } + //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 + } // //TODO: There are more tests required here // //check if we have the necessary information for the type @@ -75,7 +75,7 @@ func processCsvCommand() { // } // } - // outputAdif(verifiedOutputFilename, loadedLogFile, isWWFFcli, isSOTAcli) - // } + outputCsv(verifiedOutputFilename, loadedLogFile) + } } } diff --git a/cmd/csv_write.go b/cmd/csv_write.go new file mode 100644 index 0000000..443c04b --- /dev/null +++ b/cmd/csv_write.go @@ -0,0 +1,70 @@ +package cmd + +/* +Copyright © 2020 Jean-Marc Meessen, ON4KJM + +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) +} diff --git a/cmd/csv_write_test.go b/cmd/csv_write_test.go new file mode 100644 index 0000000..23fbc51 --- /dev/null +++ b/cmd/csv_write_test.go @@ -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) + } + }) + } +}