Documentation
¶
Overview ¶
Package initcmd implements `citadel init`: it gathers values from flags, the environment and prompts, renders a commented citadel.yml from an embedded per-runtime template, and creates the project's .citadel/ dir.
Index ¶
- Constants
- Variables
- func Render(v Values, version string) ([]byte, error)
- func Run(ctx context.Context, opts Options, p Prompter, d Detector) error
- func RuntimeFromFlags(ecs, lambda bool) (config.Runtime, error)
- func SanitizeName(s string) string
- func SplitList(s string) []string
- func ValidateAccount(s string) error
- func ValidateEnvName(s string) error
- func ValidateFunctionName(s string) error
- func ValidateHealthCheckPath(s string) error
- func ValidateName(s string) error
- func ValidateRegion(s string) error
- func ValidateSecretName(s string) error
- type Detector
- type Options
- type Prompter
- type Values
Constants ¶
const ( FlagECS = "ecs" FlagLambda = "lambda" FlagName = "name" FlagRegion = "region" FlagAccount = "account" FlagEnvs = "envs" FlagPort = "port" FlagCPU = "cpu" FlagMemory = "memory" FlagHealthCheckPath = "health-check-path" FlagFunctionName = "function-name" FlagSecrets = "secrets" )
Flag names shared by the cobra command and Options.Provided.
const FallbackRegion = "us-east-1"
FallbackRegion is used when neither --region nor AWS_REGION / AWS_DEFAULT_REGION is set.
Variables ¶
var ValueFlags = []string{ FlagECS, FlagLambda, FlagName, FlagRegion, FlagAccount, FlagEnvs, FlagPort, FlagCPU, FlagMemory, FlagHealthCheckPath, FlagFunctionName, FlagSecrets, }
ValueFlags lists every flag that sets a value (checked with Changed()).
Functions ¶
func Render ¶
Render produces citadel.yml content for v and checks it with config.Parse, so `citadel init` can never write a file that citadel itself would reject. Values are validated first, which also makes it safe to emit them unquoted.
func Run ¶
Run executes `citadel init`. Every check runs before anything is written, so a failure leaves the filesystem untouched.
func RuntimeFromFlags ¶
RuntimeFromFlags maps --ecs/--lambda to a runtime ("" when neither is set).
func SanitizeName ¶
SanitizeName turns a directory name into a default project name: lowercase ASCII letters and digits, with each run of other characters collapsed to one dash and leading/trailing dashes dropped. It falls back to "app".
func SplitList ¶
SplitList parses a comma-separated flag or prompt answer: items are trimmed, empty items dropped, duplicates removed (first occurrence wins).
func ValidateAccount ¶
ValidateAccount checks a 12-digit AWS account ID.
func ValidateEnvName ¶
ValidateEnvName checks an environment name (used in resource names too).
func ValidateFunctionName ¶
ValidateFunctionName checks lambda.functionName ("{env}" is allowed).
func ValidateHealthCheckPath ¶
ValidateHealthCheckPath checks the container health check path.
func ValidateName ¶
ValidateName checks a project name. It becomes part of every AWS resource name ("<name>-<env>-cluster", ...), so it is restricted to lowercase DNS-ish.
func ValidateRegion ¶
ValidateRegion checks the shape of an AWS region name.
func ValidateSecretName ¶
ValidateSecretName checks an env-var name declared under secrets:.
Types ¶
type Detector ¶
type Detector interface {
// Region returns the region from the environment, or "" when unset.
Region() string
// Account returns the AWS account of the current credentials.
Account(ctx context.Context, region string) (string, error)
}
Detector finds defaults from the environment.
func NewEnvDetector ¶
func NewEnvDetector() Detector
NewEnvDetector returns a Detector that reads AWS_REGION / AWS_DEFAULT_REGION and asks STS GetCallerIdentity for the account.
type Options ¶
type Options struct {
ConfigPath string // citadel.yml to write; .citadel/ goes next to it
Values Values // flag values, with DefaultValues for unset flags
Provided map[string]bool // flags the user passed explicitly
Yes bool // never prompt (also set for non-TTY stdin)
Force bool // overwrite an existing citadel.yml
DryRun bool // print what would be written; write nothing
Version string // citadel version, recorded in generated files
Out io.Writer
Now func() time.Time // nil means time.Now
}
Options configures one `citadel init`.
type Prompter ¶
type Prompter interface {
Select(title string, options []string, def string) (string, error)
Input(title, def string, validate func(string) error) (string, error)
}
Prompter asks the user for values. The real one uses huh; tests use fakes.
func NewHuhPrompter ¶
func NewHuhPrompter() Prompter
NewHuhPrompter returns the interactive terminal Prompter.
type Values ¶
type Values struct {
Runtime config.Runtime
Name string
Region string
Account string
Envs []string
Port int
CPU int
Memory int
HealthCheckPath string
FunctionName string
Secrets []string
}
Values are the answers that fill a citadel.yml template.
func DefaultValues ¶
func DefaultValues() Values
DefaultValues returns the values used when a flag is not passed and nothing is detected. Name, region and account are filled in by Run.