bwcli

package
v0.1.2-0...-0ef654a Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

file: bwcli/client.go

file: bwcli/runner.go

file: bwcli/types.go

Index

Constants

View Source
const (
	ArbitraryErrorMaxLength = 200
	DefaultBwBinary         = "bw"
)

Variables

View Source
var (
	ErrUnauthorized          = errors.New("bwcli: unauthorized")
	ErrLocked                = errors.New("bwcli: vault locked")
	ErrNotFound              = errors.New("bwcli: not found")
	ErrCLI                   = errors.New("bwcli: generic error")
	ErrDecode                = errors.New("bwcli: decode error")
	ErrTimeout               = errors.New("bwcli: timeout")
	ErrServerVersionMismatch = errors.New(
		"bwcli: server does not support this CLI version " +
			"(userDecryptionOptions missing); upgrade the server or downgrade the bw CLI")
)

Functions

This section is empty.

Types

type Client

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

func New

func New(cfg *Config) *Client

func (*Client) ConfigureAgentTempDir

func (c *Client) ConfigureAgentTempDir(serverURL, userID string) error

func (*Client) CreateItem

func (c *Client) CreateItem(ctx context.Context, session string, item *Item) (Item, error)

func (*Client) EditItem

func (c *Client) EditItem(ctx context.Context, session, itemID string, item *Item) (Item, error)

func (*Client) EnsureServer

func (c *Client) EnsureServer(ctx context.Context, serverURL string, env map[string]string) error

EnsureServer ensures bw is configured to use serverURL. If the current server differs, it logs out (required by bw) and updates it.

func (*Client) GetItem

func (c *Client) GetItem(ctx context.Context, session, itemID string) (Item, error)

func (*Client) ListCollections

func (c *Client) ListCollections(ctx context.Context, session, organizationID string) ([]Collection, error)

func (*Client) ListItems

func (c *Client) ListItems(ctx context.Context, session string) ([]Item, error)

func (*Client) ListOrganizations

func (c *Client) ListOrganizations(ctx context.Context, session string) ([]Organization, error)

func (*Client) LoginAPIKey

func (c *Client) LoginAPIKey(ctx context.Context, serverURL, clientID, clientSecret string) error

LoginAPIKey logs in using Bitwarden API key values. This method does NOT store session; it just performs login.

func (*Client) LogoutWithEnv

func (c *Client) LogoutWithEnv(ctx context.Context, env map[string]string) error

func (*Client) Status

func (c *Client) Status(ctx context.Context) (Status, error)

func (*Client) Sync

func (c *Client) Sync(ctx context.Context, session string) error

func (*Client) Unlock

func (c *Client) Unlock(ctx context.Context, masterPassword string) (string, error)

Unlock unlocks the vault and returns the session token (bw unlock --raw). The session MUST be passed per command via BW_SESSION env by the caller.

func (*Client) UpsertItemByName

func (c *Client) UpsertItemByName(ctx context.Context, session string, item *Item) (Item, bool, error)

UpsertItemByName lists items once and then chooses create vs edit. Returns (item, created, error).

func (*Client) UpsertSecureNote

func (c *Client) UpsertSecureNote(
	ctx context.Context,
	session string,
	name string,
	notes string,
	orgID string,
	collectionIDs []string,
	hiddenFields map[string]string,
) (Item, bool, error)

type Collection

type Collection struct {
	ID             string          `json:"id,omitempty"`
	Name           string          `json:"name,omitempty"`
	OrganizationID string          `json:"organizationId,omitempty"`
	Raw            json.RawMessage `json:"-"`
}

type Config

type Config struct {
	Runner   Runner
	ExtraEnv map[string]string
	BWPath   string
	WorkDir  string
	HostURL  string
	Logger   zerolog.Logger
}

type ExecRunner

type ExecRunner struct {
	BaseEnv map[string]string
	WorkDir string
}

ExecRunner is the production runner using os/exec.

func (*ExecRunner) Run

func (r *ExecRunner) Run(ctx context.Context, cmd string, args []string, env map[string]string, stdin []byte) RunResult

type Field

type Field struct {
	Name  string    `json:"name"`
	Value string    `json:"value,omitempty"`
	Type  FieldType `json:"type"`
}

type FieldType

type FieldType int
const (
	FieldTypeText    FieldType = 0
	FieldTypeHidden  FieldType = 1
	FieldTypeBoolean FieldType = 2
	FieldTypeLinked  FieldType = 3
)

type Item

type Item struct {
	SecureNote      *SecureNote     `json:"secureNote,omitempty"`
	Notes           string          `json:"notes,omitempty"`
	FolderID        string          `json:"folderId,omitempty"`
	ID              string          `json:"id,omitempty"`
	Name            string          `json:"name,omitempty"`
	OrganizationID  string          `json:"organizationId,omitempty"`
	CollectionIDs   []string        `json:"collectionIds,omitempty"`
	Identity        json.RawMessage `json:"identity,omitempty"`
	Raw             json.RawMessage `json:"-"`
	PasswordHistory json.RawMessage `json:"passwordHistory,omitempty"`
	Fields          []Field         `json:"fields,omitempty"`
	Login           json.RawMessage `json:"login,omitempty"`
	Card            json.RawMessage `json:"card,omitempty"`
	Type            ItemType        `json:"type,omitempty"`
	Reprompt        int             `json:"reprompt,omitempty"`
	Favorite        bool            `json:"favorite,omitempty"`
}

Item is a minimal representation of Bitwarden items. Many fields are optional and vary by "type". Keep unknown parts in Raw.

func (*Item) GetField

func (i *Item) GetField(name string) (Field, bool)

func (*Item) SetField

func (i *Item) SetField(name, value string, t FieldType)

type ItemType

type ItemType int
const (
	SecureNoteItemType ItemType = 2
)

type Organization

type Organization struct {
	ID   string          `json:"id,omitempty"`
	Name string          `json:"name,omitempty"`
	Raw  json.RawMessage `json:"-"`
}

Organization / Collection kept minimal for backup grouping later.

type RunResult

type RunResult struct {
	Err      error
	Stdout   []byte
	StdErr   []byte
	ExitCode int
}

type Runner

type Runner interface {
	Run(ctx context.Context, cmd string, args []string, env map[string]string, stdin []byte) RunResult
}

Runner abstracts execution so unit tests can inject a fake.

type SecureNote

type SecureNote struct {
	Type SecureNoteType `json:"type"`
}

type SecureNoteType

type SecureNoteType int
const (
	SecureNoteTypeGeneric SecureNoteType = 0
)

type Status

type Status struct {
	ServerURL string          `json:"serverUrl,omitempty"`
	LastSync  string          `json:"lastSync,omitempty"`
	UserEmail string          `json:"userEmail,omitempty"`
	UserID    string          `json:"userId,omitempty"`
	Status    string          `json:"status,omitempty"` // "unauthenticated", "locked", "unlocked"
	Raw       json.RawMessage `json:"-"`
}

Status mirrors the important parts of `bw status` output. Keep it resilient to schema changes.

Jump to

Keyboard shortcuts

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