Skip to content

Commit

Permalink
feat: use userdirs
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Jan 19, 2021
1 parent 69d202b commit b52c048
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,42 @@ Or use any of the other provided means in the [releases page][releases].

## FAQ

### Where is my data stored?
### Where are data and logs stored?

Data is stored in `~/tasktimer/{projectname}.db`.
Depends on the OS, but you can see yours running:

[Badger][] is used as database, which means you can open it with the badger
CLI if you want to.
```sh
tt paths
```

## Upgrades

### From 1.0.x to 1.1.x

Data was moved from `~/tasktimer` to user data and user logs dir according to
the OS.

To move, run:

```sh
tt paths
```

It will print something like this:

```
Database path: /Users/carlos/Library/Application Support/tasktimer/default.db
Log path: /Users/carlos/Library/Logs/tasktimer/default.log
```

We only need to migrate the data, so:

### Are there any logs?
```sh
rm -rf "/Users/carlos/Library/Application Support/tasktimer/"*.db # make sure its empty
cp -rf ~/tasktimer/*.db "/Users/carlos/Library/Application Support/tasktimer/" # copy data
rm -rf ~/tasktimer # delete old folder

Yes, they are written to `~/tasktimer/{projectname}.log`.
```

## Stargazers over time

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/charmbracelet/bubbles v0.7.6
github.com/charmbracelet/bubbletea v0.12.4
github.com/dgraph-io/badger/v3 v3.0.0-20210114060103-a35b08db404b
github.com/muesli/go-app-paths v0.2.1
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68
github.com/muesli/termenv v0.7.4
github.com/spf13/cobra v1.1.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
Expand All @@ -173,6 +174,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mmcloughlin/avo v0.0.0-20201105074841-5d2f697d268f/go.mod h1:6aKT4zZIrpGqB3RpFU14ByCSSyKY6LfJz4J/JJChHfI=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/muesli/go-app-paths v0.2.1 h1:Qi+2igkDX2aPqyRddp7P0sMQIBwBqhkfQfNcjdGjL6Y=
github.com/muesli/go-app-paths v0.2.1/go.mod h1:SxS3Umca63pcFcLtbjVb+J0oD7cl4ixQWoBKhGEtEho=
github.com/muesli/reflow v0.2.0 h1:2o0UBJPHHH4fa2GCXU4Rg4DwOtWPMekCeyc5EWbAQp0=
github.com/muesli/reflow v0.2.0/go.mod h1:qT22vjVmM9MIUeLgsVYe/Ye7eZlbv9dZjL3dVhUqLX8=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68 h1:y1p/ycavWjGT9FnmSjdbWUlLGvcxrY0Rw3ATltrxOhk=
Expand Down
59 changes: 43 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
"io"
"log"
"os"
"path/filepath"

"github.com/caarlos0/tasktimer/internal/ui"
tea "github.com/charmbracelet/bubbletea"
"github.com/dgraph-io/badger/v3"
gap "github.com/muesli/go-app-paths"
"github.com/spf13/cobra"
)

Expand All @@ -17,10 +18,10 @@ var project string
func main() {
rootCmd.PersistentFlags().StringVarP(&project, "project", "p", "default", "Project name")

rootCmd.AddCommand(reportCmd, completionsCmd)
rootCmd.AddCommand(reportCmd, completionsCmd, pathsCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Expand All @@ -29,11 +30,12 @@ var rootCmd = &cobra.Command{
Use: "tt",
Short: "Task Timer (tt) is a dead simple TUI task timer",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := setup()
db, f, err := setup()
if err != nil {
return err
}
defer db.Close()
defer f.Close()

var p = tea.NewProgram(ui.Init(db, project))
p.EnterAltScreen()
Expand All @@ -47,36 +49,61 @@ var reportCmd = &cobra.Command{
Aliases: []string{"r"},
Short: "Print a markdown report of the given project to STDOUT",
RunE: func(cmd *cobra.Command, args []string) error {
db, err := setup()
db, f, err := setup()
if err != nil {
return err
}
defer db.Close()
defer f.Close()

return ui.WriteProjectMarkdown(db, project, os.Stdout)
},
}

func setup() (*badger.DB, error) {
home, err := os.UserHomeDir()
var pathsCmd = &cobra.Command{
Use: "paths",
Short: "Print the paths being used for logs, data et al",
RunE: func(cmd *cobra.Command, args []string) error {
logfile, dbfile, err := paths()
if err != nil {
return err
}
fmt.Println("Database path:", dbfile)
fmt.Println("Log path: ", logfile)
return nil
},
}

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

logfile, err := home.LogPath(project + ".log")
if err != nil {
return nil, err
return "", "", err
}

var folder = filepath.Join(home, "tasktimer")
if err := os.MkdirAll(folder, 0764); err != nil {
return nil, err
dbfile, err := home.DataPath(project + ".db")
if err != nil {
return "", "", err
}

log.SetFlags(0)
return logfile, dbfile, nil
}

f, err := tea.LogToFile(filepath.Join(folder, project+".log"), "")
defer f.Close()
func setup() (*badger.DB, io.Closer, error) {
logfile, dbfile, err := paths()
if err != nil {
return nil, nil, err
}

log.SetFlags(0)
f, err := tea.LogToFile(logfile, "")

var options = badger.DefaultOptions(filepath.Join(folder, project+".db")).
var options = badger.DefaultOptions(dbfile).
WithLogger(badgerStdLoggerAdapter{}).
WithLoggingLevel(badger.ERROR)
return badger.Open(options)
db, err := badger.Open(options)
return db, f, err
}

type badgerStdLoggerAdapter struct{}
Expand Down

0 comments on commit b52c048

Please sign in to comment.