-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydra.go
225 lines (187 loc) · 5 KB
/
hydra.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/sudosays/hydra/pkg/data/hugo"
"io/ioutil"
"math"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"
)
type HydraConfig struct {
Extension string `json:"extension"`
Editor EditorCommand `json:"editor"`
Sites []HugoSite `json:"sites"`
}
// HugoSite contains the information for a hugo site listed in the config
type HugoSite struct {
Name string `json:"name"`
Path string `json:"path"`
}
type EditorCommand struct {
Command string `json:"command"`
Args string `json:"args"`
}
func check(e error) {
if e != nil {
panic(e)
}
}
var config HydraConfig
var currentPageIndex int = 1 // For pagination purposes
var numPages int = 0
const maxItemsPerPage int = 10
func init() {
userHomePath, err := os.UserHomeDir()
defaultConfigFilePath := path.Join(userHomePath, ".config", "hydra.json")
// Parse command line flags if any
configFilePath := flag.String("config", defaultConfigFilePath, "Path to a config file")
config, err = readConfig(*configFilePath)
check(err)
}
func main() {
clearTerm()
// Setup to parse args
blog := hugo.Load(config.Sites[0].Path)
// Calculate the number of pages
numPages = int(math.Ceil(float64(len(blog.Posts)) / float64(maxItemsPerPage)))
// main REPL
for {
printPostList(blog)
command := promptUser("\nWhat would you like to do?\n" +
"Commands: [a]dd [e]dit [d]elete [n]ext/[p]rev page [q]uit\n> ")
blog = parseCommand(command, blog)
clearTerm()
}
}
func readConfig(path string) (HydraConfig, error) {
conf := HydraConfig{}
configFile, err := os.Open(path)
check(err)
byteValue, err := ioutil.ReadAll(configFile)
check(err)
//fmt.Printf("%s", byteValue)
err = json.Unmarshal(byteValue, &conf)
check(err)
//fmt.Printf("Config is: %+v\n", conf)
configFile.Close()
return conf, nil
}
func startEditor(path string) {
editorCmd := exec.Command(config.Editor.Command, config.Editor.Args, path)
editorCmd.Stdin = os.Stdin
editorCmd.Stdout = os.Stdout
err := editorCmd.Run()
check(err)
}
func genPostList(blog hugo.Blog) ([]string, [][]string) {
posts := blog.Posts
headings := []string{"#", "Date", "Draft", "Title"}
var postList [][]string
for i, post := range posts {
draftStatus := "False"
if post.Draft {
draftStatus = "True"
}
datetime, _ := time.Parse(time.RFC3339, post.Date)
date := datetime.Format("2006/01/02")
postList = append(
postList,
[]string{fmt.Sprintf("%d", i+1), date, draftStatus, post.Title})
}
return headings, postList
}
func parseCommand(cmd string, blog hugo.Blog) hugo.Blog {
parts := strings.Split(cmd, " ")
switch parts[0][0] {
case 'e':
index := -1
if len(parts) > 1 {
// startEditor
i, err := strconv.Atoi(strings.Trim(parts[1], "\n\r "))
check(err)
index = i
} else {
i, err := strconv.Atoi(strings.Trim(promptUser("Enter a post number to edit:\n> "), "\n\r "))
check(err)
index = i
}
if index > 0 && index < len(blog.Posts) {
startEditor(blog.Posts[index-1].Path)
}
case 'a':
title := ""
if len(parts) > 1 {
// Call creation of new post with title given
title = strings.Join(parts[1:], " ")
} else {
title = promptUser("Please enter a title for the post:\n> ")
}
fmt.Println("Attempting to create post with title: %s", title)
postPath := blog.NewPost(title, config.Extension)
startEditor(postPath)
case 'd':
// Ask for confirmation first
// Possibly: add a hugo 'trash' folder in future for soft deletion.
i, err := strconv.Atoi(strings.Trim(parts[1], "\n\r "))
check(err)
post := blog.Posts[i-1]
warn := "WARNING: You are about to delete the post titled '%s'.\nThis action is irreversible, especially if the post has not been synchronised with git.\nProceed? [y/N] Default: N.\n> "
ans := promptUser(fmt.Sprintf(warn, post.Title))
if ans[0] == 'y' {
blog.DeletePost(post.Path)
}
case 'n':
if currentPageIndex < numPages {
currentPageIndex++
}
case 'p':
if currentPageIndex > 1 {
currentPageIndex--
}
case 'q':
clearTerm()
fmt.Println("You have slain the hydra...")
os.Exit(0)
}
return blog
}
func promptUser(prompt string) string {
fmt.Print(prompt)
input := bufio.NewReader(os.Stdin)
ans, err := input.ReadString('\n')
check(err)
return ans
}
func printPostList(blog hugo.Blog) {
// We might want to paginate the number of blog posts
// Currently we will set the max to 10 posts per page
header, list := genPostList(blog)
for _, col := range header {
fmt.Print(col + "\t")
}
fmt.Println()
startPostIndex := (currentPageIndex - 1) * maxItemsPerPage
endPostIndex := startPostIndex + maxItemsPerPage // Possibly longer than len(list)
if endPostIndex > len(list) {
endPostIndex = len(list)
}
pageList := list[startPostIndex:endPostIndex]
for _, post := range pageList {
for _, col := range post {
fmt.Print(col + "\t")
}
fmt.Print("\n")
}
fmt.Printf("Showing [%d-%d]", startPostIndex+1, endPostIndex)
fmt.Printf(" | Page %d of %d\n", currentPageIndex, numPages)
}
func clearTerm() {
fmt.Print("\033[H\033[2J")
}