constellation/cli/internal/cmd/cmd.go
Daniel Weiße c52086c5ff
cli: refactor flag parsing code (#2425)
Signed-off-by: Daniel Weiße <dw@edgeless.systems>
2023-10-16 15:05:29 +02:00

78 lines
2.0 KiB
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
/*
Package cmd provides the Constellation CLI.
It is responsible for the interaction with the user.
Logic should be kept to input/output parsing whenever possible.
Any more complex code should usually be implemented in one of the other CLI packages.
The code here should be kept as cloud provider agnostic as possible.
Any CSP specific tasks should be handled by the "cloudcmd" package.
All filepaths handled by the CLI code should originate from here.
Common filepaths are defined as constants in the global "/internal/constants" package.
To generate workspace correct filepaths for printing, use the functions from the "workspace" package.
*/
package cmd
import (
"errors"
"fmt"
"github.com/edgelesssys/constellation/v2/cli/internal/cmd/pathprefix"
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
"github.com/spf13/pflag"
)
// rootFlags are flags defined on the root command.
// They are available to all subcommands.
type rootFlags struct {
pathPrefixer pathprefix.PathPrefixer
tfLogLevel terraform.LogLevel
debug bool
force bool
}
// parse flags into the rootFlags struct.
func (f *rootFlags) parse(flags *pflag.FlagSet) error {
var errs error
workspace, err := flags.GetString("workspace")
if err != nil {
errs = errors.Join(err, fmt.Errorf("getting 'workspace' flag: %w", err))
}
f.pathPrefixer = pathprefix.New(workspace)
tfLogString, err := flags.GetString("tf-log")
if err != nil {
errs = errors.Join(err, fmt.Errorf("getting 'tf-log' flag: %w", err))
}
f.tfLogLevel, err = terraform.ParseLogLevel(tfLogString)
if err != nil {
errs = errors.Join(err, fmt.Errorf("parsing 'tf-log' flag: %w", err))
}
f.debug, err = flags.GetBool("debug")
if err != nil {
errs = errors.Join(err, fmt.Errorf("getting 'debug' flag: %w", err))
}
f.force, err = flags.GetBool("force")
if err != nil {
errs = errors.Join(err, fmt.Errorf("getting 'force' flag: %w", err))
}
return errs
}
func must(err error) {
if err != nil {
panic(err)
}
}