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 (
|
2022-10-20 01:07:20 +00:00
|
|
|
"fmt"
|
2022-09-10 21:35:05 +00:00
|
|
|
"os"
|
2022-09-08 15:12:06 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-10-20 19:22:01 +00:00
|
|
|
type MaxConcurrency int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// avoid hitting default open file descriptor limits (1024)
|
|
|
|
maxDirectoryScans MaxConcurrency = 32
|
|
|
|
maxFileScans MaxConcurrency = 256
|
|
|
|
)
|
|
|
|
|
2022-10-20 19:50:42 +00:00
|
|
|
type Concurrency struct {
|
|
|
|
DirectoryScans chan int
|
|
|
|
FileScans chan int
|
|
|
|
}
|
|
|
|
|
2022-10-28 22:19:04 +00:00
|
|
|
var Cache bool
|
2022-10-20 01:37:12 +00:00
|
|
|
var Filter bool
|
2022-10-18 21:54:01 +00:00
|
|
|
var Port uint16
|
2022-09-08 15:12:06 +00:00
|
|
|
var Recursive bool
|
2022-10-20 01:37:12 +00:00
|
|
|
var Sort bool
|
2022-09-09 00:02:33 +00:00
|
|
|
var Verbose bool
|
2022-09-08 15:12:06 +00:00
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
2022-09-16 19:57:40 +00:00
|
|
|
Use: "roulette <path> [path2]...",
|
2022-09-08 15:12:06 +00:00
|
|
|
Short: "Serves random images from the specified directories.",
|
2022-09-08 15:57:59 +00:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
2022-09-08 15:12:06 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-10-20 01:07:20 +00:00
|
|
|
err := ServePage(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
2022-09-10 21:35:05 +00:00
|
|
|
os.Exit(1)
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-01-18 17:50:29 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Cache, "cache", "c", false, "only scan directories once, at startup (or when filters are applied)")
|
|
|
|
rootCmd.Flags().BoolVarP(&Filter, "filter", "f", false, "enable filtering via query parameters")
|
2022-10-18 21:54:01 +00:00
|
|
|
rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on")
|
2022-09-08 15:12:06 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")
|
2022-10-20 01:37:12 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Sort, "sort", "s", false, "enable sorting via query parameters")
|
2022-09-10 21:17:55 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Verbose, "verbose", "v", false, "log accessed files to stdout")
|
|
|
|
rootCmd.Flags().SetInterspersed(true)
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|