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 (
|
2023-08-21 20:38:56 +00:00
|
|
|
"errors"
|
2023-05-31 18:01:24 +00:00
|
|
|
"log"
|
2023-08-21 20:38:56 +00:00
|
|
|
"time"
|
2022-09-08 15:12:06 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-08-21 20:38:56 +00:00
|
|
|
var (
|
|
|
|
ErrIncorrectRefreshInterval = errors.New("refresh interval must be a duration string >= 500ms")
|
|
|
|
)
|
|
|
|
|
2023-06-03 20:51:08 +00:00
|
|
|
const (
|
2023-08-21 20:38:56 +00:00
|
|
|
Version string = "0.57.2"
|
2023-06-03 20:51:08 +00:00
|
|
|
)
|
|
|
|
|
2023-01-27 19:17:13 +00:00
|
|
|
var (
|
2023-08-02 18:29:37 +00:00
|
|
|
bind string
|
|
|
|
cache bool
|
|
|
|
cacheFile string
|
|
|
|
debug bool
|
|
|
|
filtering bool
|
|
|
|
maximumFileCount uint32
|
|
|
|
minimumFileCount uint32
|
|
|
|
port uint16
|
|
|
|
recursive bool
|
2023-08-21 20:38:56 +00:00
|
|
|
refreshInterval string
|
2023-08-02 18:29:37 +00:00
|
|
|
sorting bool
|
|
|
|
statistics bool
|
|
|
|
statisticsFile string
|
|
|
|
verbose bool
|
|
|
|
version bool
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2023-01-27 19:17:13 +00:00
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "roulette <path> [path]...",
|
|
|
|
Short: "Serves random images from the specified directories.",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
2023-05-08 16:16:19 +00:00
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
if debug {
|
|
|
|
cmd.MarkFlagRequired("cache")
|
|
|
|
}
|
2023-08-21 20:38:56 +00:00
|
|
|
|
|
|
|
if refreshInterval != "" {
|
|
|
|
interval, err := time.ParseDuration(refreshInterval)
|
|
|
|
if err != nil || interval < 500*time.Millisecond {
|
|
|
|
log.Fatal(ErrIncorrectRefreshInterval)
|
|
|
|
}
|
|
|
|
}
|
2023-05-08 16:16:19 +00:00
|
|
|
},
|
2023-01-27 19:20:07 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-01-27 19:17:13 +00:00
|
|
|
err := ServePage(args)
|
|
|
|
if err != nil {
|
2023-01-27 19:20:07 +00:00
|
|
|
return err
|
2023-01-27 19:17:13 +00:00
|
|
|
}
|
2023-01-27 19:20:07 +00:00
|
|
|
|
|
|
|
return nil
|
2023-01-27 19:17:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2022-09-08 15:12:06 +00:00
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
2023-05-31 18:01:24 +00:00
|
|
|
log.Fatal(err)
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-05-08 16:45:57 +00:00
|
|
|
rootCmd.Flags().StringVarP(&bind, "bind", "b", "0.0.0.0", "address to bind to")
|
2023-01-27 19:17:13 +00:00
|
|
|
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")
|
2023-04-28 18:31:07 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "expose debug endpoint")
|
2023-01-27 19:17:13 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&filtering, "filter", "f", false, "enable filtering")
|
2023-08-13 22:43:35 +00:00
|
|
|
rootCmd.Flags().Uint32Var(&maximumFileCount, "maximum-files", 1<<32-1, "skip directories with file counts over this value")
|
2023-08-02 18:29:37 +00:00
|
|
|
rootCmd.Flags().Uint32Var(&minimumFileCount, "minimum-files", 0, "skip directories with file counts under this value")
|
2023-01-27 19:17:13 +00:00
|
|
|
rootCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "port to listen on")
|
|
|
|
rootCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "recurse into subdirectories")
|
2023-08-21 20:38:56 +00:00
|
|
|
rootCmd.Flags().StringVar(&refreshInterval, "refresh-interval", "", "force refresh interval equal to this duration (minimum 500ms)")
|
2023-01-27 19:17:13 +00:00
|
|
|
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")
|
2023-01-27 19:17:13 +00:00
|
|
|
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")
|
2022-09-10 21:17:55 +00:00
|
|
|
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
|
|
|
}
|