2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/rules"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-05 22:44:33 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
allow := mustGetBool(cmd, "allow")
|
2019-01-06 13:21:31 +00:00
|
|
|
regex := mustGetBool(cmd, "regex")
|
|
|
|
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, st *storage.Storage) {
|
|
|
|
u.Rules = append(u.Rules, rule)
|
|
|
|
err := st.Users.Save(u)
|
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
global := func(s *settings.Settings, st *storage.Storage) {
|
|
|
|
s.Rules = append(s.Rules, rule)
|
|
|
|
err := st.Settings.Save(s)
|
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
runRules(cmd, user, global)
|
|
|
|
},
|
|
|
|
}
|