-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathglee.go
105 lines (88 loc) · 2.2 KB
/
glee.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
package main
import (
"fmt"
"os"
"strings"
"github.com/ghodss/yaml"
"github.com/jessevdk/go-flags"
"github.com/pelletier/go-toml"
"github.com/sirupsen/logrus"
)
var config *toml.Tree
var log = logrus.New()
var filePath string
var opts struct {
ShowConfig bool `short:"c" long:"config" description:"Show glee configuration global file"`
Debug bool `short:"d" long:"debug" description:"debug mode"`
File string `description:"Markdown file to process"`
Help bool `short:"h" long:"help" description:"Show this help message"`
Version bool `short:"v" long:"version" description:"Show version"`
DownloadImage bool `short:"i" long:"download-image" description:"Download images from markdown file"`
}
var flagParser = flags.NewParser(&opts, flags.Default)
var version string
func initializeLogger() {
log := logrus.New()
log.SetFormatter(&logrus.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
}
func setupLogLevel() {
if opts.Debug {
log.SetLevel(logrus.DebugLevel)
} else {
log.SetLevel(logrus.InfoLevel)
}
}
func parseArgs() {
if len(version) == 0 {
version = "vUnset"
}
if opts.Version {
fmt.Println(version)
os.Exit(0)
}
if opts.Help {
flagParser.WriteHelp(os.Stdout)
os.Exit(0)
}
}
func main() {
initializeLogger()
flagParser.Usage = "Usage: glee <markdown_file_path>"
args, err := flagParser.Parse()
if err != nil {
log.Fatal(err)
}
parseArgs()
setupLogLevel()
loadGlobalConfig()
if opts.ShowConfig {
printConfiguration()
return
}
checkConfigurationsExist(log)
if len(args) == 1 {
filePath = args[0]
content, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Failed to read file %s: %v", filePath, err)
}
contentStr := string(content)
index := strings.Index(contentStr, "\n---\n")
if index == -1 {
log.Fatal("Could not find YAML delimiter")
}
yamlFrontMatter := contentStr[:index]
markdownContent := contentStr[index+len("\n---\n"):]
var metadata map[string]interface{}
err = yaml.Unmarshal([]byte(yamlFrontMatter), &metadata)
if err != nil {
log.Fatalf("Failed to parse YAML: %v", err)
}
postToGhost(metadata, markdownContent)
} else {
log.Fatal("Please provide a markdown file.")
}
}