feature: Adding service run command

This commit is contained in:
mlsmaycon
2021-06-19 14:55:45 +02:00
parent 6465e2556a
commit 41b50a08d4
6 changed files with 96 additions and 11 deletions

View File

@@ -36,6 +36,8 @@ func init() {
rootCmd.AddCommand(addPeerCmd)
rootCmd.AddCommand(upCmd)
rootCmd.AddCommand(signalCmd)
rootCmd.AddCommand(serviceCmd)
serviceCmd.AddCommand(runCmd) // run is a subcommand of service
}
// SetupCloseHandler handles SIGTERM signal and exits with success

26
cmd/service.go Normal file
View File

@@ -0,0 +1,26 @@
package cmd
import (
"github.com/kardianos/service"
"github.com/spf13/cobra"
)
func newSVCConfig() *service.Config {
return &service.Config{
Name: "wiretrustee",
DisplayName: "wiretrustee",
Description: "This is an example Go service.",
}
}
var (
serviceCmd = &cobra.Command{
Use: "service",
Short: "manages wiretrustee service",
//Run: func(cmd *cobra.Command, args []string) {
//},
}
)
func init() {
}

58
cmd/service_run.go Normal file
View File

@@ -0,0 +1,58 @@
package cmd
import (
"github.com/kardianos/service"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type program struct {
cmd *cobra.Command
args []string
}
var logger service.Logger
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
logger.Info("Starting service") //nolint
go upCmd.Run(p.cmd, p.args)
return nil
}
func (p *program) Stop(s service.Service) error {
stopUP <- 1
return nil
}
var (
runCmd = &cobra.Command{
Use: "run",
Short: "runs wiretrustee as service",
Run: func(cmd *cobra.Command, args []string) {
svcConfig := newSVCConfig()
prg := &program{
cmd: cmd,
args: args,
}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
logger.Error(err) //nolint
}
},
}
)
func init() {
}

View File

@@ -48,10 +48,15 @@ var (
//signalClient.WaitConnected()
SetupCloseHandler()
select {}
<-stopUP
log.Println("Receive signal to stop running")
},
}
)
// Execution control channel for stopUP signal
var stopUP chan int
func init() {
stopUP = make(chan int)
}