-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from wiretrustee/add-service-command
feature: Adding service command
- Loading branch information
Showing
15 changed files
with
461 additions
and
28 deletions.
There are no files selected for viewing
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
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
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,48 @@ | ||
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 newSVCConfig() *service.Config { | ||
return &service.Config{ | ||
Name: "wiretrustee", | ||
DisplayName: "Wiretrustee", | ||
Description: "A WireGuard-based mesh network that connects your devices into a single private network.", | ||
} | ||
} | ||
|
||
func newSVC(prg *program, conf *service.Config) (service.Service, error) { | ||
s, err := service.New(prg, conf) | ||
if err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
logger, err = s.Logger(nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
return s, nil | ||
} | ||
|
||
var ( | ||
serviceCmd = &cobra.Command{ | ||
Use: "service", | ||
Short: "manages wiretrustee service", | ||
//Run: func(cmd *cobra.Command, args []string) { | ||
//}, | ||
} | ||
) | ||
|
||
func init() { | ||
} |
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,110 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/kardianos/service" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
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) { | ||
|
||
prg := &program{ | ||
cmd: cmd, | ||
args: args, | ||
} | ||
|
||
s, err := newSVC(prg, newSVCConfig()) | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
err = s.Run() | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
cmd.Printf("Wiretrustee service is running") | ||
}, | ||
} | ||
) | ||
|
||
var ( | ||
startCmd = &cobra.Command{ | ||
Use: "start", | ||
Short: "starts wiretrustee service", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
s, err := newSVC(&program{}, newSVCConfig()) | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
err = s.Start() | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
cmd.Printf("Wiretrustee service has been started") | ||
}, | ||
} | ||
) | ||
|
||
var ( | ||
stopCmd = &cobra.Command{ | ||
Use: "stop", | ||
Short: "stops wiretrustee service", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
s, err := newSVC(&program{}, newSVCConfig()) | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
err = s.Stop() | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
cmd.Printf("Wiretrustee service has been stopped") | ||
}, | ||
} | ||
) | ||
|
||
var ( | ||
restartCmd = &cobra.Command{ | ||
Use: "restart", | ||
Short: "restarts wiretrustee service", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
s, err := newSVC(&program{}, newSVCConfig()) | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
err = s.Restart() | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
cmd.Printf("Wiretrustee service has been restarted") | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
} |
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,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"runtime" | ||
) | ||
|
||
var ( | ||
installCmd = &cobra.Command{ | ||
Use: "install", | ||
Short: "installs wiretrustee service", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
svcConfig := newSVCConfig() | ||
|
||
svcConfig.Arguments = []string{ | ||
"service", | ||
"run", | ||
"--config", | ||
configPath, | ||
"--log-level", | ||
logLevel, | ||
} | ||
|
||
if runtime.GOOS == "linux" { | ||
// Respected only by systemd systems | ||
svcConfig.Dependencies = []string{"After=network.target syslog.target"} | ||
} | ||
|
||
s, err := newSVC(&program{}, svcConfig) | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
|
||
err = s.Install() | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
cmd.Printf("Wiretrustee service has been installed") | ||
}, | ||
} | ||
) | ||
|
||
var ( | ||
uninstallCmd = &cobra.Command{ | ||
Use: "uninstall", | ||
Short: "uninstalls wiretrustee service from system", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
s, err := newSVC(&program{}, newSVCConfig()) | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
|
||
err = s.Uninstall() | ||
if err != nil { | ||
cmd.PrintErrln(err) | ||
return | ||
} | ||
cmd.Printf("Wiretrustee has been uninstalled") | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
} |
Oops, something went wrong.