Files
filebrowser/cmd/rules_add.go
T

59 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),
2019-01-07 20:24:23 +00:00
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
2019-01-08 14:07:55 +00:00
allow := mustGetBool(cmd.Flags(), "allow")
regex := mustGetBool(cmd.Flags(), "regex")
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
}
2019-01-07 20:24:23 +00:00
user := func(u *users.User) {
2019-01-05 22:44:33 +00:00
u.Rules = append(u.Rules, rule)
2019-01-07 20:24:23 +00:00
err := d.store.Users.Save(u)
2019-01-05 22:44:33 +00:00
checkErr(err)
}
2019-01-07 20:24:23 +00:00
global := func(s *settings.Settings) {
2019-01-05 22:44:33 +00:00
s.Rules = append(s.Rules, rule)
2019-01-07 20:24:23 +00:00
err := d.store.Settings.Save(s)
2019-01-05 22:44:33 +00:00
checkErr(err)
}
2019-01-07 20:24:23 +00:00
runRules(d.store, cmd, user, global)
}, pythonConfig{}),
2019-01-05 22:44:33 +00:00
}