-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (129 loc) · 4.48 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
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
package main
import (
"io/fs"
"strings"
"github.com/charmbracelet/log"
"github.com/slashtechno/cross-blogger/cmd"
"github.com/slashtechno/cross-blogger/internal"
"github.com/slashtechno/cross-blogger/internal/platforms"
"github.com/spf13/cobra"
"github.com/subosito/gotenv"
)
func init() {
cobra.OnInitialize(initConfig)
// Load a .env file if it exists
gotenv.Load()
}
func initConfig() {
// Tell Viper to use the prefix "CROSS_BLOGGER" for environment variables
internal.ConfigViper.SetEnvPrefix("CROSS_BLOGGER")
internal.CredentialViper.SetEnvPrefix("CROSS_BLOGGER")
// Set automatic env to true
internal.ConfigViper.AutomaticEnv()
internal.CredentialViper.AutomaticEnv()
// log.Debug(cfgFile)
if cmd.CredentialFile != "" {
// Use config file from the flag.
internal.CredentialViper.SetConfigFile(cmd.CredentialFile)
} else {
// Use config.yaml in the current working directory.
internal.CredentialViper.SetConfigFile("credentials.yaml")
}
if err := internal.CredentialViper.ReadInConfig(); err == nil {
log.Debug("", "credential file:", internal.CredentialViper.ConfigFileUsed())
} else {
// Generate a default .env file null values
if _, ok := err.(*fs.PathError); ok {
log.Debug("Credential file not found, creating a new one")
internal.CredentialViper.SetDefault("google_client_id", "")
internal.CredentialViper.SetDefault("google_client_secret", "")
// LLM stuff
internal.CredentialViper.SetDefault("llm_provider", "")
internal.CredentialViper.SetDefault("llm_api_key", "")
internal.CredentialViper.SetDefault("llm_base_url", "")
internal.CredentialViper.SetDefault("llm_model", "")
// db stuff
// internal.CredentialViper.SetDefault("db", map[string]interface{}{
// "enable": false,
// "url": "redis://<user>:<pass>@localhost:6379/0",
// "db": 0,
// })
if err := internal.CredentialViper.WriteConfigAs(cmd.CredentialFile); err != nil {
log.Fatal("Failed to write credential file:", err)
}
} else {
log.Fatal("Failed to read credential file:", err)
}
}
if cmd.ConfigFile != "" {
// Use config file from the flag.
internal.ConfigViper.SetConfigFile(cmd.ConfigFile)
} else {
// Use config.yaml in the current working directory.
internal.ConfigViper.SetConfigFile("./config.toml")
}
if err := internal.ConfigViper.ReadInConfig(); err == nil {
log.Debug("", "config file:", internal.ConfigViper.ConfigFileUsed())
} else {
// If the config file is not found, create a file, write the default values and exit
// Since viper.ConfigFileNotFoundError doesn't always work, also use fs.PathError
if _, ok := err.(*fs.PathError); ok {
log.Debug("Config file not found, creating a new one")
// Destinations
internal.ConfigViper.SetDefault("destinations", []map[string]interface{}{
{
"name": "blog",
"type": "blogger",
"blog_url": "https://example.com",
"overwrite": false,
},
{
"name": "otherblog",
"type": "markdown",
"content_dir": "/hugo-site/content/blog",
"git_dir": "/hugo-site",
"frontmatter_mapping": platforms.FrontMatterMappings,
"overwrite": false,
},
})
// Sources
internal.ConfigViper.SetDefault("sources", []map[string]interface{}{
{
"name": "someblog",
"type": "blogger",
"blog_url": "https://example.com",
"generate_llm_descriptions": true,
"category_prefix": "category::",
},
{
"name": "aBlogInMarkdown",
"type": "markdown",
"content_dir": "content",
"frontmatter_mapping": platforms.FrontMatterMappings,
},
})
if err := internal.ConfigViper.WriteConfigAs(cmd.ConfigFile); err != nil {
log.Fatal("Failed to write config file:", err)
}
log.Fatal("Failed to read config file. Created a config file with default values. Please edit the file and run the command again.", "path", cmd.ConfigFile)
} else {
log.Fatal("Failed to read config file:", err)
}
}
}
func main() {
switch strings.ToLower(internal.ConfigViper.GetString("log_level")) {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.InfoLevel)
log.Info("Invalid log level passed, using InfoLevel", "passed", internal.ConfigViper.GetString("log_level"))
}
cmd.Execute()
}