Files
filebrowser/cmd/rules_add.go
T

63 lines
1.3 KiB
Go
Raw Normal View History

2019-01-05 22:44:33 +00:00
package cmd
import (
"regexp"
2020-06-01 01:12:36 +02:00
"github.com/spf13/cobra"
2019-01-05 22:44:33 +00:00
"github.com/filebrowser/filebrowser/v2/rules"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/users"
)
func init() {
rulesCmd.AddCommand(rulesAddCmd)
2019-01-06 13:21:31 +00:00
rulesAddCmd.Flags().BoolP("allow", "a", false, "indicates this is an allow rule")
rulesAddCmd.Flags().BoolP("regex", "r", false, "indicates this is a regex rule")
2019-01-05 22:44:33 +00:00
}
var rulesAddCmd = &cobra.Command{
2019-01-06 13:21:31 +00:00
Use: "add <path|expression>",
2019-01-05 22:44:33 +00:00
Short: "Add a global rule or user rule",
2019-01-06 13:21:31 +00:00
Long: `Add a global rule or user rule.`,
Args: cobra.ExactArgs(1),
RunE: python(func(cmd *cobra.Command, args []string, d *pythonData) error {
allow, err := getBool(cmd.Flags(), "allow")
if err != nil {
return err
}
regex, err := getBool(cmd.Flags(), "regex")
if err != nil {
return err
}
2019-01-06 13:21:31 +00:00
exp := args[0]
2019-01-05 22:44:33 +00:00
2019-01-06 13:21:31 +00:00
if regex {
regexp.MustCompile(exp)
2019-01-05 22:44:33 +00:00
}
rule := rules.Rule{
Allow: allow,
2019-01-06 13:21:31 +00:00
Regex: regex,
}
if regex {
rule.Regexp = &rules.Regexp{Raw: exp}
} else {
rule.Path = exp
2019-01-05 22:44:33 +00:00
}
user := func(u *users.User) error {
2019-01-05 22:44:33 +00:00
u.Rules = append(u.Rules, rule)
return d.store.Users.Save(u)
2019-01-05 22:44:33 +00:00
}
global := func(s *settings.Settings) error {
2019-01-05 22:44:33 +00:00
s.Rules = append(s.Rules, rule)
return d.store.Settings.Save(s)
2019-01-05 22:44:33 +00:00
}
return runRules(d.store, cmd, user, global)
2019-01-07 20:24:23 +00:00
}, pythonConfig{}),
2019-01-05 22:44:33 +00:00
}