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