spritestate

package
v0.5.26 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spritestate stores what the sprites commands remember between runs: the default organization and the Sprites API token minted for each organization. It is a separate package so that `fly auth logout` can clear it without importing the sprites commands.

Index

Constants

This section is empty.

Variables

View Source
var ErrCorrupt = errors.New("the saved Sprites state could not be read")

ErrCorrupt reports that the state file could not be parsed. Load moves such a file aside, so that saving doesn't overwrite tokens that may still be valid, and returns an empty state along with the error. Callers can warn and carry on.

View Source
var RevokeTimeout = 30 * time.Second

RevokeTimeout caps how long a login or logout spends revoking Sprites tokens, so an unreachable Sprites API delays it by at most this much. It is a variable so tests can shorten it.

Functions

func Clear

func Clear(configDir string) error

Clear removes the state file. A missing file is not an error.

func KeepDefaultOrgFor

func KeepDefaultOrgFor(ctx context.Context, configDir, userID string) error

KeepDefaultOrgFor drops the saved default organization unless userID, the user now logged in, is the one who chose it. An empty userID, for a user that couldn't be found, always drops it.

func LockMint

func LockMint(ctx context.Context, configDir string) (unlock func(), err error)

LockMint serializes cached token reads and minting with login and logout. A login change holds it through both clearing state and saving the login, so an in-flight mint cannot repopulate state after it has been cleared. It is separate from Update's lock, allowing the holder to save state.

func LockMintForLogin

func LockMintForLogin(ctx context.Context, configDir string) (unlock func(), err error)

LockMintForLogin is LockMint for login and logout, which wait less: the caller warns and goes ahead without the lock rather than fail the login change over Sprites state.

func MintToken

func MintToken(ctx context.Context, apiURL, org, flyMacaroon string) (string, error)

MintToken exchanges a Fly.io macaroon for a Sprites API token in org.

func Path

func Path(configDir string) string

Path returns the state file's location inside the flyctl config directory.

func RevokeAll

func RevokeAll(ctx context.Context, configDir, flyMacaroon string, keepDefaultOrg bool) (failed []string, err error)

RevokeAll revokes every saved token and then removes the state file. It returns the tokens' organizations that could not be revoked; the tokens are forgotten regardless, since the login that could revoke them is going away. With keepDefaultOrg, a default organization whose user is known is kept, for KeepDefaultOrgFor to check once the next login's user is known.

func RevokeForLoginChange

func RevokeForLoginChange(ctx context.Context, configDir, configFile string, keepDefaultOrg bool) (warnings []string)

RevokeForLoginChange revokes and forgets the saved tokens before the Fly.io login saved in configFile is replaced or removed, since that login minted them and is the only one that can revoke them. It never fails the login change: what went wrong comes back as warnings for the caller to print. keepDefaultOrg is for a login, which may be the same user again; see RevokeAll.

func RevokeToken

func RevokeToken(ctx context.Context, apiURL, org, flyMacaroon, token string) error

RevokeToken deletes a Sprites API token minted in org. A token that is already gone counts as revoked.

func Save

func Save(configDir string, st *State) error

Save writes the state file with owner-only permissions, since it holds tokens. It replaces the file in one step, so a reader never sees it half written.

func TokenKey

func TokenKey(apiURL, org string) string

TokenKey identifies a token by the API it is for and the organization it belongs to.

func Update

func Update(ctx context.Context, configDir string, fn func(*State)) error

Update applies fn to the current state and saves the result, holding a lock so that concurrent sprites commands do not drop each other's changes.

func Used

func Used(configDir string) bool

Used reports whether the sprites commands have ever run with this config directory: whether the state file or the mint lock, which a sprites command takes before it saves anything, exists. Login and logout skip all Sprites work when it is false, so they are unchanged for people who never use Sprites. Anything but a definite "not there" counts as used, so that the cleanup is attempted rather than skipped.

Types

type State

type State struct {
	// DefaultOrg is the organization used when none is specified.
	DefaultOrg string `json:"default_org,omitempty"`

	// DefaultOrgUser is the ID of the Fly.io user who chose DefaultOrg. A
	// login as that same user keeps the default; any other login drops it.
	DefaultOrgUser string `json:"default_org_user,omitempty"`

	// Tokens holds Sprites API tokens keyed by TokenKey.
	Tokens map[string]string `json:"tokens,omitempty"`
}

State is the content of the sprites state file.

func Load

func Load(configDir string) (*State, error)

Load reads the state file. A missing file is an empty state.

Jump to

Keyboard shortcuts

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