Maint: cli package interface rework

- return concrete struct type
- split interface is sub-interfaces
This commit is contained in:
Quentin McGaw (desktop)
2021-07-23 18:52:38 +00:00
parent 0b985e8c35
commit d9ca0deb08
6 changed files with 30 additions and 19 deletions

View File

@@ -2,6 +2,6 @@ package cli
import "context" import "context"
func (c *cli) CI(context context.Context) error { func (c *CLI) CI(context context.Context) error {
return nil return nil
} }

View File

@@ -1,26 +1,21 @@
// Package cli defines an interface CLI to run command line operations. // Package cli defines an interface CLI to run command line operations.
package cli package cli
import ( var _ CLIer = (*CLI)(nil)
"context"
"github.com/qdm12/golibs/logging" type CLIer interface {
"github.com/qdm12/golibs/params" ClientKeyFormatter
) HealthChecker
OpenvpnConfigMaker
type CLI interface { Updater
ClientKey(args []string) error
HealthCheck(ctx context.Context, env params.Env, logger logging.Logger) error
OpenvpnConfig(logger logging.Logger) error
Update(ctx context.Context, args []string, logger logging.Logger) error
} }
type cli struct { type CLI struct {
repoServersPath string repoServersPath string
} }
func New() CLI { func New() *CLI {
return &cli{ return &CLI{
repoServersPath: "./internal/constants/servers.json", repoServersPath: "./internal/constants/servers.json",
} }
} }

View File

@@ -10,7 +10,11 @@ import (
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
) )
func (c *cli) ClientKey(args []string) error { type ClientKeyFormatter interface {
ClientKey(args []string) error
}
func (c *CLI) ClientKey(args []string) error {
flagSet := flag.NewFlagSet("clientkey", flag.ExitOnError) flagSet := flag.NewFlagSet("clientkey", flag.ExitOnError)
filepath := flagSet.String("path", constants.ClientKey, "file path to the client.key file") filepath := flagSet.String("path", constants.ClientKey, "file path to the client.key file")
if err := flagSet.Parse(args); err != nil { if err := flagSet.Parse(args); err != nil {

View File

@@ -12,7 +12,11 @@ import (
"github.com/qdm12/golibs/params" "github.com/qdm12/golibs/params"
) )
func (c *cli) HealthCheck(ctx context.Context, env params.Env, type HealthChecker interface {
HealthCheck(ctx context.Context, env params.Env, logger logging.Logger) error
}
func (c *CLI) HealthCheck(ctx context.Context, env params.Env,
logger logging.Logger) error { logger logging.Logger) error {
// Extract the health server port from the configuration. // Extract the health server port from the configuration.
config := configuration.Health{} config := configuration.Health{}

View File

@@ -13,7 +13,11 @@ import (
"github.com/qdm12/golibs/params" "github.com/qdm12/golibs/params"
) )
func (c *cli) OpenvpnConfig(logger logging.Logger) error { type OpenvpnConfigMaker interface {
OpenvpnConfig(logger logging.Logger) error
}
func (c *CLI) OpenvpnConfig(logger logging.Logger) error {
var allSettings configuration.Settings var allSettings configuration.Settings
err := allSettings.Read(params.NewEnv(), logger) err := allSettings.Read(params.NewEnv(), logger)
if err != nil { if err != nil {

View File

@@ -25,7 +25,11 @@ var (
ErrWriteToFile = errors.New("cannot write updated information to file") ErrWriteToFile = errors.New("cannot write updated information to file")
) )
func (c *cli) Update(ctx context.Context, args []string, logger logging.Logger) error { type Updater interface {
Update(ctx context.Context, args []string, logger logging.Logger) error
}
func (c *CLI) Update(ctx context.Context, args []string, logger logging.Logger) error {
options := configuration.Updater{CLI: true} options := configuration.Updater{CLI: true}
var endUserMode, maintainerMode, updateAll bool var endUserMode, maintainerMode, updateAll bool
flagSet := flag.NewFlagSet("update", flag.ExitOnError) flagSet := flag.NewFlagSet("update", flag.ExitOnError)