-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (75 loc) · 1.58 KB
/
main.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
package main
import (
"fmt"
"io"
"log"
"os"
"strings"
"github.com/c-bata/go-prompt"
)
func main() {
if len(os.Args) > 1 {
getKeysFromArgs()
} else {
getKeysFromPrompt()
}
}
func getKeysFromArgs() {
var (
params = strings.Join(os.Args[1:], " ")
files = ParseInput(params)
)
for _, key := range files.Keys() {
filesToOutput := GetTemplate(key).Files
for _, file := range filesToOutput {
content, err := templates.ReadFile(file)
if err != nil {
log.Println(err.Error())
continue
}
fmt.Println(string(content))
}
}
}
func getKeysFromPrompt() {
files := NewKeySet()
fmt.Println("Please select language or platform:")
for {
var (
prefix = fmt.Sprintf("[%s] > ", strings.Join(files.Strings(), ", "))
input = prompt.Input(prefix, completer)
)
if strings.TrimSpace(input) == "" {
break
}
newFiles := ParseInput(input)
files.Merge(newFiles)
}
if files.IsEmpty() {
os.Exit(0)
}
var (
filenameInput = prompt.Input("Output file to append: ", nilCompleter)
outputFile, err = os.OpenFile(filenameInput, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
defer outputFile.Close()
for _, templateFileKeys := range files.Keys() {
filesForKey := GetTemplate(templateFileKeys).Files
for _, templateFile := range filesForKey {
file, err := templates.Open(templateFile)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
defer file.Close()
if _, err := io.Copy(outputFile, file); err != nil {
log.Println(err.Error())
os.Exit(1)
}
}
}
}