Add 'dis' component

This commit adds a new 'dis' component to the distillery that serves a
list of all known instances for the moment.
This commit is contained in:
Tom Wiesing 2022-09-09 17:10:24 +02:00
parent 35bb95c5ca
commit 4b357476a3
No known key found for this signature in database
43 changed files with 434 additions and 167 deletions

14
core/core.go Normal file
View file

@ -0,0 +1,14 @@
// Package core implements the core of the WissKI Distillery and the wdcli executable.
// It does not depend on any other packages.
package core
// BaseDirectoryDefault is the default deploy directory to load the distillery from.
const BaseDirectoryDefault = "/var/www/deploy"
// Executable is the name of the 'wdcli' executable.
// It should be located inside the deployment directory.
const Executable = "wdcli"
// Config file is the name of the config file.
// It should be located inside the deployment directory.
const ConfigFile = ".env"

8
core/flags.go Normal file
View file

@ -0,0 +1,8 @@
package core
// Flags are global flags for the wdcli executable
type Flags struct {
ConfigPath string `short:"c" long:"config" description:"Path to distillery configuration file"`
InternalInDocker bool `long:"internal-in-docker" description:"Internal Flag to signal the shell that it is running inside a docker stack belonging to the distillery"`
}

71
core/meta.go Normal file
View file

@ -0,0 +1,71 @@
package core
import (
"errors"
"io/fs"
"os"
"os/user"
"path/filepath"
"strings"
)
// MetaConfigFile is the path to a configuration file that contains the path to the last used wdcli executable.
// It is expected to be in the current user's home directory.
//
// You probably want to use [MetaConfigPath] instead.
//
// It should contain the path to a deployment directory.
const MetaConfigFile = "." + Executable
// MetaConfigPath returns the full path to the MetaConfigPath()
func MetaConfigPath() (string, error) {
// find the current user
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, MetaConfigFile), nil
}
var errReadBaseDirectoryEmpty = errors.New("ReadBaseDirectory: Directory is empty")
// ReadBaseDirectory reads the base deployment directory from the environment.
// Use [ParamsFromEnv] to initialize parameters completely.
//
// It does not perform any reading of files.
func ReadBaseDirectory() (value string, err error) {
// get the path!
path, err := MetaConfigPath()
if err != nil {
return "", err
}
// read the meta config file!
contents, err := os.ReadFile(path)
if err != nil {
return "", err
}
// and trim the spaces!
value = strings.TrimSpace(string(contents))
// check that it is actually set!
if len(value) == 0 {
return "", errReadBaseDirectoryEmpty
}
// and return it!
return value, nil
}
// WriteBaseDirectory writes the base directory to the environment, or returns an error
func WriteBaseDirectory(dir string) error {
// get the path!
path, err := MetaConfigPath()
if err != nil {
return err
}
// just put the directory inside it!
return os.WriteFile(path, []byte(dir), fs.ModePerm)
}

31
core/params.go Normal file
View file

@ -0,0 +1,31 @@
package core
import (
"os"
"path/filepath"
)
// Params are used to initialize the excutable.
type Params struct {
ConfigPath string // ConfigPath is the path to the configuration file for the distillery
}
// ParamsFromEnv creates a new set of parameters from the environment.
// Uses [ReadBaseDirectory] or falls back to [BaseDirectoryDefault].
func ParamsFromEnv() (params Params, err error) {
// try to read the base directory!
value, err := ReadBaseDirectory()
switch {
case os.IsNotExist(err):
params.ConfigPath = BaseDirectoryDefault
case err == nil:
params.ConfigPath = value
default:
return params, err
}
// and add the configuration file name to it!
params.ConfigPath = filepath.Join(params.ConfigPath, ConfigFile)
return params, nil
}

26
core/requirements.go Normal file
View file

@ -0,0 +1,26 @@
package core
import (
"github.com/tkw1536/goprogram"
"github.com/tkw1536/goprogram/meta"
)
// Requirements are requirements for the WissKI Distillery
type Requirements struct {
// Do we need an installed distillery?
NeedsDistillery bool
}
// AllowsFlag checks if the provided flag may be passed to fullfill this requirement
// By default it is used only for help page generation, and may be inaccurate.
func (r Requirements) AllowsFlag(flag meta.Flag) bool {
return r.NeedsDistillery
}
// Validate validates if this requirement is fullfilled for the provided global flags.
// It should return either nil, or an error of type exit.Error.
//
// Validate does not take into account AllowsOption, see ValidateAllowedOptions.
func (r Requirements) Validate(arguments goprogram.Arguments[Flags]) error {
return nil
}