cmdx

package
v0.0.36 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package cmdx provides utilities for generating and managing command-line commands in a structured and efficient manner. This package is designed to simplify the creation and handling of commands, ensuring that arguments with spaces are properly managed and that the commands are constructed correctly.

The primary function in this package is GenerateCommand, which takes a main command and a variadic slice of arguments, and returns a pointer to a DaggerCMD slice. This slice includes the main command followed by the provided arguments. The function also performs validation to ensure that the main command is not empty.

Example usage:

// Generate a Terraform plan command
cmd, err := GenerateCommand("terraform", "plan", "-var", "foo=bar", "apply --auto-approve")
if err != nil {
    // handle error
}
// Use cmd, e.g., fmt.Println(*cmd) // Output: [terraform plan -var foo=bar apply --auto-approve]

// Generate a Go run command
cmd, err = GenerateCommand("go", "run", "main.go", "--verbose")
if err != nil {
    // handle error
}
// Use cmd, e.g., fmt.Println(*cmd) // Output: [go run main.go --verbose]

This package is intended for developers who need to programmatically generate and manage command-line commands in their Go applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildArgs

func BuildArgs(args ...string) types.DaggerCMD

BuildArgs processes and builds a command argument list from the provided arguments. It trims spaces from each argument and splits arguments with spaces into separate arguments.

Parameters:

  • args: A variadic parameter that takes multiple strings representing command arguments.

Returns:

  • A DaggerCMD slice containing the processed arguments.

Example:

// Build arguments for a Terraform command
args := BuildArgs("plan", "-var 'foo=bar'", "apply --auto-approve")
// args now contains: ["plan", "-var", "'foo=bar'", "apply", "--auto-approve"]

// Build arguments for a Go command
args = BuildArgs("run", "main.go", "--verbose")
// args now contains: ["run", "main.go", "--verbose"]

func BuildCurlCommand

func BuildCurlCommand(
	baseURL string,
	headers map[string]string,
	timeout time.Duration,
	authType string,
	authCredentials string,
) string

BuildCurlCommand generates a curl command string based on the provided parameters.

Parameters:

  • baseURL: The target URL for the curl command.
  • headers: A map of HTTP headers to be included in the request.
  • timeout: The maximum time allowed for the curl command to complete.
  • authType: The type of authentication to use ("basic" or "bearer").
  • authCredentials: The credentials for authentication (username:password for basic, token for bearer).

Returns:

  • A string containing the complete curl command.

func ConvertCMDToString

func ConvertCMDToString(cmd *types.DaggerCMD) string

ConvertCMDToString converts a DaggerCMD slice to a string. It preserves the consistency of the original command, handling arguments with spaces appropriately.

Parameters:

  • cmd: A pointer to a DaggerCMD slice containing the command and its arguments.

Returns:

  • A string representation of the command, suitable for direct execution.

Example:

cmd := types.DaggerCMD{"go", "run", "main.go", "--verbose"}
cmdString := ConvertCMDToString(&cmd)
fmt.Println(cmdString) // Output: go run main.go --verbose

cmd = types.DaggerCMD{"terraform", "plan", "-var", "foo=bar", "apply --auto-approve"}
cmdString = ConvertCMDToString(&cmd)
fmt.Println(cmdString) // Output: terraform plan -var foo=bar apply --auto-approve

func GenerateCommand

func GenerateCommand(command string, args ...string) (*types.DaggerCMD, error)

GenerateCommand generates a command with the provided arguments. It ensures that arguments with spaces are handled correctly.

Parameters:

  • command: A string representing the main command to be executed.
  • args: A variadic slice of strings representing the arguments for the command.

Returns:

  • A pointer to a DaggerCMD slice, which includes the main command followed by the provided arguments.
  • An error if the main command is empty.

Example:

// Generate a Terraform plan command
cmd, err := GenerateCommand("terraform", "plan", "-var", "foo=bar", "apply --auto-approve")
if err != nil {
    // handle error
}
// Use cmd, e.g., fmt.Println(*cmd) // Output: [terraform plan -var foo=bar apply --auto-approve]

// Generate a Go run command
cmd, err = GenerateCommand("go", "run", "main.go", "--verbose")
if err != nil {
    // handle error
}
// Use cmd, e.g., fmt.Println(*cmd) // Output: [go run main.go --verbose]

func GenerateDaggerCMDFromStr

func GenerateDaggerCMDFromStr(commands string) ([]string, error)

GenerateDaggerCMDFromStr generates a DaggerCMD from a plain command string. It splits the command string into a slice of strings, ensuring it handles special characters and consecutive spaces correctly.

Parameters:

  • commands: A string representing the command.

Returns:

  • A slice of strings representing the command and its arguments.
  • An error if the command string is empty or invalid.

func GenerateSHCommandAsDaggerCMD

func GenerateSHCommandAsDaggerCMD(command string, args ...string) (*types.DaggerCMD, error)

GenerateSHCommandAsDaggerCMD generates a command wrapped for execution using `sh -c` and returns a DaggerCMD.

Parameters:

  • command: A string representing the main command to be executed.
  • args: A variadic slice of strings representing the arguments for the command.

Returns:

  • A pointer to a DaggerCMD slice containing the complete command wrapped for `sh -c` execution.
  • An error if the main command is empty.

Example:

cmd, err := GenerateSHCommandAsDaggerCMD("echo", "Hello, World!")
if err != nil {
    // handle error
}
fmt.Println(cmd) // Output: [sh -c "echo Hello, World!"]

func GenerateShCommand

func GenerateShCommand(command string, args ...string) (string, error)

GenerateShCommand generates a command wrapped for execution using `sh -c`. It ensures that arguments with spaces are correctly handled.

Parameters:

  • command: A string representing the main command to be executed.
  • args: A variadic slice of strings representing the arguments for the command.

Returns:

  • A string containing the complete command wrapped for `sh -c` execution.
  • An error if the main command is empty.

Example:

cmd, err := GenerateShCommand("echo", "Hello, World!")
if err != nil {
    // handle error
}
fmt.Println(cmd) // Output: sh -c "echo Hello, World!"

func PtToSlice

func PtToSlice(cmd *types.DaggerCMD) ([]string, error)

PtToSlice converts a *types.DaggerCMD to a []string.

Parameters:

  • cmd: A pointer to a DaggerCMD slice containing the command and its arguments.

Returns:

  • A slice of strings representing the command and its arguments.
  • An error if the input DaggerCMD is nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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