photoprism/internal/config/cli_flags.go
Michael Mayer e739dd3e89 Auth: Add support for config and routing extensions (WIP) #782 #2478
Enables developers to extend the existing config and API. Initial proof
of concept and work in progress. Implementation details may change.
Feedback welcome!

Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-07-05 23:13:34 +02:00

82 lines
1.6 KiB
Go

package config
import (
"github.com/urfave/cli"
"github.com/photoprism/photoprism/pkg/list"
)
// CliFlags represents a list of command-line parameters.
type CliFlags []CliFlag
// Cli returns the currently active command-line parameters.
func (f CliFlags) Cli() (result []cli.Flag) {
result = make([]cli.Flag, 0, len(f))
for _, flag := range f {
result = append(result, flag.Flag)
}
return result
}
// Find finds command-line parameters based on a list of tags.
func (f CliFlags) Find(tags []string) (result []cli.Flag) {
result = make([]cli.Flag, 0, len(f))
for _, flag := range f {
if len(flag.Tags) > 0 && !list.ContainsAny(flag.Tags, tags) {
continue
}
result = append(result, flag.Flag)
}
return result
}
// Remove removes command flags by name.
func (f CliFlags) Remove(names []string) (result CliFlags) {
result = make(CliFlags, 0, len(f))
for _, flag := range f {
if list.Contains(names, flag.Name()) {
continue
}
result = append(result, flag)
}
return result
}
// Insert inserts command flags, if possible after name.
func (f CliFlags) Insert(name string, insert []CliFlag) (result CliFlags) {
result = make(CliFlags, 0, len(f)+len(insert))
done := false
for _, flag := range f {
result = append(result, flag)
if !done && flag.Name() == name {
result = append(result, insert...)
done = true
}
}
if !done {
result = append(result, insert...)
}
return result
}
// Prepend adds command flags at the beginning.
func (f CliFlags) Prepend(el []CliFlag) (result CliFlags) {
result = make(CliFlags, 0, len(f)+len(el))
result = append(result, el...)
return append(result, f...)
}