From 9e5400f52da2b19018b5d4af28ae2f6b15f2691a Mon Sep 17 00:00:00 2001 From: "Quentin McGaw (desktop)" Date: Wed, 18 Aug 2021 20:46:20 +0000 Subject: [PATCH] Maint: split out OpenVPN version functions to openvpn/config/version.go --- internal/openvpn/config/command.go | 31 ------------------------ internal/openvpn/config/version.go | 39 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 internal/openvpn/config/version.go diff --git a/internal/openvpn/config/command.go b/internal/openvpn/config/command.go index 129555a7..668972a6 100644 --- a/internal/openvpn/config/command.go +++ b/internal/openvpn/config/command.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "os/exec" - "strings" "syscall" "github.com/qdm12/gluetun/internal/constants" @@ -44,33 +43,3 @@ func (c *Configurator) Start(ctx context.Context, version string, flags []string return c.cmder.Start(cmd) } - -type VersionGetter interface { - Version24(ctx context.Context) (version string, err error) - Version25(ctx context.Context) (version string, err error) -} - -func (c *Configurator) Version24(ctx context.Context) (version string, err error) { - return c.version(ctx, binOpenvpn24) -} - -func (c *Configurator) Version25(ctx context.Context) (version string, err error) { - return c.version(ctx, binOpenvpn25) -} - -var ErrVersionTooShort = errors.New("version output is too short") - -func (c *Configurator) version(ctx context.Context, binName string) (version string, err error) { - cmd := exec.CommandContext(ctx, binName, "--version") - output, err := c.cmder.Run(cmd) - if err != nil && err.Error() != "exit status 1" { - return "", err - } - firstLine := strings.Split(output, "\n")[0] - words := strings.Fields(firstLine) - const minWords = 2 - if len(words) < minWords { - return "", fmt.Errorf("%w: %s", ErrVersionTooShort, firstLine) - } - return words[1], nil -} diff --git a/internal/openvpn/config/version.go b/internal/openvpn/config/version.go new file mode 100644 index 00000000..ccae9393 --- /dev/null +++ b/internal/openvpn/config/version.go @@ -0,0 +1,39 @@ +package config + +import ( + "context" + "errors" + "fmt" + "os/exec" + "strings" +) + +type VersionGetter interface { + Version24(ctx context.Context) (version string, err error) + Version25(ctx context.Context) (version string, err error) +} + +func (c *Configurator) Version24(ctx context.Context) (version string, err error) { + return c.version(ctx, binOpenvpn24) +} + +func (c *Configurator) Version25(ctx context.Context) (version string, err error) { + return c.version(ctx, binOpenvpn25) +} + +var ErrVersionTooShort = errors.New("version output is too short") + +func (c *Configurator) version(ctx context.Context, binName string) (version string, err error) { + cmd := exec.CommandContext(ctx, binName, "--version") + output, err := c.cmder.Run(cmd) + if err != nil && err.Error() != "exit status 1" { + return "", err + } + firstLine := strings.Split(output, "\n")[0] + words := strings.Fields(firstLine) + const minWords = 2 + if len(words) < minWords { + return "", fmt.Errorf("%w: %s", ErrVersionTooShort, firstLine) + } + return words[1], nil +}