Maint: split out OpenVPN version functions to openvpn/config/version.go
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"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)
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
39
internal/openvpn/config/version.go
Normal file
39
internal/openvpn/config/version.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user