Compare commits

..

No commits in common. 'main' and 'v0.0.1' have entirely different histories.
main ... v0.0.1

@ -1,38 +0,0 @@
# diary
## Installation
```
go install git.nagee.dev/isthisnagee/diary@v0.0.1
```
## Configuration
### Valid locations for the config file
The config file can be passed to the `diary` command through a command line
arg. For example: `diary --config=~/.diary.toml`
When there is no config argument, `diary` looks for the config here:
- `$HOME/.config/diary/diary.toml`
### Configuration options
#### `db_path`
- Name: `db_path`
- Type: String
- Default: `~/.diary.sql`
- Description: The location of the sqlite database. If the file does not exist,
it will be created.
```
db_path = "path/to/wherever/you/want/the/db/file"
```

@ -31,7 +31,7 @@ var deleteCmd = &cobra.Command{
}
fmt.Println("Are you sure you want to delete the following entry? (y/n)")
PrintEntry(entry)
PrintNotes(notes, App.Cfg.UseMarkdownInOutput)
PrintNotes(notes)
var response string
fmt.Scanln(&response)
if response == "y" {

@ -8,7 +8,6 @@ import (
"git.nagee.dev/isthisnagee/diary/model"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// listCmd represents the list command
@ -21,22 +20,11 @@ var listCmd = &cobra.Command{
$ diary list today
`,
Run: func(cmd *cobra.Command, args []string) {
var num_entries *int64 = new(int64)
q := model.GetDiaryEntriesQuery{}
*num_entries = viper.GetInt64("listNumEntries")
if *num_entries > 0 {
q.NumEntries = num_entries
}
results := App.Db.GetDiaryEntries(q)
results := App.Db.GetDiaryEntries(model.GetDiaryEntriesQuery{})
PrintEntries(results)
},
}
func init() {
listCmd.PersistentFlags().Int64P("num-entries", "n", 20, "The number of entries to list")
viper.BindPFlag("listNumEntries", listCmd.PersistentFlags().Lookup("num-entries"))
rootCmd.AddCommand(listCmd)
}

@ -8,9 +8,10 @@ import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "diary",
@ -36,7 +37,6 @@ func Execute() {
func init() {
cobra.OnInitialize(InitApp)
rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/config/diary/diary.toml)")
viper.BindPFlag("config", rootCmd.Flags().Lookup("config"))
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.diary.toml)")
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

@ -42,7 +42,7 @@ var showCmd = &cobra.Command{
var notes = App.Db.GetDiaryEntryNotes(entry.Id)
PrintEntry(entry)
fmt.Println()
PrintNotes(notes, App.Cfg.UseMarkdownInOutput)
PrintNotes(notes)
},
}

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

@ -8,6 +8,7 @@ import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"os"
"os/exec"
"path"
@ -16,8 +17,7 @@ import (
)
type Cfg struct {
DbPath string
UseMarkdownInOutput bool
DbPath string
}
type TApp struct {
@ -35,36 +35,28 @@ func initConfig() Cfg {
home, err := os.UserHomeDir()
cobra.CheckErr(err)
cfgFile := viper.GetString("config")
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath(path.Join(home, ".config", "diary"))
// Search config in home directory with name ".diary" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("toml")
viper.SetConfigName("diary")
viper.SetConfigName(".diary")
}
if viper.Get("db_path") == nil {
var db_path = path.Join(home, ".diary.sql")
viper.SetDefault("db_path", db_path)
}
if viper.Get("use_markdown_output") == nil {
viper.SetDefault("use_markdown_output", true)
}
var db_path = path.Join(home, ".diary.sql")
viper.SetDefault("db_path", db_path)
viper.AutomaticEnv() // read in environment variables that match
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found. That's OK
} else {
cobra.CheckErr(err)
}
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
return Cfg{viper.GetString("db_path"), viper.GetBool("use_markdown_output")}
return Cfg{db_path}
}
func InitApp() {
@ -72,13 +64,13 @@ func InitApp() {
if _, err := os.Stat(cfg.DbPath); errors.Is(err, os.ErrNotExist) {
_, err := os.Create(cfg.DbPath)
cobra.CheckErr(err)
if err != nil {
log.Fatal(err.Error())
}
}
var app, err = model.NewApp(cfg.DbPath)
cobra.CheckErr(err)
App = &TApp{app, &cfg}
var app = model.NewApp(cfg.DbPath)
App = &TApp{&app, &cfg}
}
var fmt_str = "%-10s %-20s %s\n"
@ -105,26 +97,21 @@ func PrintEntries(entries []*model.DiaryEntry) {
}
}
func PrintNote(note *model.DiaryEntryNote, add_tail bool, use_markdown bool) {
func PrintNote(note *model.DiaryEntryNote, add_tail bool) {
created_time := time.Unix(note.CreatedAt, 0).Format(time_fmt_str)
fmt.Println("---note ( id:", note.Id, ")", created_time)
fmt.Println()
var body string
if use_markdown {
body = string(markdown.Render(note.Body, 80, 3))
} else {
body = note.Body
}
fmt.Println(body)
body := markdown.Render(note.Body, 80, 3)
fmt.Println(string(body))
if add_tail {
fmt.Println("---")
}
}
func PrintNotes(notes []*model.DiaryEntryNote, use_markdown bool) {
func PrintNotes(notes []*model.DiaryEntryNote) {
for idx, entry := range notes {
var is_last = idx == len(notes)-1
PrintNote(entry, is_last, use_markdown)
PrintNote(entry, is_last)
}
}

@ -1,5 +1,6 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
*/
package main

@ -5,6 +5,7 @@ package model
import (
"database/sql"
"git.nagee.dev/isthisnagee/diary/db"
"log"
"strings"
)
@ -19,12 +20,12 @@ type App struct {
*db.DbCtx
}
func NewApp(db_path string) (*App, error) {
func NewApp(db_path string) App {
app, err := db.Init(db_path)
if err != nil {
return nil, err
log.Fatal(err.Error())
}
return &App{app}, nil
return App{app}
}
@ -104,8 +105,6 @@ type GetDiaryEntriesQuery struct {
CreatedBeforeTs *int64
/// Inclusive
CreatedAfterTs *int64
NumEntries *int64
}
func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry {
@ -122,10 +121,6 @@ func (app *App) GetDiaryEntries(q GetDiaryEntriesQuery) []*DiaryEntry {
whereParams = append(whereParams, *q.CreatedAfterTs)
}
query += " ORDER BY created_at desc, id desc"
if q.NumEntries != nil {
query += " LIMIT ?"
whereParams = append(whereParams, *q.NumEntries)
}
rows, err := app.Db.Query(
query,

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

Loading…
Cancel
Save