Skip to content

Commit 0df3473

Browse files
committed
feat: use cobra and add some command
1 parent 2b5da3e commit 0df3473

20 files changed

Lines changed: 279 additions & 96 deletions

File tree

cmd/alist.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

cmd/cancel2FA.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"github.com/alist-org/alist/v3/internal/db"
9+
log "github.com/sirupsen/logrus"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// cancel2FACmd represents the delete2fa command
14+
var cancel2FACmd = &cobra.Command{
15+
Use: "cancel2fa",
16+
Short: "Delete 2FA of admin user",
17+
Run: func(cmd *cobra.Command, args []string) {
18+
Init()
19+
admin, err := db.GetAdmin()
20+
if err != nil {
21+
log.Errorf("failed to get admin user: %+v", err)
22+
} else {
23+
err := db.Cancel2FAByUser(admin)
24+
if err != nil {
25+
log.Errorf("failed to cancel 2FA: %+v", err)
26+
}
27+
}
28+
},
29+
}
30+
31+
func init() {
32+
rootCmd.AddCommand(cancel2FACmd)
33+
34+
// Here you will define your flags and configuration settings.
35+
36+
// Cobra supports Persistent Flags which will work for this command
37+
// and all subcommands, e.g.:
38+
// cancel2FACmd.PersistentFlags().String("foo", "", "A help for foo")
39+
40+
// Cobra supports local flags which will only run when this command
41+
// is called directly, e.g.:
42+
// cancel2FACmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
43+
}

cmd/common.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cmd
2+
3+
import (
4+
"github.com/alist-org/alist/v3/internal/bootstrap"
5+
"github.com/alist-org/alist/v3/internal/bootstrap/data"
6+
)
7+
8+
func Init() {
9+
bootstrap.InitConfig()
10+
bootstrap.Log()
11+
bootstrap.InitDB()
12+
data.InitData()
13+
bootstrap.InitAria2()
14+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
package args
1+
package flags
22

33
var (
44
Config string // config file
55
Debug bool
6-
Version bool
7-
Password bool
86
NoPrefix bool
97
Dev bool
108
)

cmd/password.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"github.com/alist-org/alist/v3/internal/db"
9+
log "github.com/sirupsen/logrus"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// passwordCmd represents the password command
14+
var passwordCmd = &cobra.Command{
15+
Use: "password",
16+
Short: "Show admin user's password",
17+
Run: func(cmd *cobra.Command, args []string) {
18+
Init()
19+
admin, err := db.GetAdmin()
20+
if err != nil {
21+
log.Errorf("failed get admin user: %+v", err)
22+
} else {
23+
log.Infof("admin user's password is: %s", admin.Password)
24+
}
25+
},
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(passwordCmd)
30+
31+
// Here you will define your flags and configuration settings.
32+
33+
// Cobra supports Persistent Flags which will work for this command
34+
// and all subcommands, e.g.:
35+
// passwordCmd.PersistentFlags().String("foo", "", "A help for foo")
36+
37+
// Cobra supports local flags which will only run when this command
38+
// is called directly, e.g.:
39+
// passwordCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
40+
}

cmd/root.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/alist-org/alist/v3/cmd/flags"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var rootCmd = &cobra.Command{
12+
Use: "alist",
13+
Short: "A file list program that supports multiple storage.",
14+
Long: `A file list program that supports multiple storage,
15+
built with love by Xhofe and friends in Go/Solid.js.
16+
Complete documentation is available at https://alist.nn.ci/`,
17+
}
18+
19+
func Execute() {
20+
if err := rootCmd.Execute(); err != nil {
21+
fmt.Fprintln(os.Stderr, err)
22+
os.Exit(1)
23+
}
24+
}
25+
26+
func init() {
27+
rootCmd.PersistentFlags().StringVar(&flags.Config, "conf", "data/config.json", "config file")
28+
rootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "start with debug mode")
29+
rootCmd.PersistentFlags().BoolVar(&flags.NoPrefix, "no-prefix", false, "disable env prefix")
30+
rootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", false, "start with dev mode")
31+
}

cmd/server.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/alist-org/alist/v3/cmd/flags"
6+
"github.com/alist-org/alist/v3/internal/conf"
7+
"github.com/alist-org/alist/v3/server"
8+
"github.com/gin-gonic/gin"
9+
log "github.com/sirupsen/logrus"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// serverCmd represents the server command
15+
var serverCmd = &cobra.Command{
16+
Use: "server",
17+
Short: "Start the server at the specified address",
18+
Long: `Start the server at the specified address
19+
the address is defined in config file`,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
Init()
22+
if !flags.Debug && !flags.Dev {
23+
gin.SetMode(gin.ReleaseMode)
24+
}
25+
r := gin.New()
26+
r.Use(gin.LoggerWithWriter(log.StandardLogger().Out), gin.RecoveryWithWriter(log.StandardLogger().Out))
27+
server.Init(r)
28+
base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
29+
log.Infof("start server @ %s", base)
30+
var err error
31+
if conf.Conf.Scheme.Https {
32+
err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
33+
} else {
34+
err = r.Run(base)
35+
}
36+
if err != nil {
37+
log.Errorf("failed to start: %s", err.Error())
38+
}
39+
},
40+
}
41+
42+
func init() {
43+
rootCmd.AddCommand(serverCmd)
44+
45+
// Here you will define your flags and configuration settings.
46+
47+
// Cobra supports Persistent Flags which will work for this command
48+
// and all subcommands, e.g.:
49+
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
50+
51+
// Cobra supports local flags which will only run when this command
52+
// is called directly, e.g.:
53+
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
54+
}

cmd/version.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
"github.com/alist-org/alist/v3/internal/conf"
10+
"os"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
// versionCmd represents the version command
16+
var versionCmd = &cobra.Command{
17+
Use: "version",
18+
Short: "Show current version of AList",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
fmt.Printf(`Built At: %s
21+
Go Version: %s
22+
Author: %s
23+
Commit ID: %s
24+
Version: %s
25+
WebVersion: %s
26+
`,
27+
conf.BuiltAt, conf.GoVersion, conf.GitAuthor, conf.GitCommit, conf.Version, conf.WebVersion)
28+
os.Exit(0)
29+
},
30+
}
31+
32+
func init() {
33+
rootCmd.AddCommand(versionCmd)
34+
35+
// Here you will define your flags and configuration settings.
36+
37+
// Cobra supports Persistent Flags which will work for this command
38+
// and all subcommands, e.g.:
39+
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")
40+
41+
// Cobra supports local flags which will only run when this command
42+
// is called directly, e.g.:
43+
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
44+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
github.com/go-playground/validator/v10 v10.11.0 // indirect
3131
github.com/go-sql-driver/mysql v1.6.0 // indirect
3232
github.com/goccy/go-json v0.9.7 // indirect
33+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
3334
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
3435
github.com/jackc/pgconn v1.12.1 // indirect
3536
github.com/jackc/pgio v1.0.0 // indirect
@@ -48,6 +49,8 @@ require (
4849
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4950
github.com/modern-go/reflect2 v1.0.2 // indirect
5051
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
52+
github.com/spf13/cobra v1.5.0 // indirect
53+
github.com/spf13/pflag v1.0.5 // indirect
5154
github.com/ugorji/go/codec v1.2.7 // indirect
5255
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
5356
golang.org/x/net v0.0.0-20220531201128-c960675eff93 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I
1111
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
1212
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
1313
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
14+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1415
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
1516
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1617
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=
5455
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5556
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
5657
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
58+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
59+
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
5760
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
5861
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
5962
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
171174
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
172175
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
173176
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
177+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
174178
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
175179
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
176180
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
179183
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
180184
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
181185
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
186+
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
187+
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
188+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
189+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
182190
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
183191
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
184192
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=

0 commit comments

Comments
 (0)