Limit entries in `diary list`

- Adds flag to limit the number of entries in `diary list`.
- The defualt number of entries listed in `diary list` is 20.
- Bug with db_path config arg is now fixed. db_path is now properly read.
pull/12/head v0.0.2
Gitea 3 years ago committed by isthisnagee
parent d00ceceeec
commit 6fab8998d4

@ -21,14 +21,14 @@ var listCmd = &cobra.Command{
$ diary list today $ diary list today
`, `,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var num_entries *int64 = new(int64) var num_entries *int64 = new(int64)
q := model.GetDiaryEntriesQuery{} q := model.GetDiaryEntriesQuery{}
*num_entries = viper.GetInt64("listNumEntries") *num_entries = viper.GetInt64("listNumEntries")
if (*num_entries > 0) { if *num_entries > 0 {
q.NumEntries = num_entries q.NumEntries = num_entries
} }
results := App.Db.GetDiaryEntries(q) results := App.Db.GetDiaryEntries(q)
PrintEntries(results) PrintEntries(results)
@ -36,7 +36,7 @@ var listCmd = &cobra.Command{
} }
func init() { func init() {
listCmd.PersistentFlags().Int64P("num-entries", "n", 20, "The number of entries to list") listCmd.PersistentFlags().Int64P("num-entries", "n", 20, "The number of entries to list")
viper.BindPFlag("listNumEntries", listCmd.PersistentFlags().Lookup("num-entries")) viper.BindPFlag("listNumEntries", listCmd.PersistentFlags().Lookup("num-entries"))
rootCmd.AddCommand(listCmd) rootCmd.AddCommand(listCmd)
} }

@ -37,6 +37,6 @@ func Execute() {
func init() { func init() {
cobra.OnInitialize(InitApp) cobra.OnInitialize(InitApp)
rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.diary.toml)") rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.diary.toml)")
viper.BindPFlag("config", rootCmd.Flags().Lookup("config")) viper.BindPFlag("config", rootCmd.Flags().Lookup("config"))
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }

@ -7,9 +7,9 @@ package cmd
import ( import (
"time" "time"
"github.com/spf13/viper"
"git.nagee.dev/isthisnagee/diary/model" "git.nagee.dev/isthisnagee/diary/model"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
// todayCmd represents the today command // todayCmd represents the today command
@ -24,7 +24,7 @@ var todayCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var created_after_ts *int64 = new(int64) var created_after_ts *int64 = new(int64)
var created_before_ts *int64 = new(int64) var created_before_ts *int64 = new(int64)
var num_entries *int64 = new(int64) var num_entries *int64 = new(int64)
var now = time.Now() var now = time.Now()
var startOfToday = time.Date( var startOfToday = time.Date(
@ -41,13 +41,13 @@ var todayCmd = &cobra.Command{
var endOfToday = startOfToday.AddDate(0, 0, 1) var endOfToday = startOfToday.AddDate(0, 0, 1)
*created_before_ts = endOfToday.Unix() *created_before_ts = endOfToday.Unix()
if viper.GetInt64("listNumEntries") > 0 { if viper.GetInt64("listNumEntries") > 0 {
*num_entries = viper.GetInt64("listNumEntries") *num_entries = viper.GetInt64("listNumEntries")
} }
results := App.Db.GetDiaryEntries(model.GetDiaryEntriesQuery{ results := App.Db.GetDiaryEntries(model.GetDiaryEntriesQuery{
CreatedBeforeTs: created_before_ts, CreatedBeforeTs: created_before_ts,
CreatedAfterTs: created_after_ts, CreatedAfterTs: created_after_ts,
NumEntries: num_entries, NumEntries: num_entries,
}) })
PrintEntries(results) PrintEntries(results)

@ -34,7 +34,7 @@ func initConfig() Cfg {
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
cobra.CheckErr(err) cobra.CheckErr(err)
cfgFile := viper.GetString("config") cfgFile := viper.GetString("config")
if cfgFile != "" { if cfgFile != "" {
// Use config file from the flag. // Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
@ -51,13 +51,13 @@ func initConfig() Cfg {
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok { if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found. That's OK // Config file not found. That's OK
} else { } else {
cobra.CheckErr(err) cobra.CheckErr(err)
} }
} }
return Cfg{viper.GetString("db_path")} return Cfg{viper.GetString("db_path")}
} }

@ -105,7 +105,7 @@ type GetDiaryEntriesQuery struct {
/// Inclusive /// Inclusive
CreatedAfterTs *int64 CreatedAfterTs *int64
NumEntries *int64 NumEntries *int64
} }
func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry { func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry {
@ -122,10 +122,10 @@ func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry {
whereParams = append(whereParams, *q.CreatedAfterTs) whereParams = append(whereParams, *q.CreatedAfterTs)
} }
query += " ORDER BY created_at desc, id desc" query += " ORDER BY created_at desc, id desc"
if q.NumEntries != nil { if q.NumEntries != nil {
query += " LIMIT ?" query += " LIMIT ?"
whereParams = append(whereParams, *q.NumEntries) whereParams = append(whereParams, *q.NumEntries)
} }
rows, err := app.Db.Query( rows, err := app.Db.Query(
query, query,

@ -2,34 +2,34 @@ package model
import ( import (
"git.nagee.dev/isthisnagee/diary/db" "git.nagee.dev/isthisnagee/diary/db"
"runtime/debug"
"testing" "testing"
"runtime/debug"
) )
func assert_string(t *testing.T, expected string, actual string) { func assert_string(t *testing.T, expected string, actual string) {
if actual != expected { if actual != expected {
t.Log(string(debug.Stack())) t.Log(string(debug.Stack()))
t.Fatalf("(%v, %v)", expected, actual) t.Fatalf("(%v, %v)", expected, actual)
} }
} }
func assert_int(t *testing.T, expected int64, actual int64) { func assert_int(t *testing.T, expected int64, actual int64) {
if actual != expected { if actual != expected {
t.Log(string(debug.Stack())) t.Log(string(debug.Stack()))
t.Fatalf("(%v, %v)", expected, actual) t.Fatalf("(%v, %v)", expected, actual)
} }
} }
func assert_bool(t *testing.T, expected bool, actual bool) { func assert_bool(t *testing.T, expected bool, actual bool) {
if actual != expected { if actual != expected {
t.Log(string(debug.Stack())) t.Log(string(debug.Stack()))
t.Fatalf("(%v, %v)", expected, actual) t.Fatalf("(%v, %v)", expected, actual)
} }
} }
func assert_exists(t *testing.T, actual interface{}) { func assert_exists(t *testing.T, actual interface{}) {
if actual == nil { if actual == nil {
t.Log(string(debug.Stack())) t.Log(string(debug.Stack()))
t.Fatalf("Unexpected nil: %s", actual) t.Fatalf("Unexpected nil: %s", actual)
} }
} }
@ -111,27 +111,25 @@ func TestGetDiaryEntries(t *testing.T) {
var result_1 = app.NewDiaryEntry("Met with Nagee @ 1PM") var result_1 = app.NewDiaryEntry("Met with Nagee @ 1PM")
var result_2 = app.NewDiaryEntry("Met with Nagee @ 2PM") var result_2 = app.NewDiaryEntry("Met with Nagee @ 2PM")
// no numEntries // no numEntries
entries := app.GetDiaryEntries( entries := app.GetDiaryEntries(
GetDiaryEntriesQuery{}, GetDiaryEntriesQuery{},
) )
assert_int(t, int64(len(entries)), 2) assert_int(t, int64(len(entries)), 2)
assert_int(t, result_2.Id, entries[0].Id) assert_int(t, result_2.Id, entries[0].Id)
assert_int(t, result_1.Id, entries[1].Id) assert_int(t, result_1.Id, entries[1].Id)
var numEntries = new(int64)
var numEntries = new(int64) *numEntries = 1
*numEntries = 1
entries = app.GetDiaryEntries( entries = app.GetDiaryEntries(
GetDiaryEntriesQuery{ GetDiaryEntriesQuery{
NumEntries: numEntries, NumEntries: numEntries,
}, },
) )
assert_int(t, int64(len(entries)), 1) assert_int(t, int64(len(entries)), 1)
assert_int(t, result_2.Id, entries[0].Id) assert_int(t, result_2.Id, entries[0].Id)
teardown(app) teardown(app)
} }

Loading…
Cancel
Save