utils

package
v0.0.0-...-d3e8332 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Curl is global option, if true, then all HTTP request will be NOT be sent and only print as curl command
	Curl bool
	// Verbose is global option, if true, cli will print debug info in addition to the normal output
	Verbose bool
	// MoreVerbose is global option, if true, cli will print even more debug info
	MoreVerbose bool
	// PrintLog is a toggle that indicate that verbose/more-verbose logging is enabled.
	PrintLog bool
)

Functions

func ClearDotCACAODirectory

func ClearDotCACAODirectory()

ClearDotCACAODirectory removes any files in ~/.cacao used by cli

func ExactNArgsOrFileFlag

func ExactNArgsOrFileFlag(n int) cobra.PositionalArgs

ExactNArgsOrFileFlag builds on top of cobra.ExactArgs. It checks if --file flag is set. If it is NOT set, then requires a minimum number of arguments. If --file is set and other arguments are still provided then error.

func GetInput

func GetInput(prompt string, options ...UserInputOptionFunc) (string, error)

GetInput asks user for a string input. Additional options can be also specified.

func GetSecretInput

func GetSecretInput(prompt string, options ...UserInputOptionFunc) (string, error)

GetSecretInput asks user for a string input. Additional options can be also specified. The difference between this function and GetInput is that this function does not print user's input on the screen.

func IsHTTPOrHTTPSUrl

func IsHTTPOrHTTPSUrl(input string) error

IsHTTPOrHTTPSUrl check if input string is HTTP or HTTPS URL

func MaximumNArgsOrFileFlag

func MaximumNArgsOrFileFlag(n int) cobra.PositionalArgs

MaximumNArgsOrFileFlag builds on top of cobra.MaximumNArgs. It checks if --file flag is set. If it is NOT set, then enforce a maximum number of arguments. If --file is set and other arguments are still provided then error.

func MinimumNArgsOrFileFlag

func MinimumNArgsOrFileFlag(n int) cobra.PositionalArgs

MinimumNArgsOrFileFlag builds on top of cobra.MinimumNArgs. It checks if --file flag is set. If it is NOT set, then requires a minimum number of arguments. If --file is set and other arguments are still provided then error.

func NewFormatter

func NewFormatter() *prettyjson.Formatter

NewFormatter returns a new formatter with following default values.

func ParseKeyValuePairs

func ParseKeyValuePairs(parameters string) ([]http.KeyValue, error)

ParseKeyValuePairs parse a parameter string into key-value pairs. The format of the params is key=value,key=value

func SaveAuthTokenToConfigFile

func SaveAuthTokenToConfigFile(authToken string) error

SaveAuthTokenToConfigFile saves auth token to ~/.cacao/config.json

func WriteAPIURLToConfigFile

func WriteAPIURLToConfigFile(cacaoAPI string) error

WriteAPIURLToConfigFile writes the CACAO api URL to a config file in the .cacao directory

Types

type APIFile

type APIFile struct {
	APIBaseURL string `json:"api"`
}

APIFile is schema of ~/.cacao/api.json

type Client

type Client struct {
	Output
	// contains filtered or unexported fields
}

Client ...

func NewClient

func NewClient() (c Client)

NewClient ...

func NewClientWithAPIAddr

func NewClientWithAPIAddr(cacaoAPI string) (c Client)

NewClientWithAPIAddr ...

func (*Client) CheckToken

func (c *Client) CheckToken() error

CheckToken checks if there is token in the config file, if token is present, then check if it is valid by making an API call.

func (*Client) DoRequest

func (c *Client) DoRequest(req *http.Request) error

DoRequest will either execute a Request or print the curl command

func (*Client) GetHTPPClient

func (c *Client) GetHTPPClient() *http.Client

GetHTPPClient returns an HTTP Client to be used by the CLI

func (*Client) NewRequest

func (c *Client) NewRequest(verb, path string, data string) *http.Request

NewRequest creates a new HTTP request with the correct header

type ConfigFile

type ConfigFile struct {
	Token string `json:"id_token"`
}

ConfigFile is schema of ~/.cacao/config.json

type Output

type Output struct {
	UI               cli.Ui
	OutputFormat     string
	OutputCurlString bool
	PrintLog         bool
}

Output contain the meta-option that nearly all subcommand inherited.

func NewOutput

func NewOutput() Output

NewOutput ...

func (*Output) PrintCurlString

func (m *Output) PrintCurlString(req *http.Request)

PrintCurlString will output a string that is the equivalent curl command for the specified request. This will be helpful for debugging. A majority of the code in this function comes from Hashicorp Vault's equivalent function: https://github.com/hashicorp/vault/blob/528604359c20a32fef628d86abdb7663e81235c3/api/output_string.go#L35-L64

func (*Output) PrintDebug

func (m *Output) PrintDebug(msg string, extras ...interface{})

PrintDebug is used to print some output only when the debug flag is used

func (*Output) PrintError

func (m *Output) PrintError(msg string)

PrintError ...

func (*Output) PrintHTTPResponse

func (m *Output) PrintHTTPResponse(response *http.Response)

PrintHTTPResponse is used to print the body and error (if it exists) from an HTTP Response

type UserInputOptionFunc

type UserInputOptionFunc func(option *userInputOption)

UserInputOptionFunc is the type for options for GetInput()

func DefaultInput

func DefaultInput(defaultValue string) UserInputOptionFunc

DefaultInput sets a default value for GetInput(), default value is used if user enters empty string (just hit enter).

func Retry

func Retry(maxRetryCount uint) UserInputOptionFunc

Retry adds retry to GetInput()

func ValidateInput

func ValidateInput(validate func(string) error) UserInputOptionFunc

ValidateInput adds validation to GetInput()

type UsernameFile

type UsernameFile struct {
	Username string `json:"username"`
}

UsernameFile is schema of ~/.cacao/username.json

Jump to

Keyboard shortcuts

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