{"modulePath":"github.com/GoogleCloudPlatform/workloadagentplatform/sharedlibraries","version":"v0.0.0-20261019192026-b1dc9a900ccf","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package commandlineexecutor\n\nPackage commandlineexecutor creates an interface to streamline execution of shell commands across multiple platforms.\n\nThis package provides a simple interface to execute shell commands across multiple platforms. It also provides a way to check if a command exists and to get the exit code from an error.\n\nExample usage:\n\n\t// Create a new commandlineexecutor.Params struct with the executable and arguments to run.\n\tparams := commandlineexecutor.Params{\n\t\tExecutable: \"/bin/ls\",\n\t\tArgs:       []string{\"-l\", \"/usr/local/google\"},\n\t}\n\n\t// Execute the command.\n\tresult := commandlineexecutor.ExecuteCommand(context.Background(), params)\n\n\t// Check the result for any errors.\n\tif result.Error != nil {\n\t\tlog.Error(result.Error)\n\t}\n\n\t// Print the standard output and standard error.\n\tfmt.Printf(\"Standard output:\\n%s\", result.StdOut)\n\tfmt.Printf(\"Standard error:\\n%s\", result.StdErr)\n\n\t// Check the exit code.\n\tif result.ExitCode != 0 {\n\t\tlog.Error(\"Command failed with exit code\", result.ExitCode)\n\t}\n\n## Functions\n\n```go\nfunc CommandExists(executable string) bool\n```\nCommandExists returns whether or not an executable command exists within the current os runtime environment.\n\n## Types\n\n```go\ntype Execute func(context.Context, Params) Result\n```\nExecute is a function to execute a command. Production callers to pass commandlineexecutor.ExecuteCommand while calling this package's APIs.\n\n```go\ntype Exists func(string) bool\n```\nExists is a function to check if a command exists.  Production callers to pass commandlineexecutor.CommandExists while calling this package's APIs.\n\n```go\ntype ExitCode func(err error) int\n```\nExitCode is a function to get the exit code from an error.  Production callers to pass commandlineexecutor.CommandExitCode while calling this package's APIs.\n\n```go\ntype Params struct {\n\tExecutable string\n\t// One of ArgsToSplit or Args should be defined on the Params.\n\t// ArgsToSplit should be preferred when issuing commands with a subshell and using \"-c\".\n\t// An example would be an invocation like:\n\t//   Executable: \"/bin/sh\"\n\t//   ArgsToSplit: \"-c 'ls /usr/sap/*/SYS/global/hdb/custom/config/global.ini'\"\n\t// In this case ArgsToSplit will be split up correctly as:\n\t//   []string{\"-c\", \"'ls /usr/sap/*/SYS/global/hdb/custom/config/global.ini'\"}\n\tArgsToSplit string\n\tArgs        []string\n\tTimeout     int // defaults to 60, so timeout will occur in 60 seconds\n\tUser        string\n\tEnv         []string\n\tStdin       string\n}\n```\nParams encapsulates the parameters used by the Exec\\* and RunWithEnv funcs.\n\n```go\ntype Result struct {\n\tStdOut, StdErr   string\n\tExitCode         int\n\tError            error\n\tExecutableFound  bool\n\tExitStatusParsed bool // Will be true if \"exit status ([0-9]+)\" is in the error result\n}\n```\nResult holds the stdout, stderr, exit code, and error from the execution.\n\n```go\nfunc ExecuteCommand(ctx context.Context, params Params) Result\n```\nExecuteCommand takes Params and returns a Result.\n\nIf the params.Executable does not exist it will return early with the Result.Error filled If the Params ArgsToSplit is not empty then it will be split into an arguments array Else the Args will be used as the arguments array If the User is not empty then the command will be executed as that user If Env is defined then that environment will be used to execute the command\n\nThe returned Result will contain the standard out, standard error, the exit code and an error if one was encountered during execution.\n\n```go\ntype Run func() error\n```\nRun is a testable version of the exec.Run method.  Should only be used during testing.\n\n```go\ntype SetupExeForPlatform func(exe *exec.Cmd, params Params) error\n```\nSetupExeForPlatform is a testable version of the setupExeForPlatform call. Should only be used during testing.\n\n","path":"github.com/GoogleCloudPlatform/workloadagentplatform/sharedlibraries/commandlineexecutor","name":"commandlineexecutor","synopsis":"Package commandlineexecutor creates an interface to streamline execution of shell commands across multiple platforms.","isRedistributable":true}
