kouch

package module
v0.0.0-...-1bf7cb7 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

README

Build Status Codecov Go Report Card GoDoc

Kouch

Kouch is a command-line interface for CouchDB, intended to facilitate ease of scripting or manual interaction with CouchDB.

It takes great inspiration from curl, the command-line tool for transferring data with URLs.

Kouch aims to make CouchDB administration and scripting easier, by providing a simple, CouchDB-centric command-line tool for performing routine administration and debugging operations, without the cumbersome task of manually constructing HTTP requests for use with curl.

Kouch can also output (and read input) to pretty-printed JSON or YAML, rather than CouchDB's native JSON format, for more more human-friendly interaction with documents.

Example Usage

Fetch a document

$ kouch get doc localhost:5984/foo/bar
{"_attachments":{"foo.txt":{"content_type":"text/plain","digest":"md5-WiGw80mG3uQuqTKfUnIZsg==","length":9,"revpos":3,"stub":true}},"_id":"bar","_rev":"3-13438fbeeac7271383a42b57511f03ea","a":"c"}

Fetch a document, pretty JSON output

$ kouch get doc localhost:5984/foo/bar -F json --json-indent " "
{
 "_attachments": {
  "foo.txt": {
   "content_type": "text/plain",
   "digest": "md5-WiGw80mG3uQuqTKfUnIZsg==",
   "length": 9,
   "revpos": 3,
   "stub": true
  }
 },
 "_id": "bar",
 "_rev": "3-13438fbeeac7271383a42b57511f03ea",
 "a": "c"
}

Fetch a document, YAML output

$ kouch get doc localhost:5984/foo/bar --output-format yaml
_attachments:
  foo.txt:
    content_type: text/plain
    digest: md5-WiGw80mG3uQuqTKfUnIZsg==
    length: 9
    revpos: 3
    stub: true
_id: bar
_rev: 3-13438fbeeac7271383a42b57511f03ea
a: c

Fetch a document, showing only the headers

$ kouch get doc localhost:5984/foo/bar -I
Cache-Control: must-revalidate
Content-Length: 198
Content-Type: application/json
Date: Sun, 26 Aug 2018 16:32:12 GMT
Etag: "3-13438fbeeac7271383a42b57511f03ea"
Server: CouchDB/2.1.1 (Erlang OTP/17)
X-Couch-Request-Id: 0ff82e5498
X-Couchdb-Body-Time: 0

Current status

Kouch is still in the early stages of development. Most features have not yet been implemented. But fast progress is being made, and your contributions are also welcome!

License

This software is released under the terms of the Apache 2.0 license. See LICENCE.md, or read the full license.

Documentation

Index

Constants

View Source
const (
	// Curl-equivalent flags
	FlagVerbose    = "verbose"
	FlagOutputFile = "output"
	FlagData       = "data"
	FlagHead       = "head"
	FlagDumpHeader = "dump-header"
	FlagUser       = "user"
	FlagCreateDirs = "create-dirs"

	// Custom flags
	FlagClobber                 = "force"
	FlagConfigFile              = "kouchconfig"
	FlagServerRoot              = "root"
	FlagDataJSON                = "data-json"
	FlagDataYAML                = "data-yaml"
	FlagOutputFormat            = "output-format"
	FlagFilename                = "filename"
	FlagDocument                = "id"
	FlagDatabase                = "database"
	FlagFullCommit              = "full-commit"
	FlagIfNoneMatch             = "if-none-match"
	FlagRev                     = "rev"
	FlagAutoRev                 = "auto-rev"
	FlagShards                  = "shards"
	FlagPassword                = "password"
	FlagContext                 = "context"
	FlagConflicts               = "conflicts"
	FlagDescending              = "descending"
	FlagEndKey                  = "endkey"
	FlagEndKeyDocID             = "endkey-docid"
	FlagGroup                   = "group"
	FlagGroupLevel              = "group-level"
	FlagIncludeDocs             = "include-docs"
	FlagIncludeAttachments      = "attachments"
	FlagIncludeAttEncoding      = "att-encoding-info"
	FlagInclusiveEnd            = "inclusive-end"
	FlagKey                     = "key"
	FlagKeys                    = "keys"
	FlagLimit                   = "limit"
	FlagReduce                  = "reduce"
	FlagSkip                    = "skip"
	FlagSorted                  = "sorted"
	FlagStable                  = "stable"
	FlagStale                   = "stale"
	FlagStartKey                = "startkey"
	FlagStartKeyDocID           = "startkey-docid"
	FlagUpdate                  = "update"
	FlagUpdateSeq               = "update-seq"
	FlagTemplate                = "template"
	FlagTemplateFile            = "template-file"
	FlagJSONPrefix              = "json-prefix"
	FlagJSONIndent              = "json-indent"
	FlagJSONEscapeHTML          = "json-escape-html"
	FlagAttsSince               = "atts-since"
	FlagIncludeDeletedConflicts = "deleted-conflicts"
	FlagForceLatest             = "latest"
	FlagIncludeLocalSeq         = "local-seq"
	FlagMeta                    = "meta"
	FlagOpenRevs                = "open-revs"
	FlagRevs                    = "revs"
	FlagRevsInfo                = "revs-info"
	FlagBatch                   = "batch"
	FlagNewEdits                = "new-edits"

	// Curl-equivalent short flags
	FlagShortVerbose    = "v"
	FlagShortOutputFile = "o"
	FlagShortData       = "d"
	FlagShortHead       = "I"
	FlagShortDumpHeader = "D"
	FlagShortUser       = "u"

	// Short versions, custom
	FlagShortServerRoot   = "S"
	FlagShortOutputFormat = "F"
	FlagShortRev          = "r"
	FlagShortAutoRev      = "R"
	FlagShortShards       = "q"
	FlagShortPassword     = "p"
)

Common command line flags

View Source
const Version = "0.0.1-prerelease"

Version is the version of this release of Kouch.

Variables

This section is empty.

Functions

func Exit

func Exit(err error)

Exit outputs err.Error() to stderr, then exits with the exit status embedded in the error.

func ExitStatus

func ExitStatus(err error) int

ExitStatus returns the exit status embedded in the error.

func Flags

func Flags(ctx context.Context) *pflag.FlagSet

Flags returns the flags of the context.

func GetContext

func GetContext(cmd *cobra.Command) context.Context

GetContext returns the context associated with cmd.

func GetTarget

func GetTarget(ctx context.Context) string

GetTarget returns the target argument from the context.

func HeadDumper

func HeadDumper(ctx context.Context) io.WriteCloser

HeadDumper returns an io.Writer to which headers should be written, or nil if none.

func Input

func Input(ctx context.Context) io.ReadCloser

Input returns the context's current input, or panics if none is set.

func Output

func Output(ctx context.Context) io.Writer

Output returns the context's current output, or panics if none is set.

func SetConf

func SetConf(ctx context.Context, conf *Config) context.Context

SetConf returns a new context with the current config set to conf.

func SetContext

func SetContext(ctx context.Context, cmd *cobra.Command)

SetContext sets the context associated with the command.

func SetFlags

func SetFlags(ctx context.Context, flags *pflag.FlagSet) context.Context

SetFlags returns a new context with Flags.

func SetHeadDumper

func SetHeadDumper(ctx context.Context, d io.WriteCloser) context.Context

SetHeadDumper returns a new context with the head-dumper set to d.

func SetInput

func SetInput(ctx context.Context, w io.ReadCloser) context.Context

SetInput returns a new context with the input set to w.

func SetOutput

func SetOutput(ctx context.Context, w io.Writer) context.Context

SetOutput returns a new context with the output set to w.

func SetTarget

func SetTarget(ctx context.Context, target string) context.Context

SetTarget returns a new context with the target flag set to target.

func SetVerbose

func SetVerbose(ctx context.Context, value bool) context.Context

SetVerbose returns a new context with the Verbose flag set to value.

func TargetHelpText

func TargetHelpText(scope TargetScope) string

TargetHelpText returns the help text to describe the valid target format(s) for the specified scope, or "" if the scope isn't defined.

func TargetScopeName

func TargetScopeName(scope TargetScope) string

TargetScopeName returns the name of the scope, or "" if scope is invalid.

func Verbose

func Verbose(ctx context.Context) bool

Verbose returns the verbosity flag of the context.

Types

type Config

type Config struct {
	// DefaultContext is the name of the context to be used by default.
	DefaultContext string `yaml:"default-context" json:"default-context,omitempty"`
	// Contexts is a map of referencable names to context configs
	Contexts []NamedContext `json:"contexts,omitempty"`

	// File is the file where config was read from, or more precisely, where
	// changes will be saved to.
	File string `json:"-" yaml:"-"`
}

Config represents the kouch tool configuration.

func Conf

func Conf(ctx context.Context) *Config

Conf returns the context's current configuration struct, or panics if none is set.

func (*Config) DefaultCtx

func (c *Config) DefaultCtx() (*Context, error)

DefaultCtx returns the default context.

func (*Config) Dump

func (c *Config) Dump() (r io.ReadCloser)

Dump dumps the config as a JSON string on r. Any errors will be returned as an error on r.Read().

type Context

type Context struct {
	// Root is the URL to the server's root.
	Root     string `json:"root"`
	User     string `json:"user,omitempty"`
	Password string `json:"password,omitempty"`
}

Context is a server context (URL, auth info, session store, etc)

type InitError

type InitError string

InitError returns an error for init failures.

func (InitError) Error

func (i InitError) Error() string

func (InitError) ExitStatus

func (i InitError) ExitStatus() int

ExitStatus returns ExitFailedToInitialize

type NamedContext

type NamedContext struct {
	// Name is the nickname for this Context
	Name string `json:"name"`
	// Context holds the context information
	Context *Context `json:"context"`
}

NamedContext relates nicknames to context information.

type Options

type Options struct {
	*Target
	*chttp.Options
}

Options represents the accumulated options passed by the user, through config files, the commandline, etc.

func NewOptions

func NewOptions() *Options

NewOptions returns a new, empty Options struct.

func (*Options) Query

func (o *Options) Query() *url.Values

Query returns the url query parameters, initializing it if necessary.

func (*Options) SetParam

func (o *Options) SetParam(f *pflag.FlagSet, flag string) error

SetParam sets the named parameter

func (*Options) SetParams

func (o *Options) SetParams(f *pflag.FlagSet, flags ...string) error

SetParams sets parameters based on the provided flags

type Target

type Target struct {
	// Root is the root URL.
	Root string
	// Database is the database name.
	Database string
	// DocID is the document ID.
	Document string
	// Filename is the attachment filename.
	Filename string
	// User is the Auth username
	User string
	// Password is the Auth password
	Password string
}

Target is a parsed target passed on the command line.

func NewTarget

func NewTarget(ctx context.Context, scope TargetScope, flags *pflag.FlagSet) (*Target, error)

NewTarget builds a new target from the context and flags.

func ParseTarget

func ParseTarget(scope TargetScope, src string) (*Target, error)

ParseTarget parses src as a CouchDB target, according to the rules for scope.

func (*Target) DatabaseFromFlags

func (t *Target) DatabaseFromFlags(flags *pflag.FlagSet) error

DatabaseFromFlags sets t.Database from the passed flagset.

func (*Target) DocumentFromFlags

func (t *Target) DocumentFromFlags(flags *pflag.FlagSet) error

DocumentFromFlags sets t.DocID from the passed flagset.

func (*Target) FilenameFromFlags

func (t *Target) FilenameFromFlags(flags *pflag.FlagSet) error

FilenameFromFlags sets t.Filename from the passed flagset.

func (*Target) NewClient

func (t *Target) NewClient() (*chttp.Client, error)

NewClient returns a chttp.Client, connected to the target server

type TargetScope

type TargetScope int

TargetScope represents the scope for a target, as relative targets have different meanings in different contexts.

const (
	TargetRoot TargetScope = iota
	TargetDatabase
	TargetDocument
	TargetAttachment
)

The supported target scopes

Directories

Path Synopsis
cmd
internal
test
Package test provides test utilities
Package test provides test utilities
util
Package util contains random junk, for internal use only.
Package util contains random junk, for internal use only.
io
Package kouchio provides basic I/O primitives meant to suppliment the standard library's io package.
Package kouchio provides basic I/O primitives meant to suppliment the standard library's io package.

Jump to

Keyboard shortcuts

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