initial import
This commit is contained in:
92
note/note.go
Normal file
92
note/note.go
Normal file
@@ -0,0 +1,92 @@
|
||||
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
|
||||
}
|
||||
12
note/note_test.go
Normal file
12
note/note_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package note
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHelloWorld(t *testing.T) {
|
||||
n := NewNote(`Round the ragged rocks
|
||||
the rabid rascal ran.
|
||||
|
||||
How is a raven like a writing desk?
|
||||
`)
|
||||
t.Log(n)
|
||||
}
|
||||
Reference in New Issue
Block a user