diff --git a/cmd/cli/main.go b/cmd/cli/main.go new file mode 100644 index 0000000..43ca808 --- /dev/null +++ b/cmd/cli/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "ketotrack/config" + "ketotrack/data" + "ketotrack/ui/cli" +) + +func main() { + appConfig := config.Load() + appCtx, err := data.NewContext(appConfig.DataPath) + if err != nil { + fmt.Printf("Error initializing application context: %s\n", err) + return + } + cli.Run(&appCtx) +} diff --git a/data/records.go b/data/data.go similarity index 76% rename from data/records.go rename to data/data.go index 6297592..9ede60a 100644 --- a/data/records.go +++ b/data/data.go @@ -6,15 +6,9 @@ import ( "os" ) -// Record holds a pointer to a Reading or Note. -type Record struct { - Reading *model.Reading `json:"reading,omitempty"` - Note *model.Note `json:"note,omitempty"` -} - // AppContext is the application data store that holds Records type AppContext struct { - Records []Record + Records []model.Record dataPath string } @@ -47,10 +41,10 @@ func (ctx *AppContext) SaveRecords() error { // AddReading will add a Reading to the AppContext func (ctx *AppContext) AddReading(reading model.Reading) { - ctx.Records = append(ctx.Records, Record{Reading: &reading}) + ctx.Records = append(ctx.Records, model.Record{Reading: &reading}) } // AddNote will a add a Note to the AppContext func (ctx *AppContext) AddNote(note model.Note) { - ctx.Records = append(ctx.Records, Record{Note: ¬e}) + ctx.Records = append(ctx.Records, model.Record{Note: ¬e}) } diff --git a/model/types.go b/model/types.go index 9d3ed05..499da22 100644 --- a/model/types.go +++ b/model/types.go @@ -41,3 +41,9 @@ func NewReading(glucose, ketone float64) Reading { GKI: gki, } } + +// Record holds a pointer to a Reading or Note. +type Record struct { + Reading *Reading `json:"reading,omitempty"` + Note *Note `json:"note,omitempty"` +} diff --git a/main.go b/ui/cli/cli.go similarity index 78% rename from main.go rename to ui/cli/cli.go index aec2609..8fe5c85 100644 --- a/main.go +++ b/ui/cli/cli.go @@ -1,51 +1,17 @@ -package main +package cli import ( "bufio" "errors" "fmt" - "os" - "path/filepath" - "strconv" - "strings" - - "ketotrack/config" "ketotrack/data" "ketotrack/model" + "os" + "strconv" + "strings" ) -var recordsFilename string - -func init() { - // Get user's homeDir directory - homeDir, err := os.UserHomeDir() - if err != nil { - fmt.Println("Error fetching user's home directory:", err) - os.Exit(1) - } - - // Define the path to the application data directory - ketotrackDir := filepath.Join(homeDir, ".ketotrack") - - // Create the directory if it does not exist - err = os.MkdirAll(ketotrackDir, 0770) - if err != nil { - fmt.Println("Error creating .ketotrack directory:", err) - os.Exit(1) - } - - // Set data file path for readings - recordsFilename = filepath.Join(ketotrackDir, "records.json") -} - -func main() { - appConfig := config.Load() - appCtx, err := data.NewContext(appConfig.DataPath) - if err != nil { - fmt.Printf("Error initializing application context: %s\n", err) - return - } - +func Run(appCtx *data.AppContext) { if err := appCtx.LoadRecords(); err != nil { fmt.Printf("Error loading records from file: %s\n", err) }