2024-08-04 23:16:05 +07:00
|
|
|
package main
|
|
|
|
|
2024-08-06 22:13:37 +07:00
|
|
|
import (
|
|
|
|
"context"
|
2024-08-11 19:50:43 +07:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
2024-08-06 22:13:37 +07:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2024-08-11 19:50:43 +07:00
|
|
|
"github.com/adrg/xdg"
|
|
|
|
"github.com/joho/godotenv"
|
2024-08-06 22:13:37 +07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/tigorlazuardi/bluemage/go/cmd/bluemage/serve"
|
2024-08-11 19:50:43 +07:00
|
|
|
"github.com/tigorlazuardi/bluemage/go/config"
|
2024-08-06 22:13:37 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
var Cmd = &cobra.Command{
|
|
|
|
Use: "bluemage",
|
|
|
|
Short: "Bluemage is a reddit image downloader",
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Cmd.AddCommand(serve.Cmd)
|
2024-08-11 19:50:43 +07:00
|
|
|
|
|
|
|
flags := Cmd.PersistentFlags()
|
|
|
|
for _, entry := range config.DefaultConfig {
|
|
|
|
key, value, desc, hidden := entry.Key, entry.Value, entry.Desc, entry.Hidden
|
|
|
|
switch v := value.(type) {
|
|
|
|
case bool:
|
|
|
|
flags.Bool(key, v, desc)
|
|
|
|
case string:
|
|
|
|
flags.String(key, v, desc)
|
|
|
|
case int:
|
|
|
|
flags.Int(key, v, desc)
|
|
|
|
case float32:
|
|
|
|
flags.Float32(key, v, desc)
|
|
|
|
case float64:
|
|
|
|
flags.Float64(key, v, desc)
|
2024-08-27 09:10:58 +07:00
|
|
|
case []string:
|
|
|
|
flags.StringSlice(key, v, desc)
|
2024-08-11 19:50:43 +07:00
|
|
|
default:
|
|
|
|
flags.String(key, fmt.Sprintf("%v", v), desc)
|
|
|
|
}
|
|
|
|
if hidden {
|
|
|
|
flags.MarkHidden(key)
|
|
|
|
}
|
|
|
|
}
|
2024-08-06 22:13:37 +07:00
|
|
|
}
|
2024-08-05 23:06:32 +07:00
|
|
|
|
|
|
|
func main() {
|
2024-08-11 19:50:43 +07:00
|
|
|
_ = godotenv.Load()
|
|
|
|
|
|
|
|
xdgJson, _ := xdg.ConfigFile("bluemage/config.json")
|
|
|
|
xdgYaml, _ := xdg.ConfigFile("bluemage/config.yaml")
|
|
|
|
|
|
|
|
cfg, err := config.NewConfigBuilder().
|
|
|
|
LoadDefault().
|
|
|
|
LoadJSONFile("/etc/bluemage/config.json").
|
|
|
|
LoadYamlFile("/etc/bluemage/config.yaml").
|
|
|
|
LoadJSONFile(xdgJson).
|
|
|
|
LoadYamlFile(xdgYaml).
|
|
|
|
LoadJSONFile("config.json").
|
|
|
|
LoadYamlFile("config.yaml").
|
|
|
|
LoadEnv().
|
|
|
|
LoadFlags(Cmd.PersistentFlags()).
|
|
|
|
BuildHandle()
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("BLUEMAGE: failed to load config", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-08-06 22:13:37 +07:00
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
2024-08-11 19:50:43 +07:00
|
|
|
ctx = config.WithContext(ctx, cfg)
|
2024-08-06 22:13:37 +07:00
|
|
|
if err := Cmd.ExecuteContext(ctx); err != nil {
|
2024-08-27 09:10:58 +07:00
|
|
|
slog.ErrorContext(ctx, err.Error(), "error", err)
|
2024-08-06 22:13:37 +07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-08-05 23:06:32 +07:00
|
|
|
}
|