package cmd import ( "errors" "fmt" "git.nagee.dev/isthisnagee/diary/model" markdown "github.com/MichaelMure/go-term-markdown" "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/viper" "os" "os/exec" "path" "strconv" "time" ) type Cfg struct { DbPath string UseMarkdownInOutput bool } type TApp struct { Db *model.App Cfg *Cfg } var App *TApp // var App *model.App // initConfig reads in config file and ENV variables if set. func initConfig() Cfg { // Find home directory. 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")) viper.SetConfigType("toml") 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) } 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) } } return Cfg{viper.GetString("db_path"), viper.GetBool("use_markdown_output")} } func InitApp() { var cfg = initConfig() if _, err := os.Stat(cfg.DbPath); errors.Is(err, os.ErrNotExist) { _, err := os.Create(cfg.DbPath) cobra.CheckErr(err) } var app, err = model.NewApp(cfg.DbPath) cobra.CheckErr(err) App = &TApp{app, &cfg} } var fmt_str = "%-10s %-20s %s\n" var time_fmt_str = "2006/01/02 03:04pm" func PrintEntryHeader() { fmt.Printf( fmt_str+"\n", "Id", "Created time", "Title", ) } func PrintEntry(entry *model.DiaryEntry) { created_time := time.Unix(entry.CreatedAt, 0).Format(time_fmt_str) titleColor := color.New(color.FgCyan).Add(color.Underline).SprintFunc() fmt.Printf( fmt_str, strconv.FormatInt(entry.Id, 10), created_time, titleColor(entry.Title), ) } func PrintEntries(entries []*model.DiaryEntry) { PrintEntryHeader() for _, entry := range entries { PrintEntry(entry) } } func PrintNote(note *model.DiaryEntryNote, add_tail bool, use_markdown 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) if add_tail { fmt.Println("---") } } func PrintNotes(notes []*model.DiaryEntryNote, use_markdown bool) { for idx, entry := range notes { var is_last = idx == len(notes)-1 PrintNote(entry, is_last, use_markdown) } } func OpenEditor(fpath string) error { var editor = os.Getenv("EDITOR") if editor == "" { editor = "nano" } editor_cmd := exec.Command(editor, fpath) editor_cmd.Stdin = os.Stdin editor_cmd.Stdout = os.Stdout editor_cmd.Stderr = os.Stderr err := editor_cmd.Start() if err != nil { return err } err = editor_cmd.Wait() if err != nil { return err } return nil }