github.com/cidverse/cid-sdk-go@v0.0.0-20240318001225-c193d83f053e/command.go (about)

     1  package cidsdk
     2  
     3  // ExecuteCommandRequest defines model for ExecuteCommandRequest.
     4  type ExecuteCommandRequest struct {
     5  	// CaptureOutput capture and return the output (stdout and stderr will be passed through if not set)
     6  	CaptureOutput bool `json:"capture_output,omitempty"`
     7  
     8  	// Command command
     9  	Command string `json:"command,omitempty"`
    10  
    11  	// WorkDir directory to execute the command in (default = project root)
    12  	WorkDir string `json:"work_dir,omitempty"`
    13  
    14  	// Env contains additional env properties
    15  	Env map[string]string `json:"env,omitempty"`
    16  
    17  	// Ports that will be exposed
    18  	Ports []int `json:"ports,omitempty"`
    19  
    20  	// A version Constraint for the binary used in command
    21  	Constraint []int `json:"constraint,omitempty"`
    22  }
    23  
    24  // ExecuteCommandResponse defines model for ExecuteCommandResponse.
    25  type ExecuteCommandResponse struct {
    26  	// Code command exit code
    27  	Code int `json:"code,omitempty"`
    28  
    29  	// Command the command being executed
    30  	Command string `json:"command,omitempty"`
    31  
    32  	// Dir directory the command is executed in
    33  	Dir string `json:"dir,omitempty"`
    34  
    35  	// Error error message
    36  	Error string `json:"error,omitempty"`
    37  
    38  	// Stderr error output (if capture-output was request, empty otherwise)
    39  	Stderr string `json:"stderr,omitempty"`
    40  
    41  	// Stdout standard output (if capture-output was request, empty otherwise)
    42  	Stdout string `json:"stdout,omitempty"`
    43  }
    44  
    45  // ExecuteCommand command
    46  func (sdk SDK) ExecuteCommand(req ExecuteCommandRequest) (*ExecuteCommandResponse, error) {
    47  	resp, err := sdk.client.R().
    48  		SetHeader("Accept", "application/json").
    49  		SetBody(req).
    50  		SetResult(&ExecuteCommandResponse{}).
    51  		SetError(&APIError{}).
    52  		Post("/command")
    53  
    54  	if err != nil {
    55  		return nil, err
    56  	} else if resp.IsSuccess() {
    57  		return resp.Result().(*ExecuteCommandResponse), nil
    58  	} else {
    59  		return nil, resp.Error().(*APIError)
    60  	}
    61  }