-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
942b0f0
commit 401b1f8
Showing
19 changed files
with
409 additions
and
647 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea/ | ||
.vscode/ | ||
|
||
dist/ | ||
log/ | ||
|
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,9 @@ | ||
package cli | ||
|
||
type Arguments struct { | ||
Name string | ||
WorkingDirectory string | ||
LogDirectory string | ||
Command string | ||
CommandArguments []string | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// ExpandArguments replace all "env:" arguments | ||
func ExpandArguments(args []string) []string { | ||
result := []string{} | ||
|
||
for _, arg := range args { | ||
if strings.HasPrefix(arg, "env:") { | ||
result = append(result, os.Getenv(strings.TrimLeft(arg, "env:"))) | ||
} else { | ||
result = append(result, arg) | ||
} | ||
} | ||
|
||
return result | ||
} |
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,23 @@ | ||
package cli | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_ExpandArguments(t *testing.T) { | ||
require := require.New(t) | ||
|
||
os.Setenv("_ENV_VAL_", "123") | ||
result := ExpandArguments([]string{ | ||
"normal", | ||
"-test", "env:_ENV_VAL_", | ||
}) | ||
|
||
require.Equal(result, []string{ | ||
"normal", | ||
"-test", "123", | ||
}) | ||
} |
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,22 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// Validate command line arguments | ||
func Validate(arguments Arguments) error { | ||
if len(arguments.Name) == 0 { | ||
return errors.New("Name not set (-name)") | ||
} | ||
|
||
if len(arguments.WorkingDirectory) == 0 { | ||
return errors.New("Working directory not set (-workdir)") | ||
} | ||
|
||
if len(arguments.Command) == 0 { | ||
return errors.New("Command not set (-cmd)") | ||
} | ||
|
||
return nil | ||
} |
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,23 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Validate(t *testing.T) { | ||
require := require.New(t) | ||
|
||
args := Arguments{} | ||
require.Error(Validate(args)) | ||
|
||
args.Name = "123" | ||
require.Error(Validate(args)) | ||
|
||
args.WorkingDirectory = "C:/" | ||
require.Error(Validate(args)) | ||
|
||
args.Command = "_unknown_" | ||
require.NoError(Validate(args)) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,13 @@ | ||
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e h1:9vRrk9YW2BTzLP0VCB9ZDjU4cPqkg+IDWL7XgxA1yxQ= | ||
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
golang.org/x/sys v0.0.0-20191206214222-bc7efcf3b808 h1:FxzWWtFesyhA6D5ohpa+/OJY+9XOBoQtdnjgepvfOIs= | ||
golang.org/x/sys v0.0.0-20191206214222-bc7efcf3b808/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
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 |
---|---|---|
@@ -1,54 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/matthiasng/service-shark/cli" | ||
"github.com/matthiasng/service-shark/command" | ||
"github.com/matthiasng/service-shark/service" | ||
) | ||
|
||
// #todo working directory | ||
|
||
// parser := argparse.NewParser("service-wrapper", `Run a "-command" with "-arguments" as service`) | ||
|
||
// serviceName := parser.String("n", "name", &argparse.Options{ | ||
// Required: true, | ||
// Help: "Servicename", | ||
// }) | ||
// logDirectory := parser.String("l", "logdirectory", &argparse.Options{ | ||
// Required: true, | ||
// Help: "Log directory", | ||
// }) | ||
// command := parser.String("c", "command", &argparse.Options{ | ||
// Required: true, | ||
// Help: "Command", | ||
// }) | ||
// arguments := parser.List("a", "arg", &argparse.Options{ | ||
// Required: true, | ||
// Help: `Command arguments. Example: '... -a "-key" -a "value" -a "--key2" -a "value"'`, | ||
// }) | ||
|
||
// err := parser.Parse(os.Args) | ||
// if err != nil { | ||
// fmt.Print(parser.Usage(err)) | ||
// os.Exit(2) | ||
// } | ||
|
||
// cli.BindArguments(*arguments), | ||
var arguments = cli.Arguments{} | ||
|
||
func init() { | ||
flag.CommandLine.Usage = func() { | ||
fmt.Println("Usage of Service Shark:") | ||
flag.PrintDefaults() | ||
} | ||
|
||
flag.StringVar(&arguments.Name, "name", "", "Service name [required]") | ||
flag.StringVar(&arguments.WorkingDirectory, "workdir", "", "Working directory [required]") | ||
flag.StringVar(&arguments.LogDirectory, "logdir", "./log", "Log directory. Absolute path or relative to working directory.") | ||
flag.StringVar(&arguments.Command, "cmd", "", "Command [required]") | ||
|
||
flag.Parse() | ||
} | ||
|
||
func main() { | ||
if err := service.FixWorkingDirectory(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
arguments.CommandArguments = cli.ExpandArguments(flag.Args()) | ||
|
||
if err := cli.Validate(arguments); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := os.Chdir(arguments.WorkingDirectory); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
host := command.Host{ | ||
CmdConfig: command.Config{ | ||
//Name: "powershell", | ||
Name: "C:/Program Files/PowerShell/7-preview/preview/pwsh-preview.cmd", | ||
Arguments: []string{ | ||
`P:\_dev\projects\service-shark\example\test-service.ps1`, | ||
}, | ||
}, | ||
LogDirecotry: "c:/tmp", | ||
Arguments: arguments, | ||
} | ||
|
||
if err := service.Run(&host, "todo"); err != nil { | ||
log.Fatalf("[error] - %v", err) | ||
if err := service.Run(&host); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.