-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
55 lines (48 loc) · 923 Bytes
/
file.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
45
46
47
48
49
50
51
52
53
54
55
package lib
import (
"fmt"
"io/ioutil"
"os"
"path"
)
const (
Markdown = ".md"
Gocode = ".go"
)
type File struct {
Name string
Path string
Format string
HasFormat bool
}
// Returns absolute file name
func (self *File) AbsoluteName() string {
if len(self.Path) == 0 {
self.Path, _ = self.Pwd()
}
return fmt.Sprintf("%v/%v", self.Path, self.Name)
}
// Gets current directory
func (self *File) Pwd() (path string, err error) {
path, err = os.Getwd()
return
}
// Read file and set Format field
func (f *File) Read() (data []byte, err error) {
fmt.Printf("Path: %v \n", f.AbsoluteName())
if data, err = ioutil.ReadFile(f.AbsoluteName()); err != nil {
return
}
switch extension := path.Ext(f.Name); extension {
case Markdown:
f.Format = "slide"
f.HasFormat = true
case Gocode:
f.Format = "code"
f.HasFormat = true
default:
f.Format = ""
f.HasFormat = false
}
return
}