filebrowser/cmd/config.go
Henrique Dias 42134a4849 feat: cleanup cli
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

Former-commit-id: 2923c251335301f361098890bf67d47cd58c0f86 [formerly 277653e21c4b9077ea3b83c149f0c5b2982b694f] [formerly 7481557b6f47c8de6499f3ee7339ea8d29216700 [formerly 999c69de5c]]
Former-commit-id: 65eaacb4aee11f84c9f97ca2834def045921202a [formerly 7c9260a1fe142258dd0ac02c4710b03badd66d76]
Former-commit-id: ff3f3d7189b2e8cc2f00bef581cba108780e4552
2019-01-06 15:26:46 +00:00

123 lines
3.9 KiB
Go

package cmd
import (
"encoding/json"
nerrors "errors"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/filebrowser/filebrowser/v2/auth"
"github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(configCmd)
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Configuration management utility",
Long: `Configuration management utility.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
os.Exit(0)
},
}
func addConfigFlags(cmd *cobra.Command) {
addUserFlags(cmd)
cmd.Flags().BoolP("signup", "s", false, "allow users to signup")
cmd.Flags().String("shell", "", "shell command to which other commands should be appended")
cmd.Flags().String("auth.method", string(auth.MethodJSONAuth), "authentication type")
cmd.Flags().String("auth.header", "", "HTTP header for auth.method=proxy")
cmd.Flags().String("recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China")
cmd.Flags().String("recaptcha.key", "", "ReCaptcha site key")
cmd.Flags().String("recaptcha.secret", "", "ReCaptcha secret")
cmd.Flags().String("branding.name", "", "replace 'File Browser' by this name")
cmd.Flags().String("branding.files", "", "path to directory with images and custom styles")
cmd.Flags().Bool("branding.disableExternal", false, "disable external links such as GitHub links")
}
func getAuthentication(cmd *cobra.Command) (settings.AuthMethod, auth.Auther) {
method := settings.AuthMethod(mustGetString(cmd, "auth.method"))
var auther auth.Auther
if method == auth.MethodProxyAuth {
header := mustGetString(cmd, "auth.header")
if header == "" {
panic(nerrors.New("you must set the flag 'auth.header' for method 'proxy'"))
}
auther = &auth.ProxyAuth{Header: header}
}
if method == auth.MethodNoAuth {
auther = &auth.NoAuth{}
}
if method == auth.MethodJSONAuth {
jsonAuth := &auth.JSONAuth{}
host := mustGetString(cmd, "recaptcha.host")
key := mustGetString(cmd, "recaptcha.key")
secret := mustGetString(cmd, "recaptcha.secret")
if key != "" && secret != "" {
jsonAuth.ReCaptcha = &auth.ReCaptcha{
Host: host,
Key: key,
Secret: secret,
}
}
auther = jsonAuth
}
if auther == nil {
panic(errors.ErrInvalidAuthMethod)
}
return method, auther
}
func printSettings(s *settings.Settings, auther auth.Auther) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "Sign up:\t%t\n", s.Signup)
fmt.Fprintf(w, "Auth method:\t%s\n", s.AuthMethod)
fmt.Fprintf(w, "Shell:\t%s\t\n", strings.Join(s.Shell, " "))
fmt.Fprintln(w, "\nBranding:")
fmt.Fprintf(w, "\tName:\t%s\n", s.Branding.Name)
fmt.Fprintf(w, "\tFiles override:\t%s\n", s.Branding.Files)
fmt.Fprintf(w, "\tDisable external links:\t%t\n", s.Branding.DisableExternal)
fmt.Fprintln(w, "\nDefaults:")
fmt.Fprintf(w, "\tScope:\t%s\n", s.Defaults.Scope)
fmt.Fprintf(w, "\tLocale:\t%s\n", s.Defaults.Locale)
fmt.Fprintf(w, "\tView mode:\t%s\n", s.Defaults.ViewMode)
fmt.Fprintf(w, "\tCommands:\t%s\n", strings.Join(s.Defaults.Commands, " "))
fmt.Fprintf(w, "\tSorting:\n")
fmt.Fprintf(w, "\t\tBy:\t%s\n", s.Defaults.Sorting.By)
fmt.Fprintf(w, "\t\tAsc:\t%t\n", s.Defaults.Sorting.Asc)
fmt.Fprintf(w, "\tPermissions:\n")
fmt.Fprintf(w, "\t\tAdmin:\t%t\n", s.Defaults.Perm.Admin)
fmt.Fprintf(w, "\t\tExecute:\t%t\n", s.Defaults.Perm.Execute)
fmt.Fprintf(w, "\t\tCreate:\t%t\n", s.Defaults.Perm.Create)
fmt.Fprintf(w, "\t\tRename:\t%t\n", s.Defaults.Perm.Rename)
fmt.Fprintf(w, "\t\tModify:\t%t\n", s.Defaults.Perm.Modify)
fmt.Fprintf(w, "\t\tDelete:\t%t\n", s.Defaults.Perm.Delete)
fmt.Fprintf(w, "\t\tShare:\t%t\n", s.Defaults.Perm.Share)
fmt.Fprintf(w, "\t\tDownload:\t%t\n", s.Defaults.Perm.Download)
w.Flush()
b, err := json.MarshalIndent(auther, "", " ")
checkErr(err)
fmt.Printf("\nAuther configuration (raw):\n\n%s\n\n", string(b))
}