Skip to content

Commit

Permalink
chore: update some examle codes and update builtin GenAutoComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 7, 2022
1 parent 64b783c commit 4d91775
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 44 deletions.
1 change: 1 addition & 0 deletions _examples/multilevel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var l1sub1opts = struct {
var l2sub1opts = struct {
astr string
}{}

var cmd = gcli.Command{
Name: "test",
Aliases: []string{"ts"},
Expand Down
74 changes: 32 additions & 42 deletions _examples/serveman/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,18 @@ var (
confFile string
)

// ServerStart eg: cliapp serve:start
func ServerStart() *gcli.Command {
c := &gcli.Command{
Name: "start",
Desc: "start server",
Func: func(c *gcli.Command, args []string) error {
return startServer(c.BinName())
},
}

// c.StrOpt(&config.Pid, "pid", "", "", "the running server PID file")
c.StrOpt(&confFile, "config", "c", "serve-config.json", "the running json config file path")
c.BoolOpt(&config.Daemon, "daemon", "d", false, "the running server PID file")

return c
// ServerStart command
var ServerStart = &gcli.Command{
Name: "start",
Desc: "start server",
Func: func(c *gcli.Command, args []string) error {
return startServer(c.BinName())
},
Config: func(c *gcli.Command) {
// c.StrOpt(&config.Pid, "pid", "", "", "the running server PID file")
c.StrOpt(&confFile, "config", "c", "serve-config.json", "the running json config file path")
c.BoolOpt(&config.Daemon, "daemon", "d", false, "the running server PID file")
},
}

func startServer(binFile string) (err error) {
Expand All @@ -53,29 +50,38 @@ func startServer(binFile string) (err error) {
}

pid := cmd.Process.Pid
color.Green.Printf("Server start, [PID] %d running...\n", pid)
color.Greenf("Server start, [PID] %d running...\n", pid)
err = ioutil.WriteFile(config.PidFile, []byte(fmt.Sprintf("%d", pid)), 0666)
config.Daemon = false
return
}

color.Info.Println("Server started")
color.Infoln("Server started")
// front run
// startHttp()
return
}

func ServerStop() *gcli.Command {
c := &gcli.Command{
Name: "stop",
Desc: "stop the running server(by PID file)",
}

c.Func = func(_ *gcli.Command, _ []string) error {
var ServerStop = &gcli.Command{
Name: "stop",
Desc: "stop the running server(by PID file)",
Func: func(_ *gcli.Command, _ []string) error {
return stopServer()
}
},
}

// ServerRestart Server restart
var ServerRestart = &gcli.Command{
Name: "restart",
Desc: "restart the running server by PID file",
Func: func(c *gcli.Command, _ []string) (err error) {
// c.App().SubRun("stop", []string{"-c", confFile})
if err = stopServer(); err != nil {
return
}

return c
return startServer(c.BinName())
},
}

func stopServer() error {
Expand All @@ -86,19 +92,3 @@ func stopServer() error {
color.Success.Println("server stopped")
return err
}

// ServerRestart Server restart
func ServerRestart() *gcli.Command {
return &gcli.Command{
Name: "restart",
Desc: "restart the running server by PID file",
Func: func(c *gcli.Command, _ []string) (err error) {
// c.App().SubRun("stop", []string{"-c", confFile})
if err = stopServer(); err != nil {
return
}

return startServer(c.BinName())
},
}
}
2 changes: 1 addition & 1 deletion _examples/serveman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ func main() {
app.Version = "1.0.0"
app.Desc = "manage the http server start,stop,restart"

app.Add(ServerStart(), ServerStop(), ServerRestart())
app.Add(ServerStart, ServerStop, ServerRestart)
app.Run(nil)
}
2 changes: 1 addition & 1 deletion builtin/gen_auto_complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GenAutoComplete() *gcli.Command {
&genOpts.output,
"output",
"o",
"auto-completion.{shell}",
"auto-completion."+shell,
"output shell auto completion script file name.",
)

Expand Down

0 comments on commit 4d91775

Please sign in to comment.