diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dd1d12f..52d479f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,7 +33,7 @@ jobs: name: "Run all tests" strategy: matrix: - go-version: [1.14.x] + go-version: [1.23.x] platform: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/.vscode/launch.json b/.vscode/launch.json index e01278d..b4d059d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,7 +12,7 @@ "mode": "auto", "program": "${workspaceFolder}", "env": {}, - "args": ["log", "-j","TEST-01", "-t", "30m"] + "args": ["log", "TEST-01", "-t", "30m"] }, { "name": "jtl push -p", diff --git a/cmd/internal/config/config.go b/cmd/internal/config/config.go index da64e19..f0a0c12 100644 --- a/cmd/internal/config/config.go +++ b/cmd/internal/config/config.go @@ -36,7 +36,7 @@ func SetDataFilePath(p string) { const ( DefaultDateTimePattern = "02 Jan 2006 15:04" DefaultDatePattern = "02 Jan 2006" - DataFileHeader = "id,date,activity,hours,jira" + DataFileHeader = "id,date,activity,hours" ) func Init() { @@ -61,7 +61,7 @@ func Init() { viper.SetConfigName(configName) viper.SetConfigType(configType) - viper.Set("dataFileHeader", "id,date,activity,hours,jira") + viper.Set("dataFileHeader", "id,date,activity,hours") viper.SetDefault("host", "") viper.SetDefault("credentials", map[string]string{ "username": "", diff --git a/cmd/internal/csv/csv.go b/cmd/internal/csv/csv.go index e168107..d955e6d 100644 --- a/cmd/internal/csv/csv.go +++ b/cmd/internal/csv/csv.go @@ -126,7 +126,7 @@ func (f *CsvFile) AddRecord(rec CsvRec) { // UpdateRecord replaces record at the given index with the new record. func (f *CsvFile) UpdateRecord(idx int, rec CsvRec) error { if idx < 0 || idx > len(f.Records) { - return errors.New("ndex is out of range") + return errors.New("index is out of range") } rec._idx = idx f.Records[idx] = rec diff --git a/cmd/internal/csv/csv_test.go b/cmd/internal/csv/csv_test.go index 6c799b9..1d785a9 100644 --- a/cmd/internal/csv/csv_test.go +++ b/cmd/internal/csv/csv_test.go @@ -55,7 +55,7 @@ func TestCsvFile_Write(t *testing.T) { writtenFile := CsvFile{ Path: path, Header: GetCsvHeader(), - Records: CsvRecords{newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "US demo", "10m", "TICKET-1", "jira"})}, + Records: CsvRecords{newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "US demo", "10m", "TICKET-1"})}, } writtenFile.Write() @@ -80,8 +80,8 @@ func TestCsvFile_ReadAll(t *testing.T) { Path: "./csv_testdata/not_empty.csv", Header: GetCsvHeader(), Records: CsvRecords{ - newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "Row with ID", "10m", "TICKET-1", "jira"}), - newCsvRecord(1, []string{"", "15 Apr 2020 11:30", "Row without ID", "10m", "TICKET-2", "jira"}), + newCsvRecord(0, []string{"1", "14 Apr 2020 11:30", "Row with ID", "10m", "TICKET-1"}), + newCsvRecord(1, []string{"", "15 Apr 2020 11:30", "Row without ID", "10m", "TICKET-2"}), }, }, rowsExpected: 2, @@ -165,7 +165,7 @@ func TestUpdateRecord(t *testing.T) { expectedError error }{ {"Should update record at the given index", updatedRec._idx, updatedRec, nil}, - {"Should return error if the given index is out of range", idxOutOfBounds, okCsvRecord, errors.New("Index is out of range")}, + {"Should return error if the given index is out of range", idxOutOfBounds, okCsvRecord, errors.New("index is out of range")}, } for _, test := range tests { diff --git a/cmd/internal/model/jira.go b/cmd/internal/model/jira.go index 86c9f91..6b4d334 100644 --- a/cmd/internal/model/jira.go +++ b/cmd/internal/model/jira.go @@ -23,7 +23,7 @@ type JiraRequest []JiraRequestRow // NewJiraRequest creates JiraRequest from CsvRecords func NewJiraRequest(recs *csv.CsvRecords) JiraRequest { jr := JiraRequest{} - for _, row := range recs.Filter(func(r csv.CsvRec) bool { return r.IsPushed() }) { + for _, row := range recs.Filter(func(r csv.CsvRec) bool { return !r.IsPushed() }) { //Rows with IDs are pushed, don't them into request req := JiraRequestRow{ _rowIdx: row.GetIdx(), diff --git a/cmd/log.go b/cmd/log.go index 4f73846..d13e2ee 100644 --- a/cmd/log.go +++ b/cmd/log.go @@ -15,6 +15,9 @@ package cmd import ( + "errors" + "log" + "slices" "time" "github.com/philgal/jtl/cmd/internal/config" @@ -45,7 +48,7 @@ Examples: `, Run: func(cmd *cobra.Command, args []string) { //use App for testing - runLogCommand(cmd) + runLogCommand(cmd, args) displayReport() }, } @@ -59,17 +62,25 @@ const ( func init() { rootCmd.AddCommand(logCmd) - logCmd.Flags().StringP(ticketCmdStr, "j", "", "[Required] Jira ticket. Ticket aliases can be used. See > jtl help log") - logCmd.MarkFlagRequired(ticketCmdStr) - //todo improve duration parsing + //todo improve duration parsing logCmd.Flags().StringP(timeCmdStr, "t", "4h", "[Required] Time spent. Default - 4h") - logCmd.Flags().StringP(messageCmdStr, "m", "", "Comment to the work log. Will be displayed in Jira. Default - empty") + logCmd.Flags().StringP(messageCmdStr, "m", "wip", "Comment to the work log. Will be displayed in Jira. Default - \"wip\"") logCmd.Flags().StringP(dateCmdStr, "d", time.Now().Format(config.DefaultDateTimePattern), "Date and time when the work has been started. Default - current timestamp") } -func runLogCommand(cmd *cobra.Command) { +func runLogCommand(cmd *cobra.Command, args []string) { + + if len(args) < 1 { + log.Fatal("too few arguments") + } + var ticket, timeSpent, date, comment string - ticket, _ = cmd.Flags().GetString(ticketCmdStr) + + ticket = args[0] + if len(ticket) < 5 { + log.Fatalf("invalid ticket %s", ticket) + } + timeSpent, _ = cmd.Flags().GetString(timeCmdStr) comment, _ = cmd.Flags().GetString(messageCmdStr) date, _ = cmd.Flags().GetString(dateCmdStr) @@ -79,10 +90,18 @@ func runLogCommand(cmd *cobra.Command) { // total: 4h before pause, 4h after // traverse from most recent record // sum hours logged in the same day as `date` - // case =0: log 4h from 8:45, 4h from 13:00 + // case =0: log 4h from 8:45, 4h from 13:00 // case >0: - + // calculate how much is left of the date + slices.SortFunc(fcsv.Records, func(a csv.CsvRec, b csv.CsvRec) int { + atime, aerr := time.Parse(config.DefaultDateTimePattern, a.StartedTs) + btime, berr := time.Parse(config.DefaultDateTimePattern, b.StartedTs) + if err := errors.Join(aerr, berr); err != nil { + log.Fatal("cannot compare log records: ", err) + } + return atime.Compare(btime) + }) fcsv.AddRecord(csv.CsvRec{ ID: "", @@ -91,5 +110,7 @@ func runLogCommand(cmd *cobra.Command) { TimeSpent: timeSpent, Ticket: ticket, }) + + log.Print(fcsv.Records) fcsv.Write() }