Skip to content

Commit

Permalink
github workflow, new arg forwarding, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasng committed Dec 1, 2019
1 parent 7f05752 commit 4b6bab4
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 169 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Build and Test

on:
push:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x

- name: Check out code
uses: actions/checkout@v1

- name: Install golangci-lint
run: |
go get github.com/golangci/golangci-lint/cmd/golangci-lint
- name: Run linters
run: |
export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
golangci-lint -E bodyclose,misspell,gocyclo,dupl,gofmt,golint,unconvert,goimports,depguard,gocritic,funlen,interfacer run
test:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x

- name: Check out code
uses: actions/checkout@v1

- name: Run Unit tests.
run: go test -v ./...

coverage:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x

- name: Check out code
uses: actions/checkout@v1

- name: Run coverage
run: go test -v -covermode=count -coverprofile coverage.out ./...

- name: Upload Coverage report to CodeCov
uses: codecov/codecov-action@v1.0.0
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./coverage.out

build:
runs-on: ubuntu-latest
needs: [lint, test]
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x
- name: Checkout code
uses: actions/checkout@v1
- name: build
run: |
export GO111MODULE=on
GOOS=windows GOARCH=amd64 go build -o bin/ci-test-windows-amd64.exe
GOOS=linux GOARCH=amd64 go build -o bin/ci-test-linux-amd64
- name: upload artifacts
uses: actions/upload-artifact@master
with:
name: binaries
path: bin/
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
create:
tags:
- v[0-9]+.[0-9]+.[0-9]+

jobs:
releaser:
name: Release on GitHub
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master

- name: Set up Go
uses: actions/setup-go@master
with:
go-version: 1.13.x

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 BioApfelsaft
Copyright (c) 2019 matthiasng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
21 changes: 21 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cli

import (
"os"
"strings"
)

// BindArguments replace all "bind:" arguments
func BindArguments(args []string) []string {
result := []string{}

for _, arg := range args {
if strings.HasPrefix(arg, "bind:") {
result = append(result, os.Getenv(strings.TrimLeft(arg, "bind:")))
} else {
result = append(result, arg)
}
}

return result
}
1 change: 1 addition & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cli
12 changes: 8 additions & 4 deletions example/test-example-service.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@

$currentPwshExe = (Get-Process -id $pid | Get-Item).FullName
$psTestScript = "$PSScriptRoot/test-service.ps1"

$serviceName = "ExampleService-" + (New-Guid)

$serviceLogDirecotry = "$PSScriptRoot/log"
$serviceCommand = $currentPwshExe
$serviceArguments = "$PSScriptRoot/test-service.ps1 -Message 'TestMessage with space!' -PathVar bind:PATH"

$serviceBinPath = "$PSScriptRoot/../service-wrapper.exe"
$serviceBinPath += ' -logdir "' + $serviceLogDirecotry + '"'
$serviceBinPath += ' -command "' + $serviceCommand + '"'
$serviceBinPath += ' -arguments "' + $serviceArguments + '"'
$serviceBinPath += ' -n "' + $serviceName + '"'
$serviceBinPath += ' -l "' + $serviceLogDirecotry + '"'
$serviceBinPath += ' -c "' + $serviceCommand + '"'
$serviceBinPath += ' -a "' + $psTestScript + '"'
$serviceBinPath += ' -a "-Message" -a "TestMessage with space!"'
$serviceBinPath += ' -a "-PathVar" -a "-bind:PATH"'


Write-Host "New-Service:"
Write-Host $serviceBinPath
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module github.com/bioapfelsaft/service-wrapper
module github.com/matthiasng/service-wrapper

go 1.13

require golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9
require (
github.com/akamensky/argparse v0.0.0-20191006154803-1427fe674291
github.com/bioapfelsaft/service-wrapper v0.0.0-20191201190517-7f0575270b8c
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/akamensky/argparse v0.0.0-20191006154803-1427fe674291 h1:EQ9p1v9+urDzbGQfAcBf9fGuDtYqFNE7YJHZ54TWPTk=
github.com/akamensky/argparse v0.0.0-20191006154803-1427fe674291/go.mod h1:pdh+2piXurh466J9tqIqq39/9GO2Y8nZt6Cxzu18T9A=
github.com/bioapfelsaft/service-wrapper v0.0.0-20191201190517-7f0575270b8c h1:/8y95ksXi6QqXVDVMiEHYiIIpB3WPaIyLjT8bwnE8XA=
github.com/bioapfelsaft/service-wrapper v0.0.0-20191201190517-7f0575270b8c/go.mod h1:otu6HFi3pDmbkyG33JQ9iSowjRYss5nwcZWgFFW8qIg=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
118 changes: 38 additions & 80 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,40 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"strings"

"github.com/bioapfelsaft/service-wrapper/service"
"github.com/akamensky/argparse"
"github.com/matthiasng/service-wrapper/cli"
"github.com/matthiasng/service-wrapper/service"
"golang.org/x/sys/windows/svc"
)

// #todo config ?
// 1. -bind:NAME -> -NAME <Value>
// 2. -NAME cfg:VALUE_NAME -> -NAME <Value>

// wrapper.exe -cfg-file test.yaml
// $env:NAME="123" + binding

// load order
// 1. config file
// 2. environment
// 3. wrapper.exe -arguments "..."

func parseCommandLine(commandArgs string) ([]string, error) {
args := []string{}

insideQuotes := false
quoteStartPos := 0

currentArg := ""

for i := 0; i < len(commandArgs); i++ {
c := commandArgs[i]

if c == '\'' {
quoteStartPos = i
if insideQuotes && (i+1) < len(commandArgs) && commandArgs[i+1] != ' ' {
return []string{}, fmt.Errorf("end quote must be followed by a space - position %d", i)
}

insideQuotes = !insideQuotes
continue
}

if c == ' ' && !insideQuotes {
if len(currentArg) > 0 {
args = append(args, currentArg)
currentArg = ""
}

continue
}

currentArg += string(c)
}

if insideQuotes {
return []string{}, fmt.Errorf("Unclosed quote in command line - start position %d", quoteStartPos)
}

args = append(args, currentArg)
return args, nil
}

func main() {
serviceName := "WinServiceWrapper" // #todo ?

command := flag.String("command", "", "Command")
logDirectory := flag.String("logdir", "", "Log directory")
arguments := flag.String("arguments", "", "Arguments")

flag.Parse()

//
args, err := parseCommandLine(*arguments)
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 {
log.Fatalf("invalid arguments: %v", err)
}

//
for i := 0; i < len(args); i++ {
if strings.HasPrefix(args[i], "bind:") {
args[i] = os.Getenv(strings.TrimLeft(args[i], "bind:"))
}
fmt.Print(parser.Usage(err))
os.Exit(2)
}

//
Expand All @@ -93,10 +44,17 @@ func main() {
}

//
service.Run(service.Configuration{
ServiceName: serviceName,
Command: *command,
Arguments: args,
LogDirectory: *logDirectory,
}, isAnInteractiveSession)
wrapper := service.Wrapper{
Config: service.Configuration{
ServiceName: *serviceName,
Command: *command,
Arguments: cli.BindArguments(*arguments),
LogDirectory: *logDirectory,
IsAnInteractiveSession: isAnInteractiveSession,
},
}
err = wrapper.Run()
if err != nil {
log.Fatalf("wrapper.Run(): %v", err)
}
}
Loading

0 comments on commit 4b6bab4

Please sign in to comment.