Skip to content

Commit

Permalink
Merge pull request #3 from mkaz/config
Browse files Browse the repository at this point in the history
Add config file for jotsdir and timestamp format
  • Loading branch information
mkaz committed Dec 13, 2017
2 parents f6a2f45 + 24478cf commit 958ee1d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
16 changes: 12 additions & 4 deletions jot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ var n int
var now time.Time
var tsRe *regexp.Regexp
var errlog *log.Logger
var jotsdir string
var files []string
var template string

type Config struct {
Jotsdir string
Timestamp string
}

var conf Config

func main() {
errlog = log.New(os.Stderr, "", 0)
now = time.Now()
Expand Down Expand Up @@ -60,10 +66,13 @@ func main() {
}

if *versionFlag {
fmt.Println("jot v0.2")
fmt.Println("jot v0.3")
os.Exit(0)
}

// read in config file if exists
conf = getJotsConfig()

if *today {
n = 1
}
Expand All @@ -73,7 +82,6 @@ func main() {
}

// retrieve the base jots directory
jotsdir = getJotsDirectory()
files = getJotFiles()

// --------------------------------------------------
Expand Down Expand Up @@ -188,7 +196,7 @@ func main() {
tpl := ""
if template != "" {
// attempt to read in template
templateFile := filepath.Join(jotsdir, "tmpl"+template+".txt")
templateFile := filepath.Join(conf.Jotsdir, "tmpl"+template+".txt")
content, err := ioutil.ReadFile(templateFile)
if err == nil {
tpl = string(content)
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ I use the following template when I am trying to debug a tricky problem.

Jot creates a single text file a day, `jot-2017-01-06.txt` organizing them in monthly and yearly folders. By default it stores them in `~/Documents/jots/` that directory must already exist or it will give a warning.

Jot will look for a config file `.jotrc` in your home directory in TOML format.

```
# Jot config file
# base directory that all jots are stored
jotsdir = "~/Documents/jots"
# golang timestamp format for note delimiter
# See: https://golang.org/pkg/time/
timestamp = "2006-01-02 3:04PM"
```


### Errata

Expand Down
30 changes: 21 additions & 9 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
"path/filepath"
"strings"
"time"

"github.com/BurntSushi/toml"
)

// Determine file to edit by date
func getFilepathDate(dt time.Time) (filename, dir string) {

// build directory from date
dir = filepath.Join(jotsdir, dt.Format("2006"), dt.Format("01"))
dir = filepath.Join(conf.Jotsdir, dt.Format("2006"), dt.Format("01"))

// build filename from date
filename = "jot-" + dt.Format("2006-01-02") + ".txt"
Expand All @@ -27,27 +29,37 @@ func getFilepathDate(dt time.Time) (filename, dir string) {
return file, dir
}

func getJotsDirectory() (dir string) {

// TODO: config for jots dir
func getJotsConfig() (conf Config) {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}

dir = filepath.Join(usr.HomeDir, "Documents", "jots")
configFile := filepath.Join(usr.HomeDir, ".jotrc")
toml.DecodeFile(configFile, &conf)

// set default diretory if not in config
if conf.Jotsdir == "" {
conf.Jotsdir = filepath.Join(usr.HomeDir, "Documents", "jots")
}

// fail out if directory does not exist, cant do anything
// without the directory and its a good idea to let the
// user create the first one, so they know where it is
if _, err := os.Stat(dir); os.IsNotExist(err) {
errlog.Fatalln("Base directory does not exist", jotsdir)
if _, err := os.Stat(conf.Jotsdir); os.IsNotExist(err) {
errlog.Fatalln("Base directory does not exist", conf.Jotsdir)
}
return dir

// set default timestamp if not in config
if conf.Timestamp == "" {
conf.Timestamp = "2006-01-02 3:04PM"
}

return conf
}

func getJotFiles() (fa []string) {
filepath.Walk(jotsdir, func(path string, fi os.FileInfo, err error) error {
filepath.Walk(conf.Jotsdir, func(path string, fi os.FileInfo, err error) error {
if !fi.IsDir() {
if strings.Contains(path, "jot-") && strings.Contains(path, ".txt") {
fa = append(fa, path)
Expand Down
3 changes: 1 addition & 2 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func openInEditor(file string, args []string) {
// Write Jot to File
func writeFile(filename, text string) {
if !editFlag { // append timestamp
// TODO: config for timestamp date string
var timestamp = now.Format("2006-01-02 3:04PM")
var timestamp = now.Format(conf.Timestamp)
text = "\n" + timestamp + " :: " + text
}

Expand Down

0 comments on commit 958ee1d

Please sign in to comment.