iacargs

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package iacargs provides types and utilities for handling IaC (terraform/tofu) CLI arguments.

Index

Constants

View Source
const (

	// CommandNameDestroy is the terraform destroy command name
	CommandNameDestroy = "destroy"
)

Variables

This section is empty.

Functions

func IsKnownSubCommand

func IsKnownSubCommand(arg string) bool

IsKnownSubCommand returns true if arg is a known terraform subcommand.

Types

type IacArgs

type IacArgs struct {
	Command    string   // e.g., "apply", "plan", "providers"
	SubCommand []string // e.g., "lock" in "providers lock -platform=..."
	Flags      []string // e.g., "-input=false", "-auto-approve"
	Arguments  []string // e.g., plan files, resource addresses
}

IacArgs represents parsed IaC (terraform/tofu) CLI arguments with separate command, flags, and arguments fields. Provides a builder pattern for constructing CLI arguments.

Structure: [Command] [SubCommand...] [Flags...] [Arguments...] - Command: main terraform command (e.g., "apply", "providers") - SubCommand: non-flag args before any flags (e.g., "lock" in "providers lock") - Flags: all flags with their values - Arguments: non-flag args after flags (e.g., plan files)

func New

func New(args ...string) *IacArgs

New creates IacArgs from strings, parsing command/flags/arguments.

func (*IacArgs) AddFlagIfNotPresent

func (a *IacArgs) AddFlagIfNotPresent(flag string) *IacArgs

AddFlagIfNotPresent adds a flag only if not already present.

func (*IacArgs) AppendArgument

func (a *IacArgs) AppendArgument(args ...string) *IacArgs

AppendArgument adds argument(s) and returns self for chaining.

func (*IacArgs) AppendFlag

func (a *IacArgs) AppendFlag(flags ...string) *IacArgs

AppendFlag adds flag(s) and returns self for chaining.

func (*IacArgs) AppendSubCommand

func (a *IacArgs) AppendSubCommand(subs ...string) *IacArgs

AppendSubCommand adds subcommand(s) and returns self for chaining.

func (*IacArgs) Clone

func (a *IacArgs) Clone() *IacArgs

Clone returns a deep copy of IacArgs. Note: This performs a deep copy of slices (Command, SubCommand, Flags, Arguments). If IacArgs is extended with pointer fields or nested structs in the future, this method must be updated to ensure deep copying of those fields as well.

func (*IacArgs) Contains

func (a *IacArgs) Contains(target string) bool

Contains checks if the args contain the target (in command, subcommand, flags, or arguments).

func (*IacArgs) First

func (a *IacArgs) First() string

First returns the command (first element).

func (*IacArgs) HasFlag

func (a *IacArgs) HasFlag(name string) bool

HasFlag checks if flag exists (handles -flag, --flag and -flag=value formats). Note: Values starting with "-" (like -module.resource) are indistinguishable from flags.

func (*IacArgs) HasPlanFile

func (a *IacArgs) HasPlanFile() bool

HasPlanFile checks if a plan file is already specified in args. Checks for -out= flag (plan command) or any argument present (apply/destroy).

func (*IacArgs) InsertArgument

func (a *IacArgs) InsertArgument(pos int, arg string) *IacArgs

InsertArgument inserts an argument at position and returns self for chaining.

func (*IacArgs) InsertArguments

func (a *IacArgs) InsertArguments(pos int, args ...string) *IacArgs

InsertArguments inserts arguments at position and returns self for chaining.

func (*IacArgs) InsertFlag

func (a *IacArgs) InsertFlag(pos int, flags ...string) *IacArgs

InsertFlag inserts flag(s) at position and returns self for chaining.

func (*IacArgs) IsDestroyCommand

func (a *IacArgs) IsDestroyCommand(cmd string) bool

IsDestroyCommand returns true if this represents a destroy operation. It checks both the command name and the -destroy flag.

func (*IacArgs) Last

func (a *IacArgs) Last() string

Last returns the last element (last argument, or last flag, or command).

func (*IacArgs) MergeFlags

func (a *IacArgs) MergeFlags(other *IacArgs) *IacArgs

MergeFlags merges flags from another IacArgs, adding only flags not already present. Handles both -flag=value and space-separated -flag value formats. Returns self for chaining.

func (*IacArgs) Normalize

func (a *IacArgs) Normalize(acts ...NormalizeActsType) *IacArgs

Normalize formats the flags according to the given actions.

func (*IacArgs) RemoveFlag

func (a *IacArgs) RemoveFlag(name string) *IacArgs

RemoveFlag removes a flag by name (handles -flag, --flag, -flag=value, and space-separated -flag value).

func (*IacArgs) Second

func (a *IacArgs) Second() string

Second returns the second element (first subcommand, first flag, or first argument).

func (*IacArgs) SetCommand

func (a *IacArgs) SetCommand(cmd string) *IacArgs

SetCommand sets the command and returns self for chaining.

func (*IacArgs) Slice

func (a *IacArgs) Slice() []string

Slice returns args in correct order: [command] [flags...] [arguments...]

func (*IacArgs) Tail

func (a *IacArgs) Tail() []string

Tail returns everything except the command (subcommand, flags, and arguments) as a slice.

type NormalizeActsType

type NormalizeActsType byte
const (
	SingleDashFlag NormalizeActsType = iota
	DoubleDashFlag
)

Jump to

Keyboard shortcuts

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