-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (97 loc) · 2.24 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
const (
APP_NAME = "brain_dump"
APP_CONFIG_FILE = "$HOME/.config/brain_dump/brain_dump.json"
)
func main() {
var err error
parseFlags()
if printConfigFlag {
prettyPrint(getDefaultConfig())
return
}
config := getConfig()
profile := config.getProfile(profileNameFlag)
if profile.File == "" {
log.Fatal("The 'file' configuration key must be set !")
return
}
timeVars := setTimeVars(profile.Formats)
cliArgs, cliVars := extractCliVars()
inpText := ""
if len(cliArgs) > 0 {
inpText = getInput(cliArgs)
}
if len(inpText) == 0 {
openEditorFlag = true
}
cliVars["input"] = inpText
context := makeContext()
context = mergeContextMaps(context, timeVars, profile.TemplateVars, cliVars)
context = convertContextKeys(context, profile.KeysCase)
if profile.TemplateFile != "" {
inpText = execFileTemplate(profile.TemplateFile, context)
}
if !strings.HasSuffix(inpText, "\n") {
inpText += "\n"
}
outFile := expandText(profile.File)
outFile = execTemplate(outFile, context)
ensureParentDirs(outFile)
debugLevel, err := strconv.Atoi(os.Getenv("BRAINDUMP_DEBUG"))
if err == nil && !openEditorFlag && debugLevel > 1 {
fmt.Println("Profile :")
prettyPrint(profile)
fmt.Println("Context :")
prettyPrint(context)
}
if err == nil && !openEditorFlag && debugLevel > 0 {
fmt.Println("Output file :", outFile)
fmt.Println(strings.Repeat("-", 55))
fmt.Println(inpText)
}
if profile.WriteMode == "write" {
err = writeFile(outFile, inpText)
} else {
err = appendFile(outFile, inpText)
}
if err != nil {
log.Fatal(err)
}
if !openEditorFlag {
return
}
if profile.Editor == "" {
log.Fatal("The 'editor' configuration key must be set to edit files")
return
}
err = editFile(profile.Editor, profile.EditorArgs, outFile)
if err != nil {
log.Fatal(err)
}
}
func getInput(cliArgs []string) string {
var data string
if cliArgs[0] == "-" {
data = string(readStdin())
} else {
data = strings.Join(cliArgs, " ")
}
return data
}
func setTimeVars(formats map[string]string) map[string]string {
timeVars := make(map[string]string)
now := time.Now()
for name, format := range formats {
timeVars[name] = now.Format(format)
}
return timeVars
}