-
Notifications
You must be signed in to change notification settings - Fork 1
/
editGist.go
88 lines (77 loc) · 2.12 KB
/
editGist.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
package main
import (
"strings"
"github.com/google/go-github/github"
)
var gistTemplate = []byte(`# GIST FORM: Edit Metadata below
# ==============================
# description: {{ .Description }}
# starred: {{ .Starred }}
# public: {{ .Public }}
# ==============================
{{- range $elements := .Files }}
{{ fname_line (Deref $elements.Filename) }}::>>>
{{ (Deref $elements.Content ) -}}
{{end}}`)
func cleanLine(s string) string {
return strings.Trim(strings.Split(s, ":")[1], " ")
}
func appendFile(items map[github.GistFilename]github.GistFile, filename string, fileContent string) {
if filename != "" && fileContent != "" {
items[github.GistFilename(filename)] = github.GistFile{
Filename: &filename,
Content: &fileContent,
}
}
}
func parseGistTemplate(s string) (github.Gist, bool, error) {
// Parses GistTemplate and returns
// a gist object
result := strings.Split(s, "\n")
nlines := len(result)
var filename string
var fileContent string
var description string
var starred bool
var public bool
var err error
var items map[github.GistFilename]github.GistFile
items = make(map[github.GistFilename]github.GistFile, 1)
for idx, line := range result {
if idx <= 5 {
switch {
case strings.HasPrefix(line, "# description:"):
description = cleanLine(line)
case strings.HasPrefix(line, "# starred:"):
starred, err = parseTrueFalse(cleanLine(line))
case strings.HasPrefix(line, "# public:"):
public, err = parseTrueFalse(cleanLine(line))
}
if err != nil {
return github.Gist{}, false, err
}
}
if idx > 5 {
switch {
case strings.HasSuffix(line, "::>>>"):
// Store content
appendFile(items, filename, strings.TrimSuffix(fileContent, "\n"))
// Initate new file
fileContent = ""
filename = strings.Trim(line[0:len(line)-5], "-")
default:
fileContent += line + "\n"
}
}
if idx == nlines-1 {
appendFile(items, filename, strings.TrimSuffix(fileContent, "\n"))
}
}
// Need to handle starring manually
var resultGist = github.Gist{
Description: &description,
Public: &public,
Files: items,
}
return resultGist, starred, nil
}