-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
81 lines (70 loc) · 1.46 KB
/
page.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
)
type Page struct {
Filepath string
Content string
Comment string
}
func NewHomePage(filepath, content, comment string) *Page {
return &Page{
Filepath: filepath,
Content: content,
Comment: comment,
}
}
func (p *Page) Html() string {
return ""
}
func (p *Page) Save(cfg *Config) error {
if os.MkdirAll(p.Dir(cfg), 0777) != nil {
return errors.New("Unable to create directory for wiki")
}
var err error
_, err = os.Stat(p.Path(cfg))
var fileOut *os.File
// create file if not exists
if os.IsNotExist(err) {
fileOut, err = os.Create(p.Path(cfg))
if err != nil {
return err
}
fmt.Println("Successfully created file")
fileOut.Close()
} else if err != nil {
return err
}
content := []byte(p.Content)
err = ioutil.WriteFile(p.Path(cfg), content, 0644)
if err != nil {
return err
}
err = gitCommit(p, false)
if err != nil {
return err
}
log.Println("written Successfully!!!")
return nil
}
func (p *Page) Title() string {
return path.Base(p.Filepath)
}
func (p *Page) Dir(cfg *Config) string {
return filepath.Join(cfg.RepositoryRoot, path.Dir(p.Filepath))
}
func (p *Page) FileName() string {
return fmt.Sprintf("%s", titleToFileName(p.Title()))
}
func (p *Page) Path(cfg *Config) string {
return filepath.Join(p.Dir(cfg), p.FileName())
}
func (p *Page) RelativePath() string {
return filepath.Join(path.Dir(p.Filepath), p.FileName())
}