t

ref: master

commands/create.go


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package commands

import (
	"fmt"
	"git.sr.ht/~porcellis/t/config"
	"git.sr.ht/~porcellis/t/models"
	"os"
	"path"
	"strings"
	"time"
)

func CreateWithName(config config.TConfig, originalTitle string) (models.Note, error) {
	var (
		err  error
		note models.Note
	)

	title := strings.Replace(originalTitle, " ", "-", -1)
	file, err := os.Create(path.Join(config.BasePath, fmt.Sprintf("%s.md", title)))

	stat, _ := file.Stat()

	note = models.Note{Name: stat.Name(), Path: path.Join(config.BasePath, stat.Name()), ModTime: stat.ModTime()}

	return note, err
}

func Create(config config.TConfig) (models.Note, error) {
	var (
		err  error
		note models.Note
	)

	currentTime := time.Now()
	noteName := currentTime.Format("2006-01-02T15:04:05Z07:00")

	file, err := os.Create(path.Join(config.BasePath, fmt.Sprintf("%s.md", noteName)))

	stat, _ := file.Stat()
	note = models.Note{Name: stat.Name(), Path: path.Join(config.BasePath, stat.Name()), ModTime: stat.ModTime()}

	return note, err
}