Skip to content

Commit

Permalink
feat: cli 路由支持
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Sep 18, 2024
1 parent dded5e2 commit 407203d
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 40 deletions.
9 changes: 3 additions & 6 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
project_name: panel

builds:
- id: panel
- id: web
main: ./cmd/web
binary: panel
binary: web
env:
- CGO_ENABLED=0
goos:
Expand Down Expand Up @@ -47,11 +47,8 @@ archives:
strip_binary_directory: true
files:
- LICENSE
- docs/*
- storage/*
- lang/*
- scripts/*
- panel-example.conf
- config/*

gitlab_urls:
api: https://git.haozi.net/api/v4/
Expand Down
29 changes: 2 additions & 27 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package main

import (
"fmt"
"log"
"os"

"github.com/urfave/cli/v2"
"github.com/TheTNB/panel/internal/bootstrap"
)

func main() {
app := &cli.App{
Name: "panel",
HelpName: "耗子面板",
Usage: "命令行工具",
UsageText: "panel [global options] command [command options] [arguments...]",
HideVersion: true,
Commands: []*cli.Command{
{
Name: "test",
Aliases: []string{"t"},
Usage: "print a test message",
Action: func(c *cli.Context) error {
fmt.Println("Hello, World!")
return nil
},
},
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
bootstrap.BootCli()
}
2 changes: 1 addition & 1 deletion cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ import "github.com/TheTNB/panel/internal/bootstrap"

// @BasePath /api
func main() {
bootstrap.Boot()
bootstrap.BootWeb()
}
5 changes: 0 additions & 5 deletions config/README.md

This file was deleted.

11 changes: 10 additions & 1 deletion internal/bootstrap/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ import (
"runtime/debug"
)

func Boot() {
func boot() {
debug.SetGCPercent(10)
debug.SetMemoryLimit(64 << 20)

initConf()
initGlobal()
initOrm()
runMigrate()
}

func BootWeb() {
boot()
initValidator()
initSession()
initQueue()
go initHttp()

select {}
}

func BootCli() {
boot()
initCli()
}
26 changes: 26 additions & 0 deletions internal/bootstrap/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package bootstrap

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

"github.com/TheTNB/panel/internal/panel"
"github.com/TheTNB/panel/internal/route"
)

func initCli() {
app := &cli.App{
Name: "panel-cli",
HelpName: fmt.Sprintf("耗子面板 %s", panel.Version),
Usage: "命令行工具",
UsageText: "panel-cli [global options] command [command options] [arguments...]",
HideVersion: true,
Commands: route.Cli(),
}

if err := app.Run(os.Args); err != nil {
panic(fmt.Sprintf("failed to run cli: %v", err))
}
}
19 changes: 19 additions & 0 deletions internal/route/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package route

import (
"github.com/urfave/cli/v2"

"github.com/TheTNB/panel/internal/service"
)

func Cli() []*cli.Command {
cliService := service.NewCliService()
return []*cli.Command{
{
Name: "test",
Aliases: []string{"t"},
Usage: "print a test message",
Action: cliService.Test,
},
}
}
15 changes: 15 additions & 0 deletions internal/service/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package service

import "github.com/urfave/cli/v2"

type CliService struct {
}

func NewCliService() *CliService {
return &CliService{}
}

func (s *CliService) Test(c *cli.Context) error {
println("Hello, World!")
return nil
}

0 comments on commit 407203d

Please sign in to comment.