Skip to content

Commit

Permalink
refactor: centralize dbPath variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Diegiwg committed Jun 26, 2024
1 parent 252f9a3 commit ac12196
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
10 changes: 10 additions & 0 deletions data/data.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package data

import (
"os"
"path/filepath"
)

const DATA_BASE_RECORDS_LIMIT = 1_000

func Path() string {
USER_HOME, _ := os.UserHomeDir()
return filepath.Join(USER_HOME, "tt.db")
}
5 changes: 1 addition & 4 deletions data/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package data
import (
"encoding/json"
"os"
"path/filepath"

"github.com/Diegiwg/cli"
"github.com/Diegiwg/tt/model"
)

func ReadOrCreateRecord(ctx *cli.Context) model.RecordTable {
USER_HOME, _ := os.UserHomeDir()

dbPath := filepath.Join(USER_HOME, "tt.db")
dbPath := Path()

_, err := os.Stat(dbPath)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions data/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package data
import (
"encoding/json"
"os"
"path/filepath"

"github.com/Diegiwg/cli"
"github.com/Diegiwg/tt/model"
)

func SaveRecordToFile(ctx *cli.Context, table *model.RecordTable) {
USER_HOME, _ := os.UserHomeDir()

dbPath := filepath.Join(USER_HOME, "tt.db")
dbPath := Path()

if len(table.Records) > DATA_BASE_RECORDS_LIMIT {
table.Records = table.Records[len(table.Records)-DATA_BASE_RECORDS_LIMIT:]
Expand Down

0 comments on commit ac12196

Please sign in to comment.