shellcmd

package
v0.0.0-...-b872246 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdFormat

type CmdFormat int

CmdFormat defines a way of formatting shell commands in a devbox config.

const (
	// CmdArray formats shell commands as an array of strings.
	CmdArray CmdFormat = iota

	// CmdString formats shell commands as a single string.
	CmdString
)

Formats for marshalling and unmarshalling a series of shell commands in a devbox config.

func (CmdFormat) String

func (c CmdFormat) String() string

type Commands

type Commands struct {
	// MarshalAs determines how MarshalJSON encodes the shell commands.
	// UnmarshalJSON will set MarshalAs automatically so that commands
	// marshal back to their original format. The default zero-value
	// formats them as an array.
	MarshalAs CmdFormat
	Cmds      []string
}

Commands marshals and unmarshals shell commands from a devbox config as either a single string or an array of strings. It preserves the original value such that:

data == marshal(unmarshal(data)))

func (*Commands) AppendScript

func (s *Commands) AppendScript(script string)

AppendScript appends each line of a script to s.Cmds. It also applies the following formatting rules:

  • Trim leading newlines from the script.
  • Trim trailing whitespace from the script.
  • If the first line of the script begins with one or more tabs ('\t'), then unindent each line by that same number of tabs.
  • Remove trailing whitespace from each line.

Note that unindenting only happens when a line starts with at least as many tabs as the first line. If it starts with fewer tabs, then it is not unindented at all.

Example
shCmds := Commands{}
shCmds.AppendScript(`
		# This script will be unindented by the number of leading tabs
		# on the first line.
		if true; then
			echo "this is always printed"
		fi`,
)
b, _ := cuecfg.MarshalJSON(&shCmds)
fmt.Println(string(b))
Output:

[
  "# This script will be unindented by the number of leading tabs",
  "# on the first line.",
  "if true; then",
  "\techo \"this is always printed\"",
  "fi"
]

func (Commands) MarshalJSON

func (s Commands) MarshalJSON() ([]byte, error)

MarshalJSON marshals shell commands according to s.MarshalAs. It marshals commands to a string by joining s.Cmds with newlines.

func (*Commands) String

func (s *Commands) String() string

String formats the commands as a single string by joining them with newlines.

func (*Commands) UnmarshalJSON

func (s *Commands) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals shell commands from a string, an array of strings, or null. When the JSON value is a string, it unmarshals into the first index of s.Cmds.

Jump to

Keyboard shortcuts

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