Move internal/core => internal/cli
This commit is contained in:
parent
8d2855fdcb
commit
10df1c3243
45 changed files with 113 additions and 143 deletions
4
internal/cli/cli.go
Normal file
4
internal/cli/cli.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
// Package cli contains helper code for the command line executable
|
||||
package cli
|
||||
|
||||
//go:generate gogenlicense -n LegalNotices -m
|
||||
1546
internal/cli/cli_notices.go
Executable file
1546
internal/cli/cli_notices.go
Executable file
File diff suppressed because one or more lines are too long
8
internal/cli/flags.go
Normal file
8
internal/cli/flags.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package cli
|
||||
|
||||
// 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
internal/cli/meta.go
Normal file
71
internal/cli/meta.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/bootstrap"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
// 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.
|
||||
//
|
||||
// It should contain the path to a deployment directory.
|
||||
const metaConfigFile = "." + bootstrap.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(env environment.Environment) (value string, err error) {
|
||||
// get the path!
|
||||
path, err := MetaConfigPath()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// read the meta config file!
|
||||
contents, err := environment.ReadFile(env, 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(env environment.Environment, dir string) error {
|
||||
// get the path!
|
||||
path, err := MetaConfigPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// just put the directory inside it!
|
||||
return environment.WriteFile(env, path, []byte(dir), fs.ModePerm)
|
||||
}
|
||||
51
internal/cli/params.go
Normal file
51
internal/cli/params.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/bootstrap"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
// Params are used to initialize the excutable.
|
||||
type Params struct {
|
||||
ConfigPath string // ConfigPath is the path to the configuration file for the distillery
|
||||
Context context.Context // Context 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(environment.Native{}) // TODO: Are we sure about the native environment here?
|
||||
switch {
|
||||
case environment.IsNotExist(err):
|
||||
params.ConfigPath = bootstrap.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, bootstrap.ConfigFile)
|
||||
|
||||
// generate a new context
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
params.Context = ctx
|
||||
|
||||
// cancel the context on an interrupt
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
go func() {
|
||||
<-c
|
||||
cancel()
|
||||
}()
|
||||
|
||||
// and return the params!
|
||||
return params, nil
|
||||
}
|
||||
26
internal/cli/requirements.go
Normal file
26
internal/cli/requirements.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package cli
|
||||
|
||||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue