cli

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const SampleConfig = `` /* 1224-byte string literal not displayed */

Variables

This section is empty.

Functions

func AutoRefreshToken

func AutoRefreshToken(cfg *ConfigFile) (string, error)

AutoRefreshToken attempts to refresh the OIDC token if it is near expiry.

func AutoRefreshTokenToPath

func AutoRefreshTokenToPath(cfg *ConfigFile, configPath string) (string, error)

AutoRefreshTokenToPath attempts to refresh the OIDC token if it is near expiry and saves refreshed tokens to configPath.

func BuildCommands

func BuildCommands(spec *Spec) []*cli.Command

BuildCommands converts parsed OpenAPI operations into a cli.Command tree grouped by tag.

func ConfigDir

func ConfigDir() string

ConfigDir returns the directory containing the active config file.

func ConfigPath

func ConfigPath() string

ConfigPath returns the active config file path.

func ExchangeAPIKey

func ExchangeAPIKey(cfg *ConfigFile, configPath string) (string, error)

ExchangeAPIKey exchanges an NGC API key for a bearer token, updates the config, and saves it. Returns the new token. NGC Personal/Service API keys (those prefixed with "nvapi-") are bearer tokens themselves and are returned as-is without contacting the authn endpoint.

func ExecuteTokenCommand

func ExecuteTokenCommand(tokenCommand string) (string, error)

ExecuteTokenCommand runs a shell command that prints a bearer token.

func FormatOutput

func FormatOutput(data []byte, format string) error

func GetAuthToken

func GetAuthToken(cfg *ConfigFile) string

GetAuthToken returns the best available bearer token from the config. Priority: auth.token > auth.oidc.token > auth.api_key.token

func HasAPIKeyConfig

func HasAPIKeyConfig(cfg *ConfigFile) bool

HasAPIKeyConfig returns true when NGC API key settings are present.

func HasOIDCConfig

func HasOIDCConfig(cfg *ConfigFile) bool

HasOIDCConfig returns true when OIDC credentials are present in the config.

func HasTokenCommandConfig

func HasTokenCommandConfig(cfg *ConfigFile) bool

HasTokenCommandConfig returns true when an auth token command is configured.

func InitCommand

func InitCommand() *cli.Command

InitCommand returns the 'init' CLI command that generates a sample config.

func LoginCommand

func LoginCommand() *cli.Command

LoginCommand returns the 'login' CLI command.

func LoginFromConfig

func LoginFromConfig(cfg *ConfigFile, configPath string) (string, error)

LoginFromConfig performs a login using the configured auth method and returns the current bearer token.

func LoginWithOIDCConfig

func LoginWithOIDCConfig(cfg *ConfigFile, configPath string) (string, error)

LoginWithOIDCConfig performs a non-interactive OIDC login from config.

func LoginWithTokenCommand

func LoginWithTokenCommand(cfg *ConfigFile, configPath, tokenCommand string) (string, error)

LoginWithTokenCommand runs a shell command that prints a bearer token, stores it in config, and returns it.

func NewApp

func NewApp(specData []byte) (*cli.App, error)

NewApp builds a cli.App from the embedded OpenAPI spec data.

func ReadBodyInput

func ReadBodyInput(data, dataFile string) ([]byte, error)

ReadBodyInput reads request body from --data flag or --data-file flag. Use "--data-file -" to read from stdin.

func ResolveToken

func ResolveToken(token, tokenCommand string) (string, error)

ResolveToken returns the token or executes the token command.

func SaveConfig

func SaveConfig(cfg *ConfigFile) error

SaveConfig writes the config back to ConfigPath(), preserving unknown keys.

func SaveConfigToPath

func SaveConfigToPath(cfg *ConfigFile, path string) error

SaveConfigToPath writes the config to a specific path, preserving any unknown keys the user may have manually added.

func SetConfigPath

func SetConfigPath(path string)

SetConfigPath overrides the default config file path for the process lifetime.

func ValidateOutputFormat

func ValidateOutputFormat(format string) error

ValidateOutputFormat returns an error if format is outside the allowed set. The empty string is treated as valid so the StringFlag default ("json") and callers that pass an unset value continue to work.

Without this validator, FormatOutput silently routed any unknown value to formatJSON, so a typo like `--output xml` exited 0 and produced JSON -- dangerous in scripts that branch on the requested format.

Types

type APIError

type APIError struct {
	StatusCode int
	Status     string
	Body       string
	Message    string
	Data       interface{}
}

func (*APIError) Error

func (e *APIError) Error() string

type AuthRetryAction

type AuthRetryAction string
const (
	AuthRetryActionLogin AuthRetryAction = "login"
	AuthRetryActionRetry AuthRetryAction = "retry"
	AuthRetryActionSkip  AuthRetryAction = "skip"
)

type AuthRetryEvent

type AuthRetryEvent struct {
	Action      AuthRetryAction
	Attempt     int
	MaxAttempts int
	StatusCode  int
	Status      string
	Method      string
}

type Client

type Client struct {
	BaseURL         string
	Org             string
	Token           string
	APIName         string
	HTTPClient      *http.Client
	Debug           bool
	Log             *logrus.Entry
	TokenRefresh    func() (string, error)
	AuthRetryMax    int
	AuthRetryNotify func(AuthRetryEvent)
}

func NewClient

func NewClient(baseURL, org, token string, log *logrus.Entry, debug bool) *Client

func (*Client) Do

func (c *Client) Do(method, pathTemplate string, pathParams, queryParams map[string]string, body []byte) ([]byte, http.Header, error)

Do executes an HTTP request against the API.

type Components

type Components struct {
	Schemas   map[string]*Schema     `yaml:"schemas"`
	Responses map[string]interface{} `yaml:"responses"`
}

type ConfigAPI

type ConfigAPI struct {
	Base string `yaml:"base,omitempty"`
	Org  string `yaml:"org,omitempty"`
	Name string `yaml:"name,omitempty"`
}

type ConfigAPIKey

type ConfigAPIKey struct {
	AuthnURL string `yaml:"authn_url,omitempty"`
	Key      string `yaml:"key,omitempty"`
	Token    string `yaml:"token,omitempty"`
}

type ConfigAuth

type ConfigAuth struct {
	Token        string        `yaml:"token,omitempty"`
	TokenCommand string        `yaml:"token_command,omitempty"`
	OIDC         *ConfigOIDC   `yaml:"oidc,omitempty"`
	APIKey       *ConfigAPIKey `yaml:"api_key,omitempty"`
}

type ConfigFile

type ConfigFile struct {
	API  ConfigAPI  `yaml:"api"`
	Auth ConfigAuth `yaml:"auth"`
}

ConfigFile mirrors the ~/.nico/config.yaml structure.

func LoadConfig

func LoadConfig() (*ConfigFile, error)

LoadConfig reads config from the active path (override or default).

func LoadConfigFromPath

func LoadConfigFromPath(path string) (*ConfigFile, error)

LoadConfigFromPath reads a config file at a specific path.

type ConfigOIDC

type ConfigOIDC struct {
	TokenURL     string `yaml:"token_url,omitempty"`
	ClientID     string `yaml:"client_id,omitempty"`
	ClientSecret string `yaml:"client_secret,omitempty"`
	Username     string `yaml:"username,omitempty"`
	Password     string `yaml:"password,omitempty"`
	Token        string `yaml:"token,omitempty"`
	RefreshToken string `yaml:"refresh_token,omitempty"`
	ExpiresAt    string `yaml:"expires_at,omitempty"`
}

type MediaType

type MediaType struct {
	Schema *Schema `yaml:"schema"`
}

type Operation

type Operation struct {
	OperationID string       `yaml:"operationId"`
	Summary     string       `yaml:"summary"`
	Description string       `yaml:"description"`
	Tags        []string     `yaml:"tags"`
	Parameters  []Parameter  `yaml:"parameters"`
	RequestBody *RequestBody `yaml:"requestBody"`
}

type Parameter

type Parameter struct {
	Name        string  `yaml:"name"`
	In          string  `yaml:"in"`
	Required    bool    `yaml:"required"`
	Description string  `yaml:"description"`
	Schema      *Schema `yaml:"schema"`
}

type PathItem

type PathItem struct {
	Parameters []Parameter `yaml:"parameters"`
	Get        *Operation  `yaml:"get"`
	Post       *Operation  `yaml:"post"`
	Patch      *Operation  `yaml:"patch"`
	Put        *Operation  `yaml:"put"`
	Delete     *Operation  `yaml:"delete"`
}

type RequestBody

type RequestBody struct {
	Content map[string]MediaType `yaml:"content"`
}

type Schema

type Schema struct {
	Ref        string             `yaml:"$ref"`
	Type       SchemaType         `yaml:"type"`
	Format     string             `yaml:"format"`
	Enum       []string           `yaml:"enum"`
	Properties map[string]*Schema `yaml:"properties"`
	Required   []string           `yaml:"required"`
	Items      *Schema            `yaml:"items"`
	MinLength  *int               `yaml:"minLength"`
	MaxLength  *int               `yaml:"maxLength"`
	Minimum    *int               `yaml:"minimum"`
	Maximum    *int               `yaml:"maximum"`
	Default    interface{}        `yaml:"default"`
}

type SchemaType

type SchemaType string

SchemaType handles OpenAPI 3.1 type fields that can be a string or a list of strings.

func (*SchemaType) UnmarshalYAML

func (t *SchemaType) UnmarshalYAML(value *yaml.Node) error

type Server

type Server struct {
	URL         string `yaml:"url"`
	Description string `yaml:"description"`
}

type Spec

type Spec struct {
	Info       SpecInfo            `yaml:"info"`
	Servers    []Server            `yaml:"servers"`
	Tags       []Tag               `yaml:"tags"`
	Paths      map[string]PathItem `yaml:"paths"`
	Components Components          `yaml:"components"`
}

func ParseSpec

func ParseSpec(data []byte) (*Spec, error)

func (*Spec) RequestBodySchema

func (s *Spec) RequestBodySchema(op *Operation) *Schema

func (*Spec) ResolveRef

func (s *Spec) ResolveRef(ref string) *Schema

func (*Spec) ResolveSchema

func (s *Spec) ResolveSchema(schema *Schema) *Schema

type SpecInfo

type SpecInfo struct {
	Title   string `yaml:"title"`
	Version string `yaml:"version"`
}

type Tag

type Tag struct {
	Name        string `yaml:"name"`
	Description string `yaml:"description"`
}

type TokenResponse

type TokenResponse struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	ExpiresIn    int    `json:"expires_in"`
	TokenType    string `json:"token_type"`
}

TokenResponse is the OAuth2 token endpoint response.

Jump to

Keyboard shortcuts

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