11package app
22
33import (
4- "errors"
54 "fmt"
65 "io/ioutil"
76 "klog"
@@ -27,10 +26,10 @@ type Context interface {
2726 BuildHash string
2827 }
2928 RetrieveRecords (... string ) ([]klog.Record , error )
30- SetBookmark (string ) error
31- Bookmark () * File
32- OpenInFileBrowser (string ) error
33- AppendTemplateToFile (string , string ) error
29+ SetBookmark (string ) Error
30+ Bookmark () ( * File , Error )
31+ OpenInFileBrowser (string ) Error
32+ AppendTemplateToFile (string , string ) Error
3433}
3534
3635type context struct {
@@ -91,11 +90,14 @@ func (c *context) MetaInfo() struct {
9190
9291func (c * context ) RetrieveRecords (paths ... string ) ([]klog.Record , error ) {
9392 if len (paths ) == 0 {
94- if b := c .Bookmark (); b != nil {
95- paths = []string {b .Path }
96- } else {
97- return nil , errors .New ("No input file(s) specified; couldn’t read from bookmarked file either." )
93+ b , err := c .Bookmark ()
94+ if err != nil {
95+ return nil , appError {
96+ "No input files specified" ,
97+ "Either specify input files, or set a bookmark" ,
98+ }
9899 }
100+ paths = []string {b .Path }
99101 }
100102 var records []klog.Record
101103 for _ , p := range paths {
@@ -118,84 +120,123 @@ type File struct {
118120 Path string
119121}
120122
121- func (c * context ) Bookmark () * File {
123+ func (c * context ) Bookmark () ( * File , Error ) {
122124 bookmarkPath := c .KlogFolder () + "bookmark.klg"
123125 dest , err := os .Readlink (bookmarkPath )
124126 if err != nil {
125- return nil
127+ return nil , appError {
128+ "No bookmark set" ,
129+ "You can set a bookmark by running: klog bookmark somefile.klg" ,
130+ }
126131 }
127132 _ , err = os .Stat (dest )
128133 if err != nil {
129- return nil
134+ return nil , appError {
135+ "Bookmark doesn’t point to valid file" ,
136+ "Please check the current bookmark location or set a new one" ,
137+ }
130138 }
131139 return & File {
132140 Name : filepath .Base (dest ),
133141 Location : filepath .Dir (dest ),
134142 Path : dest ,
135- }
143+ }, nil
136144}
137145
138- func (c * context ) SetBookmark (path string ) error {
146+ func (c * context ) SetBookmark (path string ) Error {
139147 bookmark , err := filepath .Abs (path )
140148 if err != nil {
141- return errors .New ("Target file does not exist" )
149+ return appError {
150+ "Invalid target file" ,
151+ "Please check the file path" ,
152+ }
142153 }
143154 if ! strings .HasSuffix (bookmark , ".klg" ) {
144- return errors .New ("File name must have .klg extension" )
155+ return appError {
156+ "Invalid file extension" ,
157+ "File name must have .klg extension" ,
158+ }
145159 }
146160 klogFolder := c .KlogFolder ()
147161 err = os .MkdirAll (klogFolder , 0700 )
148162 if err != nil {
149- return errors .New ("Unable to initialise ~/.klog folder" )
163+ return appError {
164+ "Unable to initialise ~/.klog folder" ,
165+ "Please create a ~/.klog folder manually" ,
166+ }
150167 }
151168 symlink := klogFolder + "/bookmark.klg"
152169 _ = os .Remove (symlink )
153170 err = os .Symlink (bookmark , symlink )
154171 if err != nil {
155- return errors .New ("Failed to create bookmark" )
172+ return appError {
173+ "Failed to create bookmark" ,
174+ "" ,
175+ }
156176 }
157177 return nil
158178}
159179
160- func (c * context ) OpenInFileBrowser (path string ) error {
180+ func (c * context ) OpenInFileBrowser (path string ) Error {
161181 cmd := exec .Command ("open" , path )
162- return cmd .Run ()
182+ err := cmd .Run ()
183+ if err != nil {
184+ return appError {
185+ "Failed to open file browser" ,
186+ err .Error (),
187+ }
188+ }
189+ return nil
163190}
164191
165- func (c * context ) AppendTemplateToFile (filePath string , templateName string ) error {
192+ func (c * context ) AppendTemplateToFile (filePath string , templateName string ) Error {
166193 location := c .KlogFolder () + templateName + ".template.klg"
167194 template , err := readFile (location )
168195 if err != nil {
169- return errors .New ("No such template: " + location )
196+ return appError {
197+ "No such template" ,
198+ "There is no template at location " + location ,
199+ }
170200 }
171- instance , err := service .RenderTemplate (template , time .Now ())
172- if err != nil {
173- return err
201+ instance , tErr := service .RenderTemplate (template , time .Now ())
202+ if tErr != nil {
203+ return appError {
204+ "Invalid template" ,
205+ tErr .Error (),
206+ }
174207 }
175208 contents , err := readFile (filePath )
176209 if err != nil {
177210 return err
178211 }
179- err = appendToFile (filePath , service .AppendableText (contents , instance ))
180- return err
212+ return appendToFile (filePath , service .AppendableText (contents , instance ))
181213}
182214
183- func readFile (path string ) (string , error ) {
215+ func readFile (path string ) (string , Error ) {
184216 contents , err := ioutil .ReadFile (path )
185217 if err != nil {
186- return "" , errors .New ("Cannot read file: " + path )
218+ return "" , appError {
219+ "Cannot read file" ,
220+ "Location: " + path ,
221+ }
187222 }
188223 return string (contents ), nil
189224}
190225
191- func appendToFile (path string , textToAppend string ) error {
226+ func appendToFile (path string , textToAppend string ) Error {
192227 file , err := os .OpenFile (path , os .O_APPEND | os .O_WRONLY , 0644 )
193228 if err != nil {
194- return errors .New ("Cannot write to file: " + path )
229+ return appError {
230+ "Cannot write to file" ,
231+ "Location: " + path ,
232+ }
195233 }
196234 defer file .Close ()
197235 if _ , err := file .WriteString (textToAppend ); err != nil {
198- return errors .New ("Cannot write to file: " + path )
236+ return appError {
237+ "Cannot write to file" ,
238+ "Location: " + path ,
239+ }
199240 }
200241 return nil
201242}
0 commit comments