diff --git a/cmd/adif_filename.go b/cmd/adif_filename.go index 1c1cb41..4c1f803 100644 --- a/cmd/adif_filename.go +++ b/cmd/adif_filename.go @@ -48,6 +48,7 @@ func buildOutputFilename(output string, input string, overwrite bool) (outputFil if output != "" { info, err := os.Stat(output) if os.IsNotExist(err) { + //File doesn't exist, so we're good return output, true } //It exisits but is a directory @@ -58,10 +59,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 } + + fmt.Println("File already exists. Use --overwrite flag if necessary.") + return "", false } return outputFilename, true diff --git a/cmd/adif_write.go b/cmd/adif_write.go index 45fbbf9..e7cd50c 100644 --- a/cmd/adif_write.go +++ b/cmd/adif_write.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strings" + "time" ) /* @@ -44,9 +45,8 @@ func buildAdif(fullLog []LogLine) (adifList []string) { for _, logLine := range fullLog { adifLine := "" adifLine = adifLine + adifElement("STATION_CALLSIGN", logLine.MyCall) - adifLine = adifLine + adifElement("CALL", logLine.Call) - //TODO: strip the delimiters of the date - adifLine = adifLine + adifElement("QSO_DATE", logLine.Date) + adifLine = adifLine + adifElement("CALL", logLine.Call) + adifLine = adifLine + adifElement("QSO_DATE", adifDate(logLine.Date)) adifLine = adifLine + adifElement("TIME_ON", logLine.Time) adifLine = adifLine + adifElement("BAND", logLine.Band) adifLine = adifLine + adifElement("MODE", logLine.Mode) @@ -104,3 +104,16 @@ func checkFileError(e error) { panic(e) } } + +//adifDate converts a date in YYYY-MM-DD format to YYYYMMDD +func adifDate(inputDate string) (outputDate string) { + const RFC3339FullDate = "2006-01-02" + date, err := time.Parse(RFC3339FullDate, inputDate) + //error should never happen + if err != nil { + panic(err) + } + outputDate = fmt.Sprintf("%04d%02d%02d", date.Year(), date.Month(), date.Day()) + + return outputDate +} diff --git a/cmd/adif_write_test.go b/cmd/adif_write_test.go index cd8d37b..cbab728 100644 --- a/cmd/adif_write_test.go +++ b/cmd/adif_write_test.go @@ -37,8 +37,8 @@ func Test_adifElement(t *testing.T) { func Test_buildAdif(t *testing.T) { sampleFilledLog1 := []LogLine{ - {MyCall: "ON4KJM/P", Call: "S57LC", Date: "20200524", Time: "1310", Band: "20m", Frequency: "14.045", Mode: "CW", RSTsent: "599", RSTrcvd: "599", MyWWFF: "ONFF-0259", Operator: "ON4KJM", Nickname: "ONFF-0259-1"}, - {MyCall: "ON4KJM/P", Call: "ON4LY", Date: "20200524", Time: "1312", Band: "20m", Mode: "CW", RSTsent: "559", RSTrcvd: "599", MyWWFF: "ONFF-0259", Operator: "ON4KJM"}, + {MyCall: "ON4KJM/P", Call: "S57LC", Date: "2020-05-24", Time: "1310", Band: "20m", Frequency: "14.045", Mode: "CW", RSTsent: "599", RSTrcvd: "599", MyWWFF: "ONFF-0259", 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", MyWWFF: "ONFF-0259", Operator: "ON4KJM"}, } expectedOutput1 := []string{ @@ -72,3 +72,33 @@ func Test_buildAdif(t *testing.T) { }) } } + +func Test_adifDate(t *testing.T) { + type args struct { + inputDate string + } + tests := []struct { + name string + args args + wantOutputDate string + }{ + { + "Happy case", + args{inputDate: "2020-06-13"}, + "20200613", + }, + //Panics as expected but I don't know how to test this. + // { + // "Bad format", + // args{inputDate: "2020-13-06"}, + // "20200613", + // }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if gotOutputDate := adifDate(tt.args.inputDate); gotOutputDate != tt.wantOutputDate { + t.Errorf("adifDate() = %v, want %v", gotOutputDate, tt.wantOutputDate) + } + }) + } +}