@@ -6,19 +6,22 @@ import (
66 "io/ioutil"
77 "klog"
88 "klog/parser"
9+ "klog/service"
910 "os"
1011 "os/exec"
1112 "os/user"
1213 "path/filepath"
1314 "strings"
15+ "time"
1416)
1517
1618var BinaryVersion string // will be set during build
1719var BinaryBuildHash string // will be set during build
1820
1921type Context interface {
2022 Print (string )
21- HomeDir () string
23+ KlogFolder () string
24+ HomeFolder () string
2225 MetaInfo () struct {
2326 Version string
2427 BuildHash string
@@ -27,6 +30,7 @@ type Context interface {
2730 SetBookmark (string ) error
2831 Bookmark () * File
2932 OpenInFileBrowser (string ) error
33+ AppendTemplateToFile (string , string ) error
3034}
3135
3236type context struct {
@@ -51,10 +55,14 @@ func (c *context) Print(text string) {
5155 fmt .Print (text )
5256}
5357
54- func (c * context ) HomeDir () string {
58+ func (c * context ) HomeFolder () string {
5559 return c .homeDir
5660}
5761
62+ func (c * context ) KlogFolder () string {
63+ return c .homeDir + "/.klog/"
64+ }
65+
5866func (c * context ) MetaInfo () struct {
5967 Version string
6068 BuildHash string
@@ -95,9 +103,9 @@ func (c *context) RetrieveRecords(paths ...string) ([]klog.Record, error) {
95103 if err != nil {
96104 return nil , err
97105 }
98- rs , errs := parser .Parse (content )
99- if errs != nil {
100- return nil , errs
106+ rs , parserErrors := parser .Parse (content )
107+ if parserErrors != nil {
108+ return nil , parserErrors
101109 }
102110 records = append (records , rs ... )
103111 }
@@ -111,7 +119,7 @@ type File struct {
111119}
112120
113121func (c * context ) Bookmark () * File {
114- bookmarkPath := c .HomeDir () + "/.klog/ bookmark.klg"
122+ bookmarkPath := c .KlogFolder () + "bookmark.klg"
115123 dest , err := os .Readlink (bookmarkPath )
116124 if err != nil {
117125 return nil
@@ -130,30 +138,64 @@ func (c *context) Bookmark() *File {
130138func (c * context ) SetBookmark (path string ) error {
131139 bookmark , err := filepath .Abs (path )
132140 if err != nil {
133- return err
141+ return errors . New ( "Target file does not exist" )
134142 }
135143 if ! strings .HasSuffix (bookmark , ".klg" ) {
136144 return errors .New ("File name must have .klg extension" )
137145 }
138- klogFolder := c .HomeDir () + "/.klog"
146+ klogFolder := c .KlogFolder ()
139147 err = os .MkdirAll (klogFolder , 0700 )
140148 if err != nil {
141- return err
149+ return errors . New ( "Unable to initialise ~/.klog folder" )
142150 }
143151 symlink := klogFolder + "/bookmark.klg"
144152 _ = os .Remove (symlink )
145- return os .Symlink (bookmark , symlink )
153+ err = os .Symlink (bookmark , symlink )
154+ if err != nil {
155+ return errors .New ("Failed to create bookmark" )
156+ }
157+ return nil
146158}
147159
148160func (c * context ) OpenInFileBrowser (path string ) error {
149161 cmd := exec .Command ("open" , path )
150162 return cmd .Run ()
151163}
152164
165+ func (c * context ) AppendTemplateToFile (filePath string , templateName string ) error {
166+ location := c .KlogFolder () + templateName + ".template.klg"
167+ template , err := readFile (location )
168+ if err != nil {
169+ return errors .New ("No such template: " + location )
170+ }
171+ instance , err := service .RenderTemplate (template , time .Now ())
172+ if err != nil {
173+ return err
174+ }
175+ contents , err := readFile (filePath )
176+ if err != nil {
177+ return err
178+ }
179+ err = appendToFile (filePath , service .AppendableText (contents , instance ))
180+ return err
181+ }
182+
153183func readFile (path string ) (string , error ) {
154184 contents , err := ioutil .ReadFile (path )
155185 if err != nil {
156- return "" , err
186+ return "" , errors . New ( "Cannot read file: " + path )
157187 }
158188 return string (contents ), nil
159189}
190+
191+ func appendToFile (path string , textToAppend string ) error {
192+ file , err := os .OpenFile (path , os .O_APPEND | os .O_WRONLY , 0644 )
193+ if err != nil {
194+ return errors .New ("Cannot write to file: " + path )
195+ }
196+ defer file .Close ()
197+ if _ , err := file .WriteString (textToAppend ); err != nil {
198+ return errors .New ("Cannot write to file: " + path )
199+ }
200+ return nil
201+ }
0 commit comments