From 0df3473337771b2a0228c4b6b0212ddb6c00d5bf Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 7 Aug 2022 13:09:59 +0800 Subject: [PATCH] feat: use cobra and add some command --- cmd/alist.go | 59 ------------------------------ cmd/cancel2FA.go | 43 ++++++++++++++++++++++ cmd/common.go | 14 +++++++ cmd/{args => flags}/config.go | 4 +- cmd/password.go | 40 ++++++++++++++++++++ cmd/root.go | 31 ++++++++++++++++ cmd/server.go | 54 +++++++++++++++++++++++++++ cmd/version.go | 44 ++++++++++++++++++++++ go.mod | 3 ++ go.sum | 8 ++++ internal/bootstrap/config.go | 31 ++++++++-------- internal/bootstrap/data/data.go | 4 +- internal/bootstrap/data/dev.go | 4 +- internal/bootstrap/data/setting.go | 6 +-- internal/bootstrap/data/user.go | 7 ++-- internal/bootstrap/db.go | 4 +- internal/bootstrap/log.go | 4 +- main.go | 7 ++++ server/common/common.go | 4 +- server/router.go | 4 +- 20 files changed, 279 insertions(+), 96 deletions(-) delete mode 100644 cmd/alist.go create mode 100644 cmd/cancel2FA.go create mode 100644 cmd/common.go rename cmd/{args => flags}/config.go (66%) create mode 100644 cmd/password.go create mode 100644 cmd/root.go create mode 100644 cmd/server.go create mode 100644 cmd/version.go create mode 100644 main.go diff --git a/cmd/alist.go b/cmd/alist.go deleted file mode 100644 index e42d904a1b0..00000000000 --- a/cmd/alist.go +++ /dev/null @@ -1,59 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/alist-org/alist/v3/cmd/args" - _ "github.com/alist-org/alist/v3/drivers" - "github.com/alist-org/alist/v3/internal/bootstrap" - "github.com/alist-org/alist/v3/internal/bootstrap/data" - "github.com/alist-org/alist/v3/internal/conf" - "github.com/alist-org/alist/v3/server" - "github.com/gin-gonic/gin" - log "github.com/sirupsen/logrus" -) - -func init() { - flag.StringVar(&args.Config, "conf", "data/config.json", "config file") - flag.BoolVar(&args.Debug, "debug", false, "start with debug mode") - flag.BoolVar(&args.Version, "version", false, "print version info") - flag.BoolVar(&args.Password, "password", false, "print current password") - flag.BoolVar(&args.NoPrefix, "no-prefix", false, "disable env prefix") - flag.BoolVar(&args.Dev, "dev", false, "start with dev mode") - flag.Parse() -} - -func Init() { - if args.Version { - fmt.Printf("Built At: %s\nGo Version: %s\nAuthor: %s\nCommit ID: %s\nVersion: %s\nWebVersion: %s\n", - conf.BuiltAt, conf.GoVersion, conf.GitAuthor, conf.GitCommit, conf.Version, conf.WebVersion) - os.Exit(0) - } - bootstrap.InitConfig() - bootstrap.Log() - bootstrap.InitDB() - data.InitData() - bootstrap.InitAria2() -} -func main() { - Init() - if !args.Debug && !args.Dev { - gin.SetMode(gin.ReleaseMode) - } - r := gin.New() - r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out)) - server.Init(r) - base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port) - log.Infof("start server @ %s", base) - var err error - if conf.Conf.Scheme.Https { - err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile) - } else { - err = r.Run(base) - } - if err != nil { - log.Errorf("failed to start: %s", err.Error()) - } -} diff --git a/cmd/cancel2FA.go b/cmd/cancel2FA.go new file mode 100644 index 00000000000..ef3bf970b50 --- /dev/null +++ b/cmd/cancel2FA.go @@ -0,0 +1,43 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "github.com/alist-org/alist/v3/internal/db" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// cancel2FACmd represents the delete2fa command +var cancel2FACmd = &cobra.Command{ + Use: "cancel2fa", + Short: "Delete 2FA of admin user", + Run: func(cmd *cobra.Command, args []string) { + Init() + admin, err := db.GetAdmin() + if err != nil { + log.Errorf("failed to get admin user: %+v", err) + } else { + err := db.Cancel2FAByUser(admin) + if err != nil { + log.Errorf("failed to cancel 2FA: %+v", err) + } + } + }, +} + +func init() { + rootCmd.AddCommand(cancel2FACmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // cancel2FACmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // cancel2FACmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/common.go b/cmd/common.go new file mode 100644 index 00000000000..4776cae8445 --- /dev/null +++ b/cmd/common.go @@ -0,0 +1,14 @@ +package cmd + +import ( + "github.com/alist-org/alist/v3/internal/bootstrap" + "github.com/alist-org/alist/v3/internal/bootstrap/data" +) + +func Init() { + bootstrap.InitConfig() + bootstrap.Log() + bootstrap.InitDB() + data.InitData() + bootstrap.InitAria2() +} diff --git a/cmd/args/config.go b/cmd/flags/config.go similarity index 66% rename from cmd/args/config.go rename to cmd/flags/config.go index 1df93bab686..01161b189c8 100644 --- a/cmd/args/config.go +++ b/cmd/flags/config.go @@ -1,10 +1,8 @@ -package args +package flags var ( Config string // config file Debug bool - Version bool - Password bool NoPrefix bool Dev bool ) diff --git a/cmd/password.go b/cmd/password.go new file mode 100644 index 00000000000..4c709e4c507 --- /dev/null +++ b/cmd/password.go @@ -0,0 +1,40 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "github.com/alist-org/alist/v3/internal/db" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// passwordCmd represents the password command +var passwordCmd = &cobra.Command{ + Use: "password", + Short: "Show admin user's password", + Run: func(cmd *cobra.Command, args []string) { + Init() + admin, err := db.GetAdmin() + if err != nil { + log.Errorf("failed get admin user: %+v", err) + } else { + log.Infof("admin user's password is: %s", admin.Password) + } + }, +} + +func init() { + rootCmd.AddCommand(passwordCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // passwordCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // passwordCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 00000000000..5172fa41f9d --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/alist-org/alist/v3/cmd/flags" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "alist", + Short: "A file list program that supports multiple storage.", + Long: `A file list program that supports multiple storage, +built with love by Xhofe and friends in Go/Solid.js. +Complete documentation is available at https://alist.nn.ci/`, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func init() { + rootCmd.PersistentFlags().StringVar(&flags.Config, "conf", "data/config.json", "config file") + rootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "start with debug mode") + rootCmd.PersistentFlags().BoolVar(&flags.NoPrefix, "no-prefix", false, "disable env prefix") + rootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", false, "start with dev mode") +} diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 00000000000..9dd379a4da4 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "fmt" + "github.com/alist-org/alist/v3/cmd/flags" + "github.com/alist-org/alist/v3/internal/conf" + "github.com/alist-org/alist/v3/server" + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + + "github.com/spf13/cobra" +) + +// serverCmd represents the server command +var serverCmd = &cobra.Command{ + Use: "server", + Short: "Start the server at the specified address", + Long: `Start the server at the specified address +the address is defined in config file`, + Run: func(cmd *cobra.Command, args []string) { + Init() + if !flags.Debug && !flags.Dev { + gin.SetMode(gin.ReleaseMode) + } + r := gin.New() + r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out)) + server.Init(r) + base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port) + log.Infof("start server @ %s", base) + var err error + if conf.Conf.Scheme.Https { + err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile) + } else { + err = r.Run(base) + } + if err != nil { + log.Errorf("failed to start: %s", err.Error()) + } + }, +} + +func init() { + rootCmd.AddCommand(serverCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // serverCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000000..d341afcaacf --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,44 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + "github.com/alist-org/alist/v3/internal/conf" + "os" + + "github.com/spf13/cobra" +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Show current version of AList", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf(`Built At: %s +Go Version: %s +Author: %s +Commit ID: %s +Version: %s +WebVersion: %s +`, + conf.BuiltAt, conf.GoVersion, conf.GitAuthor, conf.GitCommit, conf.Version, conf.WebVersion) + os.Exit(0) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // versionCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/go.mod b/go.mod index 4150c4d3acb..a06eaed9f1c 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/go-playground/validator/v10 v10.11.0 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/goccy/go-json v0.9.7 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgconn v1.12.1 // indirect github.com/jackc/pgio v1.0.0 // indirect @@ -48,6 +49,8 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.1 // indirect + github.com/spf13/cobra v1.5.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/ugorji/go/codec v1.2.7 // indirect golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect golang.org/x/net v0.0.0-20220531201128-c960675eff93 // indirect diff --git a/go.sum b/go.sum index 811575f6e5d..ac27701f534 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,7 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -54,6 +55,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= @@ -171,6 +174,7 @@ github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6po github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= @@ -179,6 +183,10 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= +github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= diff --git a/internal/bootstrap/config.go b/internal/bootstrap/config.go index e660eb24b78..af8fecdf330 100644 --- a/internal/bootstrap/config.go +++ b/internal/bootstrap/config.go @@ -5,7 +5,7 @@ import ( "os" "path/filepath" - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/pkg/utils" "github.com/caarlos0/env/v6" @@ -13,36 +13,35 @@ import ( ) func InitConfig() { - log.Infof("reading config file: %s", args.Config) - if !utils.Exists(args.Config) { + log.Infof("reading config file: %s", flags.Config) + if !utils.Exists(flags.Config) { log.Infof("config file not exists, creating default config file") - _, err := utils.CreateNestedFile(args.Config) + _, err := utils.CreateNestedFile(flags.Config) if err != nil { log.Fatalf("failed to create config file: %+v", err) } conf.Conf = conf.DefaultConfig() - if !utils.WriteToJson(args.Config, conf.Conf) { + if !utils.WriteToJson(flags.Config, conf.Conf) { log.Fatalf("failed to create default config file") } } else { - configBytes, err := ioutil.ReadFile(args.Config) + configBytes, err := ioutil.ReadFile(flags.Config) if err != nil { - log.Fatalf("reading config file error:%s", err.Error()) + log.Fatalf("reading config file error: %+v", err) } conf.Conf = conf.DefaultConfig() err = utils.Json.Unmarshal(configBytes, conf.Conf) if err != nil { - log.Fatalf("load config error: %s", err.Error()) + log.Fatalf("load config error: %+v", err) } - log.Debugf("config:%+v", conf.Conf) // update config.json struct confBody, err := utils.Json.MarshalIndent(conf.Conf, "", " ") if err != nil { - log.Fatalf("marshal config error:%s", err.Error()) + log.Fatalf("marshal config error: %+v", err) } - err = ioutil.WriteFile(args.Config, confBody, 0777) + err = ioutil.WriteFile(flags.Config, confBody, 0777) if err != nil { - log.Fatalf("update config struct error: %s", err.Error()) + log.Fatalf("update config struct error: %+v", err) } } if !conf.Conf.Force { @@ -54,7 +53,7 @@ func InitConfig() { if !filepath.IsAbs(conf.Conf.TempDir) { absPath, err = filepath.Abs(conf.Conf.TempDir) if err != nil { - log.Fatalf("get abs path error: %s", err.Error()) + log.Fatalf("get abs path error: %+v", err) } } conf.Conf.TempDir = absPath @@ -64,20 +63,20 @@ func InitConfig() { } err = os.MkdirAll(conf.Conf.TempDir, 0700) if err != nil { - log.Fatalf("create temp dir error: %s", err.Error()) + log.Fatalf("create temp dir error: %+v", err) } log.Debugf("config: %+v", conf.Conf) } func confFromEnv() { prefix := "ALIST_" - if args.NoPrefix { + if flags.NoPrefix { prefix = "" } log.Infof("load config from env with prefix: %s", prefix) if err := env.Parse(conf.Conf, env.Options{ Prefix: prefix, }); err != nil { - log.Fatalf("load config from env error: %s", err.Error()) + log.Fatalf("load config from env error: %+v", err) } } diff --git a/internal/bootstrap/data/data.go b/internal/bootstrap/data/data.go index 832ffce149a..6c77ebf2385 100644 --- a/internal/bootstrap/data/data.go +++ b/internal/bootstrap/data/data.go @@ -1,11 +1,11 @@ package data -import "github.com/alist-org/alist/v3/cmd/args" +import "github.com/alist-org/alist/v3/cmd/flags" func InitData() { initUser() initSettings() - if args.Dev { + if flags.Dev { initDevData() initDevDo() } diff --git a/internal/bootstrap/data/dev.go b/internal/bootstrap/data/dev.go index 441787be03e..efdfc6af204 100644 --- a/internal/bootstrap/data/dev.go +++ b/internal/bootstrap/data/dev.go @@ -3,7 +3,7 @@ package data import ( "context" - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/message" "github.com/alist-org/alist/v3/internal/model" @@ -35,7 +35,7 @@ func initDevData() { } func initDevDo() { - if args.Dev { + if flags.Dev { go func() { err := message.GetMessenger().WaitSend(map[string]string{ "type": "dev", diff --git a/internal/bootstrap/data/setting.go b/internal/bootstrap/data/setting.go index 53e41d6120a..3efff2c7b00 100644 --- a/internal/bootstrap/data/setting.go +++ b/internal/bootstrap/data/setting.go @@ -1,7 +1,7 @@ package data import ( - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" @@ -60,7 +60,7 @@ func isActive(key string) bool { func initialSettings() { var token string - if args.Dev { + if flags.Dev { token = "dev_token" } else { token = random.Token() @@ -96,7 +96,7 @@ func initialSettings() { // single settings {Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE}, } - if args.Dev { + if flags.Dev { initialSettingItems = append(initialSettingItems, model.SettingItem{Key: "test_deprecated", Value: "test_value", Type: conf.TypeString, Flag: model.DEPRECATED}) } } diff --git a/internal/bootstrap/data/user.go b/internal/bootstrap/data/user.go index 1fd9d2bc1fc..2e63c5c7441 100644 --- a/internal/bootstrap/data/user.go +++ b/internal/bootstrap/data/user.go @@ -1,7 +1,7 @@ package data import ( - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/db" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils/random" @@ -13,7 +13,7 @@ import ( func initUser() { admin, err := db.GetAdmin() adminPassword := random.String(8) - if args.Dev { + if flags.Dev { adminPassword = "admin" } if err != nil { @@ -26,6 +26,8 @@ func initUser() { } if err := db.CreateUser(admin); err != nil { panic(err) + } else { + log.Infof("Successfully created the administrator user and the initial password is: %s", admin.Password) } } else { panic(err) @@ -48,5 +50,4 @@ func initUser() { panic(err) } } - log.Infof("admin password: %+v", admin.Password) } diff --git a/internal/bootstrap/db.go b/internal/bootstrap/db.go index 05ab85b6de3..58d5209ba01 100644 --- a/internal/bootstrap/db.go +++ b/internal/bootstrap/db.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/db" log "github.com/sirupsen/logrus" @@ -36,7 +36,7 @@ func InitDB() { } var dB *gorm.DB var err error - if args.Dev { + if flags.Dev { dB, err = gorm.Open(sqlite.Open("file::memory:?cache=shared"), gormConfig) } else { database := conf.Conf.Database diff --git a/internal/bootstrap/log.go b/internal/bootstrap/log.go index a63c0f21f54..80082c53557 100644 --- a/internal/bootstrap/log.go +++ b/internal/bootstrap/log.go @@ -4,7 +4,7 @@ import ( "log" "time" - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/conf" rotatelogs "github.com/lestrrat-go/file-rotatelogs" "github.com/sirupsen/logrus" @@ -22,7 +22,7 @@ func init() { func Log() { log.SetOutput(logrus.StandardLogger().Out) - if args.Debug || args.Dev { + if flags.Debug || flags.Dev { logrus.SetLevel(logrus.DebugLevel) logrus.SetReportCaller(true) } else { diff --git a/main.go b/main.go new file mode 100644 index 00000000000..ecf0a643e68 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/alist-org/alist/v3/cmd" + +func main() { + cmd.Execute() +} diff --git a/server/common/common.go b/server/common/common.go index c436a757b1a..9032513938f 100644 --- a/server/common/common.go +++ b/server/common/common.go @@ -1,7 +1,7 @@ package common import ( - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" ) @@ -10,7 +10,7 @@ import ( // @param l: if true, log error func ErrorResp(c *gin.Context, err error, code int, l ...bool) { if len(l) > 0 && l[0] { - if args.Debug || args.Dev { + if flags.Debug || flags.Dev { log.Errorf("%+v", err) } else { log.Errorf("%v", err) diff --git a/server/router.go b/server/router.go index bdd68505dea..65c784eb136 100644 --- a/server/router.go +++ b/server/router.go @@ -1,7 +1,7 @@ package server import ( - "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/message" "github.com/alist-org/alist/v3/server/common" @@ -34,7 +34,7 @@ func Init(r *gin.Engine) { fs(auth.Group("/fs")) admin(auth.Group("/admin", middlewares.AuthAdmin)) - if args.Dev { + if flags.Dev { dev(r.Group("/dev")) } }