initcmd

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 20 Imported by: 0

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

View Source
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.

View Source
const FallbackRegion = "us-east-1"

FallbackRegion is used when neither --region nor AWS_REGION / AWS_DEFAULT_REGION is set.

Variables

ValueFlags lists every flag that sets a value (checked with Changed()).

Functions

func Render

func Render(v Values, version string) ([]byte, error)

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

func Run(ctx context.Context, opts Options, p Prompter, d Detector) error

Run executes `citadel init`. Every check runs before anything is written, so a failure leaves the filesystem untouched.

func RuntimeFromFlags

func RuntimeFromFlags(ecs, lambda bool) (config.Runtime, error)

RuntimeFromFlags maps --ecs/--lambda to a runtime ("" when neither is set).

func SanitizeName

func SanitizeName(s string) string

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

func SplitList(s string) []string

SplitList parses a comma-separated flag or prompt answer: items are trimmed, empty items dropped, duplicates removed (first occurrence wins).

func ValidateAccount

func ValidateAccount(s string) error

ValidateAccount checks a 12-digit AWS account ID.

func ValidateEnvName

func ValidateEnvName(s string) error

ValidateEnvName checks an environment name (used in resource names too).

func ValidateFunctionName

func ValidateFunctionName(s string) error

ValidateFunctionName checks lambda.functionName ("{env}" is allowed).

func ValidateHealthCheckPath

func ValidateHealthCheckPath(s string) error

ValidateHealthCheckPath checks the container health check path.

func ValidateName

func ValidateName(s string) error

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

func ValidateRegion(s string) error

ValidateRegion checks the shape of an AWS region name.

func ValidateSecretName

func ValidateSecretName(s string) error

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.

func (Values) Validate

func (v Values) Validate() error

Validate checks every value for v.Runtime. Render calls it before rendering.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL