roulette/cmd/root.go

83 lines
2.2 KiB
Go
Raw Normal View History

2022-09-08 15:12:06 +00:00
/*
2023-01-18 17:19:29 +00:00
Copyright © 2023 Seednode <seednode@seedno.de>
2022-09-08 15:12:06 +00:00
*/
package cmd
import (
"log"
2022-09-08 15:12:06 +00:00
"github.com/spf13/cobra"
)
2023-06-03 20:51:08 +00:00
const (
2023-06-09 20:56:03 +00:00
Version string = "0.51.4"
2023-06-03 20:51:08 +00:00
)
var (
bind string
2023-02-08 13:50:40 +00:00
cache bool
cacheFile string
debug bool
2023-02-08 13:50:40 +00:00
filtering bool
port uint16
recursive bool
sorting bool
statistics bool
statisticsFile string
verbose bool
2023-05-12 04:22:31 +00:00
version bool
2022-09-08 15:12:06 +00:00
rootCmd = &cobra.Command{
Use: "roulette <path> [path]...",
Short: "Serves random images from the specified directories.",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
if debug {
cmd.MarkFlagRequired("cache")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
err := ServePage(args)
if err != nil {
return err
}
return nil
},
}
)
2022-09-08 15:12:06 +00:00
func Execute() {
err := rootCmd.Execute()
if err != nil {
log.Fatal(err)
2022-09-08 15:12:06 +00:00
}
}
func init() {
rootCmd.Flags().StringVarP(&bind, "bind", "b", "0.0.0.0", "address to bind to")
rootCmd.Flags().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup")
2023-02-05 20:34:22 +00:00
rootCmd.Flags().StringVar(&cacheFile, "cache-file", "", "path to optional persistent cache file")
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "expose debug endpoint")
rootCmd.Flags().BoolVarP(&filtering, "filter", "f", false, "enable filtering")
rootCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "port to listen on")
rootCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "recurse into subdirectories")
rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting")
2023-02-05 20:34:22 +00:00
rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint")
2023-02-08 13:50:40 +00:00
rootCmd.Flags().StringVar(&statisticsFile, "stats-file", "", "path to optional persistent stats file")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "log accessed files to stdout")
2023-05-12 04:22:31 +00:00
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit")
rootCmd.Flags().SetInterspersed(true)
2023-05-12 04:22:31 +00:00
rootCmd.CompletionOptions.HiddenDefaultCmd = true
rootCmd.SilenceErrors = true
rootCmd.SetHelpCommand(&cobra.Command{
Hidden: true,
})
rootCmd.SetVersionTemplate("roulette v{{.Version}}\n")
2023-06-03 20:51:08 +00:00
rootCmd.Version = Version
2022-09-08 15:12:06 +00:00
}