-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use cobra and add some command
- Loading branch information
Showing
20 changed files
with
279 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
package args | ||
package flags | ||
|
||
var ( | ||
Config string // config file | ||
Debug bool | ||
Version bool | ||
Password bool | ||
NoPrefix bool | ||
Dev bool | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.