util

package module
v0.0.0-...-9673488 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const ErrMsgClosedStdinAfterDeadline = "closed stdin after waiting"
View Source
const ErrMsgDockerServerUrlBadInputMultipleLines = "cannot parse url from multiple lines of input"
View Source
const ErrMsgDockerServerUrlBadInputZeroLines = "cannot parse url from zero lines of input"
View Source
const ErrMsgUnknownCommand = "unable to read inputs in the correct format without knowledge of the current command"
View Source
const ServiceName = "com.tlowerison.credential-1password"
View Source
const VaultKey = "vault"

Variables

View Source
var PredefinedModes = []string{
	string(DockerMode),
	string(GitMode),
}

Functions

func HandleErr

func HandleErr(err error)

HandleErr does if nothing if the provided error is nil, otherwise it prints the provided err's message to stderr and exits.

func WithSessionRetry

func WithSessionRetry(ctx *Context, cmd *cobra.Command, args []string, fn func(ctx *Context) error)

WithSessionRetry runs fn and if it receives an error which the op utils recognizes as an indication that a session token is missing or out of date, then it will request a new signin to generate a new session token and then run fn again with the new session token.

Types

type Context

type Context struct {
	Flags *Flags

	OpFunc op.OpFunc
	// contains filtered or unexported fields
}

func NewContext

func NewContext(opFunc op.OpFunc, ks keystore.Keystore, stdin io.ReadCloser) *Context

func (*Context) GetCmd

func (ctx *Context) GetCmd() *cobra.Command

GetCmd returns the private cmd field.

func (*Context) GetInput

func (ctx *Context) GetInput() string

GetInput returns the private field input. input is the cached value of what is read from stdin.

func (*Context) GetInputs

func (ctx *Context) GetInputs() map[string]string

GetInputs is only required for testing, it duplicates all inputs in case of any bad usage in order to ensure that ctx.inputs is not mutated outside of this package.

func (*Context) GetKey

func (ctx *Context) GetKey() (string, error)

GetKey returns the input key provided over stdin. This key will be used as the stored file's title. If it has already been computed, returns the cached value, otherwise, computes it based on whether the current mode is predefined or not. If predefined, the current mode is assumed to have its own method for processing stdin into a key. If not predefined, the current mode is returned as the key itself.

func (*Context) GetMode

func (ctx *Context) GetMode() Mode

GetMode returns the mode set by the persistent flag --mode.

func (*Context) GetName

func (ctx *Context) GetName() string

GetName returns "$mode-credential-1password" if using a predefined mode, otherwise returns "credential-1password".

func (*Context) GetOpQuery

func (ctx *Context) GetOpQuery() (*op.Query, error)

GetOpQuery wraps an op.Context and the input key provided over stdin into an op.Query. This is a useful helper function as op.Query is embedded into most op structs.

func (*Context) GetSessionToken

func (ctx *Context) GetSessionToken() (string, error)

GetSessionToken retrieves the session token in context if present. If not, checks if the most recent session token date if it's still valid, and if it is, tries to return whatever is stored in the encrypted keystore. If there's nothing in the keystore or the token is out of date, it will request the user to sigin, store the newly created session token in the encrypted keystore as well as context, and return the session token.

func (*Context) GetStdinDeadline

func (ctx *Context) GetStdinDeadline() time.Duration

GetStdinDeadline returns the stdin timeout.

func (*Context) GetVaultName

func (ctx *Context) GetVaultName() (string, error)

GetVaultName reads the configured vault name or returns the cached value if already read.

func (*Context) ParseInput

func (ctx *Context) ParseInput() (err error)

ParseInput scans from stdin and splits each line by "=" to find key/value pairs. Any line which does not contain "=" is skipped over. Tries to store the inputs in the provided map, but if it's nil, will create a new map and fill that; returns the filled inputs map.

func (*Context) SetCmd

func (ctx *Context) SetCmd(cmd *cobra.Command)

SetCmd sets the private cmd field. cmd should be assigned by a prerun cobra command hook.

func (*Context) SetStdinDeadline

func (ctx *Context) SetStdinDeadline(stdinDeadline time.Duration)

SetStdinDeadline sets the private stdinDeadline field.

func (*Context) SetVaultName

func (ctx *Context) SetVaultName(vaultName string, shouldCreate bool) error

SetVaultName does: 1. checks whether the provided vault exists 2a. if so, sets the vault name and the vaultUUID in the encrypted keystore 2b. if not, and shouldCreate is false, fails 2c. if not, and shouldCreate is true, creates a new vault with the provided name, loop back to step 2a

func (*Context) Signin

func (ctx *Context) Signin() (string, error)

Signin clears the current cached session token, requests the user to signin, stores the new returned session token and returns it as well.

type Flags

type Flags struct {
	Mode                string
	Config_Vault_Create bool
}

type Mode

type Mode string
const (
	DockerMode Mode = "docker"
	GitMode    Mode = "git"
)

func (Mode) IsPredefined

func (m Mode) IsPredefined() bool

func (Mode) Valid

func (m Mode) Valid() bool

type Runnable

type Runnable func(cmd *cobra.Command, args []string)

func PreRunWithInput

func PreRunWithInput(ctx *Context) Runnable

PreRunWithInput wraps ctx.ParseInput with a session retry.

func Run

func Run(ctx *Context, fn func(ctx *Context) error) Runnable

Run wraps fn with a session retry.

func RunWithArgs

func RunWithArgs(ctx *Context, fn func(ctx *Context, args []string) error) Runnable

RunWithArgs fn with a session retry and passes args to fn.

Jump to

Keyboard shortcuts

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