feat: use cobra and add some command
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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",
|
||||
|
@ -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})
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user