cliconfig

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cliconfig stores and resolves the CLI's own settings — the API endpoint and credentials the user keeps in $HOME/.scanoss/settings.json.

This is NOT a project's scanoss.json (see pkg/settings), which holds that project's BOM and skip rules. The two are unrelated despite the similar file name.

The package is deliberately under internal/: reading ambient state (environment variables, a file in $HOME) is correct for a CLI and wrong for a library, so the SDK in pkg/ cannot import it even by accident.

The package is split by concern: registry.go declares which settings exist and how they are spelled, store.go reads and writes the file, and resolve.go decides which source wins for a given command.

Index

Constants

View Source
const (
	KeyAPIURL = "api_url"
	KeyAPIKey = "api_key"
	KeyProxy  = "proxy"
	KeyCACert = "ca_cert"
)

Recognized setting keys. Keys are snake_case in the file; a new one is an entry in registry below plus, if it should affect behaviour, a rung in the resolver.

Variables

This section is empty.

Functions

func CLIKey

func CLIKey(key string) string

CLIKey returns how key is spelled on the command line: api_key → api-key. Every recognized key the CLI prints goes through here, so its output can be pasted back into a command. It is also the flag name, deliberately the same string.

func EnvName

func EnvName(key string) string

EnvName returns the environment variable that overrides key, e.g. api_url → SCANOSS_API_URL. It mirrors what viper's SetEnvPrefix("SCANOSS") + AutomaticEnv derive, so the resolver and viper cannot disagree about a name.

func IsRecognized

func IsRecognized(key string) bool

IsRecognized reports whether key — in its stored form — is one this version knows about. Keys the CLI does not recognize may still exist in the file: they are preserved, not rejected, but nothing reads them.

This takes the stored spelling because it guards the Go API (Set, Unset). Command arguments arrive in the command-line spelling and go through StoredKey first.

func IsSecret

func IsSecret(key string) bool

IsSecret reports whether key's value must never be displayed or logged.

func Path

func Path() (string, error)

Path returns the absolute path of the settings file, whether or not it exists.

func Set

func Set(key, value string) error

Set stores value under key — the stored (snake_case) form; command arguments go through StoredKey first. It creates ~/.scanoss and the file if missing, and leaves keys already in the file that this version does not recognize untouched.

func StoredKey

func StoredKey(input string) (string, bool)

StoredKey maps a command-line key to the form stored in the file: api-key → api_key. There is exactly one accepted spelling per setting — the dashed one, as listed by cliKeys — so api_key, API-KEY and apikey are all reported as unrecognized. One vocabulary means one thing to document and one error to explain.

Surrounding whitespace is trimmed: that is not a spelling, and leaving it in produces a baffling `unrecognized key " api-key"`.

func Unset

func Unset(key string) error

Unset removes key from the file. Removing a key that is not there succeeds: the requested end state is what the caller asked for.

Types

type API

type API struct {
	URL string
	Key string
}

API holds the resolved endpoint settings every API command needs.

func ResolveAPI

func ResolveAPI(flags *pflag.FlagSet) (API, error)

ResolveAPI returns the API settings a command should use, applying flag > environment > config file > built-in default. This is what the commands that talk to the API call.

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config is the settings as stored on disk, including keys this version does not recognize.

func Load

func Load() (*Config, error)

Load reads the settings file. A missing file is not an error — it yields an empty Config, and nothing is created. A malformed file IS an error: falling back to defaults would silently scan against the wrong endpoint.

func (*Config) Get

func (c *Config) Get(key string) (string, bool)

Get returns the stored value of key, rendered as a string. An absent key and an empty value are both reported as unset, so `"api_key": ""` in the file behaves like no api_key at all.

func (*Config) Keys

func (c *Config) Keys() []string

Keys returns every key present in the file, sorted — including unrecognized ones, so `config list` can show what a hand-edited file actually holds.

type Setting

type Setting struct {
	Key    string
	Value  string
	Source Source
}

Setting is one recognized setting as a command sees it: the key in its stored form, the effective value, and which source won.

func Resolve

func Resolve(flags *pflag.FlagSet, key string) (Setting, error)

Resolve returns the effective value of one recognized key and its source. The key is the stored form; map a command argument with StoredKey first.

func ResolveAll

func ResolveAll(flags *pflag.FlagSet) ([]Setting, error)

ResolveAll returns every recognized setting, sorted by key — what `config list` reports. The file is read once for the whole set.

type Source

type Source string

Source names where a resolved value came from. `config list` reports it, and it is the only observable signal about a secret, whose value is never displayed.

const (
	SourceFlag    Source = "flag"
	SourceEnv     Source = "env"
	SourceFile    Source = "config file"
	SourceDefault Source = "default"
	SourceUnset   Source = "unset"
)

type Transport

type Transport struct {
	Proxy      string
	CACertFile string
}

Transport holds the resolved settings for how to reach the API: through which proxy, and trusting which extra certificate authority. Both are empty when nothing is configured, which means "use the environment, and the system certificate pool".

func ResolveTransport

func ResolveTransport(flags *pflag.FlagSet) (Transport, error)

ResolveTransport returns the transport settings a command should use, applying the same ladder as ResolveAPI. Separate from ResolveAPI because the two answer different questions — where to send the request, and how to get there — but resolved the same way, and each call reads the file once.

type UnknownKeyError

type UnknownKeyError struct {
	Key string
}

UnknownKeyError reports a key the CLI does not recognize.

func (*UnknownKeyError) Error

func (e *UnknownKeyError) Error() string

Jump to

Keyboard shortcuts

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