93 lines
1.4 KiB
Go
93 lines
1.4 KiB
Go
|
package note
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"time"
|
||
|
|
||
|
"github.com/google/uuid"
|
||
|
)
|
||
|
|
||
|
var loc *time.Location
|
||
|
|
||
|
func init() {
|
||
|
var err error
|
||
|
loc, err = time.LoadLocation("America/Los_Angeles")
|
||
|
if err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type Header struct {
|
||
|
ID string
|
||
|
Created time.Time
|
||
|
}
|
||
|
|
||
|
func NewHeader() *Header {
|
||
|
return &Header{
|
||
|
ID: uuid.NewString(),
|
||
|
Created: time.Now().In(loc),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (h *Header) Title() string {
|
||
|
return h.Created.Format("Quick Note 2006-01-02 15:04")
|
||
|
}
|
||
|
|
||
|
func (h *Header) String() string {
|
||
|
return fmt.Sprintf(`---
|
||
|
id: %s
|
||
|
title: %s
|
||
|
desc: ''
|
||
|
updated: %d
|
||
|
created: %d
|
||
|
---
|
||
|
`, h.ID, h.Title(), h.Created.Unix()*1000, h.Created.Unix()*1000)
|
||
|
}
|
||
|
|
||
|
type Note struct {
|
||
|
Header *Header
|
||
|
Body string
|
||
|
}
|
||
|
|
||
|
func NewNote(s string) *Note {
|
||
|
return &Note{
|
||
|
Header: NewHeader(),
|
||
|
Body: s,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var BasePath = []string{"data", "kyle", "dendron", "vault"}
|
||
|
|
||
|
func (n *Note) basePath() string {
|
||
|
return n.Header.Created.Format("quick.2006.01.02.1504.md")
|
||
|
}
|
||
|
|
||
|
func (n *Note) Path() string {
|
||
|
path := BasePath[:]
|
||
|
path = append(path, n.basePath())
|
||
|
return filepath.Join(path...)
|
||
|
}
|
||
|
|
||
|
func (n *Note) String() string {
|
||
|
return fmt.Sprintf(`%s
|
||
|
%s`, n.Header, n.Body)
|
||
|
}
|
||
|
|
||
|
func (n *Note) Write() error {
|
||
|
path := n.Path()
|
||
|
file, err := os.Create(path)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
_, err = file.WriteString(n.String())
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|