service: update windows to use arguments.
This commit is contained in:
+7
-3
@@ -6,8 +6,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"bitbucket.org/kardianos/service2beta"
|
||||
@@ -61,6 +61,9 @@ func (p *program) Stop(s service.Service) error {
|
||||
// Handle service controls (optional).
|
||||
// Run the service.
|
||||
func main() {
|
||||
svcFlag := flag.String("service", "", "Control the system service.")
|
||||
flag.Parse()
|
||||
|
||||
svcConfig := &service.Config{
|
||||
Name: "GoServiceTest",
|
||||
DisplayName: "Go Service Test",
|
||||
@@ -77,6 +80,7 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
err := <-errs
|
||||
@@ -86,8 +90,8 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
err := service.Control(s, os.Args[1])
|
||||
if len(*svcFlag) != 0 {
|
||||
err := service.Control(s, *svcFlag)
|
||||
if err != nil {
|
||||
log.Printf("Valid actions: %q\n", service.ControlAction)
|
||||
log.Fatal(err)
|
||||
|
||||
+22
-6
@@ -5,9 +5,11 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -163,8 +165,20 @@ func (ws *windowsService) Install() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Used if path contains a space.
|
||||
exepath = `"` + exepath + `"`
|
||||
binPath := &bytes.Buffer{}
|
||||
// Quote exe path in case it contains a string.
|
||||
binPath.WriteRune('"')
|
||||
binPath.WriteString(exepath)
|
||||
binPath.WriteRune('"')
|
||||
|
||||
// Arguments are encoded with the binary path to service.
|
||||
// Enclose arguments in quotes. Escape quotes with a backslash.
|
||||
for _, arg := range ws.Arguments {
|
||||
binPath.WriteRune(' ')
|
||||
binPath.WriteString(`"`)
|
||||
binPath.WriteString(strings.Replace(arg, `"`, `\"`, -1))
|
||||
binPath.WriteString(`"`)
|
||||
}
|
||||
m, err := mgr.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -175,10 +189,12 @@ func (ws *windowsService) Install() error {
|
||||
s.Close()
|
||||
return fmt.Errorf("service %s already exists", ws.Name)
|
||||
}
|
||||
s, err = m.CreateService(ws.Name, exepath, mgr.Config{
|
||||
DisplayName: ws.DisplayName,
|
||||
Description: ws.Description,
|
||||
StartType: mgr.StartAutomatic,
|
||||
s, err = m.CreateService(ws.Name, binPath.String(), mgr.Config{
|
||||
DisplayName: ws.DisplayName,
|
||||
Description: ws.Description,
|
||||
StartType: mgr.StartAutomatic,
|
||||
ServiceStartName: ws.UserName,
|
||||
Password: ws.KV.string("Password", ""),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user