2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 10:17:17 +08:00

feat: 添加CLI命令

This commit is contained in:
耗子
2024-11-26 04:06:04 +08:00
parent c1580afadf
commit ff67ace165
2 changed files with 82 additions and 16 deletions

View File

@@ -173,6 +173,51 @@ func Cli() []*cli.Command {
}, },
}, },
}, },
{
Name: "database",
Usage: "数据库管理",
Commands: []*cli.Command{
{
Name: "add-server",
Usage: "添加数据库服务器",
Action: cliService.DatabaseAddServer,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "type",
Usage: "服务器类型",
Required: true,
},
&cli.StringFlag{
Name: "name",
Usage: "服务器名称",
Required: true,
},
&cli.StringFlag{
Name: "host",
Usage: "服务器地址",
Required: true,
},
&cli.UintFlag{
Name: "port",
Usage: "服务器端口",
Required: true,
},
&cli.StringFlag{
Name: "username",
Usage: "服务器用户名",
},
&cli.StringFlag{
Name: "password",
Usage: "服务器密码",
},
&cli.StringFlag{
Name: "remark",
Usage: "服务器备注",
},
},
},
},
},
{ {
Name: "backup", Name: "backup",
Usage: "数据备份", Usage: "数据备份",

View File

@@ -31,26 +31,28 @@ import (
) )
type CliService struct { type CliService struct {
hr string hr string
api *api.API api *api.API
appRepo biz.AppRepo appRepo biz.AppRepo
userRepo biz.UserRepo userRepo biz.UserRepo
settingRepo biz.SettingRepo settingRepo biz.SettingRepo
backupRepo biz.BackupRepo backupRepo biz.BackupRepo
websiteRepo biz.WebsiteRepo websiteRepo biz.WebsiteRepo
hash hash.Hasher databaseServerRepo biz.DatabaseServerRepo
hash hash.Hasher
} }
func NewCliService() *CliService { func NewCliService() *CliService {
return &CliService{ return &CliService{
hr: `+----------------------------------------------------`, hr: `+----------------------------------------------------`,
api: api.NewAPI(app.Version), api: api.NewAPI(app.Version),
appRepo: data.NewAppRepo(), appRepo: data.NewAppRepo(),
userRepo: data.NewUserRepo(), userRepo: data.NewUserRepo(),
settingRepo: data.NewSettingRepo(), settingRepo: data.NewSettingRepo(),
backupRepo: data.NewBackupRepo(), backupRepo: data.NewBackupRepo(),
websiteRepo: data.NewWebsiteRepo(), websiteRepo: data.NewWebsiteRepo(),
hash: hash.NewArgon2id(), databaseServerRepo: data.NewDatabaseServerRepo(),
hash: hash.NewArgon2id(),
} }
} }
@@ -479,6 +481,25 @@ func (s *CliService) WebsiteWrite(ctx context.Context, cmd *cli.Command) error {
return nil return nil
} }
func (s *CliService) DatabaseAddServer(ctx context.Context, cmd *cli.Command) error {
req := &request.DatabaseServerCreate{
Type: cmd.String("type"),
Name: cmd.String("name"),
Host: cmd.String("host"),
Port: uint(cmd.Uint("port")),
Username: cmd.String("username"),
Password: cmd.String("password"),
Remark: cmd.String("remark"),
}
if err := s.databaseServerRepo.Create(req); err != nil {
return err
}
fmt.Printf("数据库服务器 %s 添加成功\n", cmd.String("name"))
return nil
}
func (s *CliService) BackupWebsite(ctx context.Context, cmd *cli.Command) error { func (s *CliService) BackupWebsite(ctx context.Context, cmd *cli.Command) error {
fmt.Println(s.hr) fmt.Println(s.hr)
fmt.Printf("★ 开始备份 [%s]\n", time.Now().Format(time.DateTime)) fmt.Printf("★ 开始备份 [%s]\n", time.Now().Format(time.DateTime))