refactor: cleanup some functions

This commit is contained in:
Henrique Dias 2025-11-16 08:30:29 +01:00
parent 7de4d83ab1
commit 2ba19954d6
No known key found for this signature in database
3 changed files with 23 additions and 24 deletions

View File

@ -52,12 +52,12 @@ override the options.`,
}, },
} }
s.FileMode, err = getAndParseMode(v, "filemode") s.FileMode, err = parseFileMode(v.GetString("fileMode"))
if err != nil { if err != nil {
return err return err
} }
s.DirMode, err = getAndParseMode(v, "dirmode") s.DirMode, err = parseFileMode(v.GetString("dirMode"))
if err != nil { if err != nil {
return err return err
} }

View File

@ -78,9 +78,9 @@ you want to change. Other options will remain unchanged.`,
case "branding.files": case "branding.files":
set.Branding.Files = v.GetString(key) set.Branding.Files = v.GetString(key)
case "filemode": case "filemode":
set.FileMode, err = getAndParseMode(v, key) set.FileMode, err = parseFileMode(v.GetString(key))
case "dirmode": case "dirmode":
set.DirMode, err = getAndParseMode(v, key) set.DirMode, err = parseFileMode(v.GetString(key))
} }
if err != nil { if err != nil {

View File

@ -24,11 +24,10 @@ import (
"github.com/filebrowser/filebrowser/v2/storage/bolt" "github.com/filebrowser/filebrowser/v2/storage/bolt"
) )
const dbPerms = 0640 const databasePermissions = 0640
func getAndParseMode(v *viper.Viper, param string) (fs.FileMode, error) { func parseFileMode(value string) (fs.FileMode, error) {
s := v.GetString(param) b, err := strconv.ParseUint(value, 0, 32)
b, err := strconv.ParseUint(s, 0, 32)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -43,20 +42,6 @@ func generateKey() []byte {
return k return k
} }
type cobraFunc func(cmd *cobra.Command, args []string) error
type pythonFunc func(cmd *cobra.Command, args []string, v *viper.Viper, data *pythonData) error
type pythonConfig struct {
noDB bool
allowNoDB bool
}
type pythonData struct {
hadDB bool
store *storage.Storage
err error
}
func dbExists(path string) (bool, error) { func dbExists(path string) (bool, error) {
stat, err := os.Stat(path) stat, err := os.Stat(path)
if err == nil { if err == nil {
@ -79,7 +64,7 @@ func dbExists(path string) (bool, error) {
// Generate the replacements for all environment variables. This allows to // Generate the replacements for all environment variables. This allows to
// use FB_BRANDING_DISABLE_EXTERNAL environment variables, even when the // use FB_BRANDING_DISABLE_EXTERNAL environment variables, even when the
// option name is branding.disableexternal. // option name is branding.disableExternal.
func generateEnvKeyReplacements(cmd *cobra.Command) []string { func generateEnvKeyReplacements(cmd *cobra.Command) []string {
replacements := []string{} replacements := []string{}
@ -141,6 +126,20 @@ func initViper(cmd *cobra.Command) (*viper.Viper, error) {
return v, nil return v, nil
} }
type cobraFunc func(cmd *cobra.Command, args []string) error
type pythonFunc func(cmd *cobra.Command, args []string, v *viper.Viper, data *pythonData) error
type pythonConfig struct {
noDB bool
allowNoDB bool
}
type pythonData struct {
hadDB bool
store *storage.Storage
err error
}
func python(fn pythonFunc, cfg pythonConfig) cobraFunc { func python(fn pythonFunc, cfg pythonConfig) cobraFunc {
return func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error {
v, err := initViper(cmd) v, err := initViper(cmd)
@ -169,7 +168,7 @@ func python(fn pythonFunc, cfg pythonConfig) cobraFunc {
log.Println("Using database: " + absPath) log.Println("Using database: " + absPath)
data.hadDB = exists data.hadDB = exists
db, err := storm.Open(path, storm.BoltOptions(dbPerms, nil)) db, err := storm.Open(path, storm.BoltOptions(databasePermissions, nil))
if err != nil { if err != nil {
return err return err
} }