1
0
mirror of https://github.com/taigrr/pastebin synced 2026-04-17 14:44:57 -07:00

refactor(config): migrate to jety for config parsing

This commit is contained in:
2026-03-09 05:58:57 +00:00
parent 46562efafb
commit ed52f9102a
5 changed files with 212 additions and 12 deletions

31
main.go
View File

@@ -4,23 +4,37 @@ import (
"context"
"fmt"
"os"
"time"
"github.com/charmbracelet/fang"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/taigrr/jety"
)
// version is set at build time via ldflags.
var version = "dev"
func main() {
var cfg Config
var configFile string
InitConfig()
rootCmd := &cobra.Command{
Use: "pastebin",
Short: "A self-hosted ephemeral pastebin service",
Long: "pastebin is a self-hosted pastebin web app that lets you create and share ephemeral data between devices and users.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := ReadConfigFile(configFile); err != nil {
return fmt.Errorf("loading config: %w", err)
}
// Override jety values with any flags explicitly set on the command line.
cmd.Flags().Visit(func(flag *pflag.Flag) {
jety.SetString(flag.Name, flag.Value.String())
})
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cfg := LoadConfig()
if cfg.Expiry.Seconds() < 60 {
return fmt.Errorf("expiry of %s is too small (minimum 1m)", cfg.Expiry)
}
@@ -29,17 +43,12 @@ func main() {
},
}
rootCmd.Flags().StringVar(&cfg.Bind, "bind", defaultBind, "address and port to bind to")
rootCmd.Flags().StringVar(&cfg.FQDN, "fqdn", defaultFQDN, "FQDN for public access")
rootCmd.Flags().DurationVar(&cfg.Expiry, "expiry", defaultExpiry, "expiry time for pastes")
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "path to config file (JSON, TOML, or YAML)")
rootCmd.Flags().String(configKeyBind, defaultBind, "address and port to bind to")
rootCmd.Flags().String(configKeyFQDN, defaultFQDN, "FQDN for public access")
rootCmd.Flags().Duration(configKeyExpiry, defaultExpiry, "expiry time for pastes")
if err := fang.Execute(context.Background(), rootCmd, fang.WithVersion(version)); err != nil {
os.Exit(1)
}
}
const (
defaultBind = "0.0.0.0:8000"
defaultFQDN = "localhost"
defaultExpiry = 5 * time.Minute
)