-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow time tracking via the CLI
- Loading branch information
Showing
19 changed files
with
586 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ( | ||
errCouldntUnmarshalToJSON = errors.New("couldn't unmarshal data to JSON") | ||
errCouldntFetchDataFromDB = errors.New("couldn't fetch data from hours' DB") | ||
errCouldntUpdateDataInDB = errors.New("couldn't update data in hours' DB") | ||
errCouldntParseTaskID = errors.New("couldn't parse the argument for task ID as an integer") | ||
errTaskAlreadyBeingTracked = errors.New("task is already being tracked") | ||
errCouldntParseBeginTS = errors.New("couldn't parse begin timestamp") | ||
errCouldntParseEndTS = errors.New("couldn't parse end timestamp") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
pers "github.com/dhth/hours/internal/persistence" | ||
) | ||
|
||
const ( | ||
tasksLimit = 500 | ||
) | ||
|
||
func renderTasks(db *sql.DB, writer io.Writer, limit uint) error { | ||
limitToUse := limit | ||
if limit > tasksLimit { | ||
limitToUse = tasksLimit | ||
} | ||
|
||
tasks, err := pers.FetchTasks(db, true, limitToUse) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(tasks) == 0 { | ||
return nil | ||
} | ||
|
||
result, err := json.MarshalIndent(tasks, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", errCouldntUnmarshalToJSON, err.Error()) | ||
} | ||
|
||
fmt.Fprintln(writer, string(result)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package cmd | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
pers "github.com/dhth/hours/internal/persistence" | ||
) | ||
|
||
func renderActiveTLDetails(db *sql.DB, writer io.Writer) error { | ||
details, err := pers.FetchActiveTaskDetails(db) | ||
if errors.Is(err, pers.ErrNoTaskActive) { | ||
return err | ||
} else if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntFetchDataFromDB, err) | ||
} | ||
|
||
result, err := json.MarshalIndent(details, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", errCouldntUnmarshalToJSON, err.Error()) | ||
} | ||
|
||
fmt.Fprintln(writer, string(result)) | ||
|
||
return nil | ||
} | ||
|
||
func startTracking(db *sql.DB, writer io.Writer, taskID int, comment *string) error { | ||
details, err := pers.FetchActiveTaskDetails(db) | ||
var noTaskActive bool | ||
if errors.Is(err, pers.ErrNoTaskActive) { | ||
noTaskActive = true | ||
} else if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntFetchDataFromDB, err) | ||
} | ||
|
||
now := time.Now() | ||
switch noTaskActive { | ||
case true: | ||
_, err := pers.InsertNewTL(db, taskID, now, comment) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntUpdateDataInDB, err) | ||
} | ||
case false: | ||
if details.TaskID == taskID { | ||
return errTaskAlreadyBeingTracked | ||
} | ||
_, err := pers.QuickSwitchActiveTL(db, taskID, now, comment) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntUpdateDataInDB, err) | ||
} | ||
} | ||
|
||
return renderActiveTLDetails(db, writer) | ||
} | ||
|
||
func updateTracking(db *sql.DB, writer io.Writer, beginTS time.Time, comment *string) error { | ||
_, err := pers.FetchActiveTaskDetails(db) | ||
if errors.Is(err, pers.ErrNoTaskActive) { | ||
return err | ||
} else if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntFetchDataFromDB, err) | ||
} | ||
|
||
err = pers.EditActiveTL(db, beginTS, comment) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntUpdateDataInDB, err) | ||
} | ||
|
||
return renderActiveTLDetails(db, writer) | ||
} | ||
|
||
func stopTracking(db *sql.DB, writer io.Writer, beginTS, endTS *time.Time, comment *string) error { | ||
details, err := pers.FetchActiveTaskDetails(db) | ||
if errors.Is(err, pers.ErrNoTaskActive) { | ||
return err | ||
} else if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntFetchDataFromDB, err) | ||
} | ||
|
||
var bTS time.Time | ||
if beginTS == nil { | ||
bTS = details.CurrentLogBeginTS | ||
} else { | ||
bTS = *beginTS | ||
} | ||
|
||
var eTS time.Time | ||
if endTS == nil { | ||
eTS = time.Now() | ||
} else { | ||
eTS = *endTS | ||
} | ||
|
||
var commentToUse *string | ||
if comment == nil { | ||
commentToUse = details.CurrentLogComment | ||
} else { | ||
commentToUse = comment | ||
} | ||
|
||
err = pers.FinishActiveTL(db, details.TaskID, bTS, eTS, commentToUse) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntUpdateDataInDB, err) | ||
} | ||
|
||
tlDetails, err := pers.FetchTLByID(db, details.TLID) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", errCouldntFetchDataFromDB, err) | ||
} | ||
|
||
result, err := json.MarshalIndent(tlDetails, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", errCouldntUnmarshalToJSON, err.Error()) | ||
} | ||
|
||
fmt.Fprintln(writer, string(result)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.