mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 04:06:18 +08:00
feat(cmd): initialize command structure and configuration management
This commit is contained in:
32
cmd/common.go
Normal file
32
cmd/common.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/OpenListTeam/OpenList/v5/cmd/flags"
|
||||||
|
"github.com/OpenListTeam/OpenList/v5/internal/bootstrap"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
InitLogrus()
|
||||||
|
bootstrap.InitConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initLog(l *logrus.Logger) {
|
||||||
|
if flags.Debug || flags.Dev {
|
||||||
|
l.SetLevel(logrus.DebugLevel)
|
||||||
|
l.SetReportCaller(true)
|
||||||
|
} else {
|
||||||
|
l.SetLevel(logrus.InfoLevel)
|
||||||
|
l.SetReportCaller(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func InitLogrus() {
|
||||||
|
formatter := logrus.TextFormatter{
|
||||||
|
ForceColors: true,
|
||||||
|
EnvironmentOverrideColors: true,
|
||||||
|
TimestampFormat: "2006-01-02 15:04:05",
|
||||||
|
FullTimestamp: true,
|
||||||
|
}
|
||||||
|
logrus.SetFormatter(&formatter)
|
||||||
|
initLog(logrus.StandardLogger())
|
||||||
|
}
|
40
cmd/flags/config.go
Normal file
40
cmd/flags/config.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package flags
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ConfigFile string
|
||||||
|
Debug bool
|
||||||
|
NoPrefix bool
|
||||||
|
Dev bool
|
||||||
|
ForceBinDir bool
|
||||||
|
LogStd bool
|
||||||
|
|
||||||
|
pwd string
|
||||||
|
)
|
||||||
|
|
||||||
|
// Program working directory
|
||||||
|
func PWD() string {
|
||||||
|
if pwd != "" {
|
||||||
|
return pwd
|
||||||
|
}
|
||||||
|
if ForceBinDir {
|
||||||
|
ex, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
pwd = filepath.Dir(ex)
|
||||||
|
return pwd
|
||||||
|
}
|
||||||
|
d, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
pwd = d
|
||||||
|
return d
|
||||||
|
}
|
33
cmd/root.go
Normal file
33
cmd/root.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/OpenListTeam/OpenList/v5/cmd/flags"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var RootCmd = &cobra.Command{
|
||||||
|
Use: "openlist",
|
||||||
|
Short: "A file list program that supports multiple storage.",
|
||||||
|
Long: `A file list program that supports multiple storage,
|
||||||
|
built with love by OpenListTeam.
|
||||||
|
Complete documentation is available at https://doc.oplist.org/`,
|
||||||
|
}
|
||||||
|
|
||||||
|
func Execute() {
|
||||||
|
if err := RootCmd.Execute(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.PersistentFlags().StringVarP(&flags.ConfigFile, "config", "c", "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")
|
||||||
|
RootCmd.PersistentFlags().BoolVarP(&flags.ForceBinDir, "force-bin-dir", "f", false, "force to use the directory where the binary file is located as data directory")
|
||||||
|
RootCmd.PersistentFlags().BoolVar(&flags.LogStd, "log-std", false, "force to log to std")
|
||||||
|
}
|
31
cmd/server.go
Normal file
31
cmd/server.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
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()
|
||||||
|
log.Println("Server exit")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(ServerCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutOpenListInit 暴露用于外部启动server的函数
|
||||||
|
func OutOpenListInit() {
|
||||||
|
var (
|
||||||
|
cmd *cobra.Command
|
||||||
|
args []string
|
||||||
|
)
|
||||||
|
ServerCmd.Run(cmd, args)
|
||||||
|
}
|
11
go.mod
11
go.mod
@ -1,3 +1,14 @@
|
|||||||
module github.com/OpenListTeam/OpenList/v5
|
module github.com/OpenListTeam/OpenList/v5
|
||||||
|
|
||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/sirupsen/logrus v1.9.3
|
||||||
|
github.com/spf13/cobra v1.9.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.6 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||||
|
)
|
||||||
|
24
go.sum
Normal file
24
go.sum
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||||
|
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||||
|
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||||
|
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
27
internal/bootstrap/config.go
Normal file
27
internal/bootstrap/config.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/OpenListTeam/OpenList/v5/cmd/flags"
|
||||||
|
"github.com/OpenListTeam/OpenList/v5/internal/conf"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitConfig() {
|
||||||
|
if !filepath.IsAbs(flags.ConfigFile) {
|
||||||
|
flags.ConfigFile = filepath.Join(flags.PWD(), flags.ConfigFile)
|
||||||
|
}
|
||||||
|
log.Infof("reading config file: %s", flags.ConfigFile)
|
||||||
|
conf.Conf = conf.DefaultConfig()
|
||||||
|
|
||||||
|
// convert abs path
|
||||||
|
configDir := filepath.Dir(flags.ConfigFile)
|
||||||
|
convertAbsPath := func(path *string) {
|
||||||
|
if !filepath.IsAbs(*path) {
|
||||||
|
*path = filepath.Join(configDir, *path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
convertAbsPath(&conf.Conf.TempDir)
|
||||||
|
log.Debugf("config: %+v", conf.Conf)
|
||||||
|
}
|
13
internal/conf/config.go
Normal file
13
internal/conf/config.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package conf
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
TempDir string `json:"temp_dir" env:"TEMP_DIR"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
TempDir: "temp",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var Conf *Config
|
Reference in New Issue
Block a user