Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit c440a6e

Browse files
Merge pull request #10 from NETWAYS/configurable_timeout
Make timeout configurable
2 parents 1e070be + 6d4252b commit c440a6e

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

.golangci.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ run:
33

44
linters:
55
disable-all: false
6-
enable:
7-
- funlen
8-
- dogsled
9-
- dupl
10-
- lll
11-
- whitespace
12-
- wsl
136
presets:
147
- bugs
158
- unused

api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ type RestAPI struct {
1818
Client *http.Client
1919
}
2020

21-
func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{}) (*APICheckResult, error) {
21+
func (a RestAPI) ExecuteCheck(command string, arguments map[string]interface{}, timeout uint32) (*APICheckResult, error) { //nolint:lll
2222
// Build body
2323
body, err := json.Marshal(arguments)
2424
if err != nil {
2525
return nil, fmt.Errorf("could not build JSON body: %w", err)
2626
}
2727

2828
// With timeout
29-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
29+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
3030
defer cancel()
3131

3232
// Build request

config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"crypto/tls"
55
"errors"
66
"fmt"
7-
flag "github.com/spf13/pflag"
87
"net/http"
98
"os"
9+
10+
flag "github.com/spf13/pflag"
1011
)
1112

1213
const (
@@ -15,6 +16,7 @@ const (
1516
)
1617

1718
type Config struct {
19+
Timeout uint32
1820
API string
1921
Command string
2022
Arguments map[string]interface{}
@@ -50,6 +52,7 @@ func (c *Config) BuildFlags(fs *flag.FlagSet) {
5052
fs.BoolVar(&c.Insecure, "insecure", c.Insecure, "Ignore any certificate checks")
5153
fs.BoolVar(&c.Debug, "debug", c.Debug, "Enable debug logging")
5254
fs.BoolVar(&c.PrintVersion, "version", false, "Print program version")
55+
fs.Uint32Var(&c.Timeout, "timeout", 10, "Powershell connector timeout")
5356
}
5457

5558
// ParseConfigFromFlags to be called to parse CLI arguments and return the built Config struct.

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"os"
7+
68
"github.com/NETWAYS/go-check"
79
log "github.com/sirupsen/logrus"
810
flag "github.com/spf13/pflag"
9-
"os"
1011
)
1112

1213
const License = `
@@ -43,7 +44,7 @@ func main() {
4344

4445
api := RestAPI{URL: config.API, Client: config.NewClient()}
4546

46-
result, err := api.ExecuteCheck(config.Command, config.Arguments)
47+
result, err := api.ExecuteCheck(config.Command, config.Arguments, config.Timeout)
4748
if err != nil {
4849
check.ExitError(err)
4950
}

0 commit comments

Comments
 (0)