Skip to content

Commit

Permalink
fix: improve to-json and from-json, format with gofumpt
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Feb 4, 2021
1 parent e3dbbd5 commit 941799b
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 36 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func paths(project string) (string, string, error) {
var home = gap.NewScope(gap.User, "tasktimer")
home := gap.NewScope(gap.User, "tasktimer")

logfile, err := home.LogPath(project + ".log")
if err != nil {
Expand All @@ -32,7 +32,7 @@ func setup(project string) (*badger.DB, io.Closer, error) {
return nil, nil, err
}

if err := os.MkdirAll(filepath.Dir(logfile), 0754); err != nil {
if err := os.MkdirAll(filepath.Dir(logfile), 0o754); err != nil {
return nil, nil, err
}

Expand All @@ -42,7 +42,7 @@ func setup(project string) (*badger.DB, io.Closer, error) {
}

// TODO: maybe sync writes?
var options = badger.DefaultOptions(dbfile).
options := badger.DefaultOptions(dbfile).
WithLogger(badgerStdLoggerAdapter{}).
WithLoggingLevel(badger.ERROR)
db, err := badger.Open(options)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type completionCmd struct {
}

func newCompletionCmd() *completionCmd {
var cmd = &cobra.Command{
cmd := &cobra.Command{
Use: "completion [bash|zsh|fish]",
Short: "Print shell autocompletion scripts for tt",
Long: `To load completions:
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/fromjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type fromJSONCmd struct {
}

func newFromJSONCmd() *fromJSONCmd {
var cmd = &cobra.Command{
cmd := &cobra.Command{
Use: "from-json",
Short: "Imports a JSON into a project - WARNING: it will wipe the project first, use with care!",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var project = cmd.Parent().Flag("project").Value.String()
project := cmd.Parent().Flag("project").Value.String()
db, f, err := setup(project)
if err != nil {
return err
Expand All @@ -34,7 +34,7 @@ func newFromJSONCmd() *fromJSONCmd {
return fmt.Errorf("failed to read %s: %w", args[0], err)
}

var tasks []model.Task
var tasks []model.ExportedTask
if err := json.Unmarshal(input, &tasks); err != nil {
return fmt.Errorf("input json is not in the correct format: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ type pathsCmd struct {
}

func newPathsCmd() *pathsCmd {
var cmd = &cobra.Command{
cmd := &cobra.Command{
Use: "paths",
Short: "Print the paths being used for logs, data et al",
RunE: func(cmd *cobra.Command, args []string) error {
var project = cmd.Parent().Flag("project").Value.String()
project := cmd.Parent().Flag("project").Value.String()
logfile, dbfile, err := paths(project)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type reportCmd struct {
}

func newRerportCmd() *reportCmd {
var cmd = &cobra.Command{
cmd := &cobra.Command{
Use: "report",
Aliases: []string{"r"},
Short: "Print a markdown report of the given project to STDOUT",
RunE: func(cmd *cobra.Command, args []string) error {
var project = cmd.Parent().Flag("project").Value.String()
project := cmd.Parent().Flag("project").Value.String()
db, f, err := setup(project)
if err != nil {
return err
Expand All @@ -34,7 +34,7 @@ func newRerportCmd() *reportCmd {
return err
}

var md = buf.String()
md := buf.String()

if isatty.IsTerminal(os.Stdout.Fd()) {
rendered, err := glamour.RenderWithEnvironmentConfig(md)
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func (c rootCmd) Execute(args []string) {
}

func newRootCmd(version string, exit func(int)) *rootCmd {
var root = &rootCmd{
root := &rootCmd{
exit: exit,
}
var cmd = &cobra.Command{
cmd := &cobra.Command{
Use: "tt",
Short: "Task Timer (tt) is a dead simple TUI task timer",
Version: version,
Expand All @@ -40,7 +40,7 @@ func newRootCmd(version string, exit func(int)) *rootCmd {
defer db.Close()
defer f.Close()

var p = tea.NewProgram(ui.Init(db, root.project))
p := tea.NewProgram(ui.Init(db, root.project))
p.EnterAltScreen()
defer p.ExitAltScreen()
return p.Start()
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/tojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type toJSONCmd struct {
}

func newToJSONCmd() *toJSONCmd {
var cmd = &cobra.Command{
cmd := &cobra.Command{
Use: "to-json",
Short: "Exports the database as JSON",
RunE: func(cmd *cobra.Command, args []string) error {
var project = cmd.Parent().Flag("project").Value.String()
project := cmd.Parent().Flag("project").Value.String()
db, f, err := setup(project)
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ func (t Task) Bytes() []byte {
}
return bts
}

type ExportedTask struct {
Title string `json:"desc"`
StartAt time.Time `json:"start"`
EndAt time.Time `json:"end"`
}

func (t ExportedTask) Bytes() []byte {
bts, err := json.Marshal(&t)
if err != nil {
log.Fatalln(err)
}
return bts
}
25 changes: 15 additions & 10 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import (
"github.com/dgraph-io/badger/v3"
)

var prefix = []byte("tasks.")
var sequenceID = []byte("tasks_seq")
var (
prefix = []byte("tasks.")
sequenceID = []byte("tasks_seq")
)

func GetTaskList(db *badger.DB) ([]model.Task, error) {
var tasks []model.Task
if err := db.View(func(txn *badger.Txn) error {
var it = txn.NewIterator(badger.DefaultIteratorOptions)
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
var item = it.Item()
item := it.Item()
err := item.Value(func(v []byte) error {
var task model.Task
if err := json.Unmarshal(v, &task); err != nil {
Expand All @@ -46,11 +48,11 @@ func GetTaskList(db *badger.DB) ([]model.Task, error) {

func CloseTasks(db *badger.DB) error {
return db.Update(func(txn *badger.Txn) error {
var it = txn.NewIterator(badger.DefaultIteratorOptions)
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
var item = it.Item()
var k = item.Key()
item := it.Item()
k := item.Key()
err := item.Value(func(v []byte) error {
var task model.Task
if err := json.Unmarshal(v, &task); err != nil {
Expand Down Expand Up @@ -87,7 +89,7 @@ func CreateTask(db *badger.DB, t string) error {
return err
}

var id = string(prefix) + strconv.FormatUint(s, 10)
id := string(prefix) + strconv.FormatUint(s, 10)
log.Println("creating task:", id, "->", t)
return txn.Set([]byte(id), model.Task{
ID: s,
Expand All @@ -97,7 +99,10 @@ func CreateTask(db *badger.DB, t string) error {
})
}

func LoadTasks(db *badger.DB, tasks []model.Task) error {
func LoadTasks(db *badger.DB, tasks []model.ExportedTask) error {
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].StartAt.Before(tasks[j].StartAt)
})
return db.Update(func(txn *badger.Txn) error {
seq, err := db.GetSequence(sequenceID, 100)
if err != nil {
Expand All @@ -110,7 +115,7 @@ func LoadTasks(db *badger.DB, tasks []model.Task) error {
if err != nil {
return err
}
var id = string(prefix) + strconv.FormatUint(s, 10)
id := string(prefix) + strconv.FormatUint(s, 10)
log.Println("creating task:", id, "->", t)
if err := txn.Set([]byte(id), model.Task{
ID: s,
Expand Down
11 changes: 10 additions & 1 deletion internal/ui/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io"

"github.com/caarlos0/tasktimer/internal/model"
"github.com/caarlos0/tasktimer/internal/store"
"github.com/dgraph-io/badger/v3"
)
Expand All @@ -16,7 +17,15 @@ func WriteProjectJSON(db *badger.DB, project string, w io.Writer) error {
return err
}

bts, err := json.Marshal(tasks)
var expTasks []model.ExportedTask
for _, t := range tasks {
expTasks = append(expTasks, model.ExportedTask{
Title: t.Title,
StartAt: t.StartAt,
EndAt: t.EndAt,
})
}
bts, err := json.Marshal(expTasks)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/ui/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func (m taskListModel) View() string {
func taskList(tasks []model.Task) string {
var s string
for _, t := range tasks {
var z = time.Now()
var icon = iconOngoing
var decorate = bold
z := time.Now()
icon := iconOngoing
decorate := bold
if !t.EndAt.IsZero() {
z = t.EndAt
icon = iconDone
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func Init(db *badger.DB, project string) tea.Model {
var input = textinput.NewModel()
input := textinput.NewModel()
input.Placeholder = "New task description..."
input.Focus()
input.CharLimit = 250
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func WriteProjectMarkdown(db *badger.DB, project string, w io.Writer) error {
w,
"> Total time **%s**, timed between **%s** and **%s**\n\n",
sumTasksTimes(tasks).Round(time.Second).String(),
tasks[len(tasks)-1].StartAt.Format(time.Stamp),
tasks[0].EndAt.Format(time.Stamp),
tasks[len(tasks)-1].StartAt.Format("2006-01-02"),
tasks[0].EndAt.Format("2006-01-02"),
)

for _, task := range tasks {
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/project_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func updateProjectTimerCmd(tasks []model.Task) tea.Cmd {
}

func sumTasksTimes(tasks []model.Task) time.Duration {
var d = time.Duration(0)
d := time.Duration(0)
for _, t := range tasks {
var z = t.EndAt
z := t.EndAt
if z.IsZero() {
z = time.Now()
}
Expand Down

0 comments on commit 941799b

Please sign in to comment.