cockroach: github.com/cockroachdb/cockroach/pkg/cli Index | Examples | Files | Directories

package cli

import "github.com/cockroachdb/cockroach/pkg/cli"

Code:

c := newCLITest(cliTestParams{})
defer c.cleanup()

c.RunWithCAArgs([]string{"cert", "create-client", "foo"})
c.RunWithCAArgs([]string{"cert", "create-client", "Ομηρος"})
c.RunWithCAArgs([]string{"cert", "create-client", "0foo"})

Output:

cert create-client foo
cert create-client Ομηρος
cert create-client 0foo
failed to generate client certificate and key: username "0foo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters

Code:

c := newCLITest(cliTestParams{noServer: true})
defer c.cleanup()

testData := [][]string{
    {`demo`, `-e`, `show database`},
    {`demo`, `-e`, `show database`, `--empty`},
    {`demo`, `-e`, `show application_name`},
    {`demo`, `--format=table`, `-e`, `show database`},
    {`demo`, `-e`, `select 1 as "1"`, `-e`, `select 3 as "3"`},
    {`demo`, `--echo-sql`, `-e`, `select 1 as "1"`},
    {`demo`, `--set=errexit=0`, `-e`, `select nonexistent`, `-e`, `select 123 as "123"`},
    {`demo`, `startrek`, `-e`, `show databases`},
    {`demo`, `startrek`, `-e`, `show databases`, `--format=table`},
}
for _, cmd := range testData {
    c.RunWithArgs(cmd)
}

Output:

demo -e show database
database
movr
demo -e show database --empty
database
defaultdb
demo -e show application_name
application_name
$ cockroach demo
demo --format=table -e show database
  database
+----------+
  movr
(1 row)
demo -e select 1 as "1" -e select 3 as "3"
1
1
3
3
demo --echo-sql -e select 1 as "1"
> select 1 as "1"
1
1
demo --set=errexit=0 -e select nonexistent -e select 123 as "123"
pq: column "nonexistent" does not exist
123
123
demo startrek -e show databases
database_name
defaultdb
postgres
startrek
system
demo startrek -e show databases --format=table
  database_name
+---------------+
  defaultdb
  postgres
  startrek
  system
(4 rows)

Code:

c := newCLITest(cliTestParams{})
defer c.cleanup()

c.RunWithArgs([]string{`sql`, `--logtostderr=false`, `-e`, `select 1 as "1"`})
c.RunWithArgs([]string{`sql`, `--logtostderr=true`, `-e`, `select 1 as "1"`})
c.RunWithArgs([]string{`sql`, `--vmodule=foo=1`, `-e`, `select 1 as "1"`})

Output:

sql --logtostderr=false -e select 1 as "1"
1
1
sql --logtostderr=true -e select 1 as "1"
1
1
sql --vmodule=foo=1 -e select 1 as "1"
1
1

Code:

c := newCLITest(cliTestParams{})
defer c.cleanup()

// Refresh time series data, which is required to retrieve stats.
if err := c.WriteSummaries(); err != nil {
    log.Fatalf(context.Background(), "Couldn't write stats summaries: %s", err)
}

c.Run("node ls")
c.Run("node ls --format=table")
c.Run("node status 10000")

Output:

node ls
id
1
node ls --format=table
  id
+----+
   1
(1 row)
node status 10000
Error: node 10000 doesn't exist

Code:

c := newCLITest(cliTestParams{})
defer c.cleanup()

c.RunWithArgs([]string{`sql`, `-e`, `show application_name`})
c.RunWithArgs([]string{`sql`, `-e`, `create database t; create table t.f (x int, y int); insert into t.f values (42, 69)`})
c.RunWithArgs([]string{`sql`, `-e`, `select 3 as "3"`, `-e`, `select * from t.f`})
c.RunWithArgs([]string{`sql`, `-e`, `begin`, `-e`, `select 3 as "3"`, `-e`, `commit`})
c.RunWithArgs([]string{`sql`, `-e`, `select * from t.f`})
c.RunWithArgs([]string{`sql`, `--execute=show databases`})
c.RunWithArgs([]string{`sql`, `-e`, `select 1 as "1"; select 2 as "2"`})
c.RunWithArgs([]string{`sql`, `-e`, `select 1 as "1"; select 2 as "@" where false`})
// CREATE TABLE AS returns a SELECT tag with a row count, check this.
c.RunWithArgs([]string{`sql`, `-e`, `create table t.g1 (x int)`})
c.RunWithArgs([]string{`sql`, `-e`, `create table t.g2 as select * from generate_series(1,10)`})
// It must be possible to access pre-defined/virtual tables even if the current database
// does not exist yet.
c.RunWithArgs([]string{`sql`, `-d`, `nonexistent`, `-e`, `select count(*) from "".information_schema.tables limit 0`})
// It must be possible to create the current database after the
// connection was established.
c.RunWithArgs([]string{`sql`, `-d`, `nonexistent`, `-e`, `create database nonexistent; create table foo(x int); select * from foo`})
// COPY should return an intelligible error message.
c.RunWithArgs([]string{`sql`, `-e`, `copy t.f from stdin`})
// --echo-sql should print out the SQL statements.
c.RunWithArgs([]string{`user`, `ls`, `--echo-sql`})
// --set changes client-side variables before executing commands.
c.RunWithArgs([]string{`sql`, `--set=errexit=0`, `-e`, `select nonexistent`, `-e`, `select 123 as "123"`})
c.RunWithArgs([]string{`sql`, `--set`, `echo=true`, `-e`, `select 123 as "123"`})
c.RunWithArgs([]string{`sql`, `--set`, `unknownoption`, `-e`, `select 123 as "123"`})

Output:

sql -e show application_name
application_name
$ cockroach sql
sql -e create database t; create table t.f (x int, y int); insert into t.f values (42, 69)
INSERT 1
sql -e select 3 as "3" -e select * from t.f
3
3
x	y
42	69
sql -e begin -e select 3 as "3" -e commit
BEGIN
3
3
COMMIT
sql -e select * from t.f
x	y
42	69
sql --execute=show databases
database_name
defaultdb
postgres
system
t
sql -e select 1 as "1"; select 2 as "2"
1
1
2
2
sql -e select 1 as "1"; select 2 as "@" where false
1
1
@
sql -e create table t.g1 (x int)
CREATE TABLE
sql -e create table t.g2 as select * from generate_series(1,10)
CREATE TABLE AS
sql -d nonexistent -e select count(*) from "".information_schema.tables limit 0
count
sql -d nonexistent -e create database nonexistent; create table foo(x int); select * from foo
x
sql -e copy t.f from stdin
woops! COPY has confused this client! Suggestion: use 'psql' for COPY
user ls --echo-sql
warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
> SHOW USERS
user_name
root
sql --set=errexit=0 -e select nonexistent -e select 123 as "123"
pq: column "nonexistent" does not exist
123
123
sql --set echo=true -e select 123 as "123"
> select 123 as "123"
123
123
sql --set unknownoption -e select 123 as "123"
invalid syntax: \set unknownoption. Try \? for help.
invalid syntax

Code:

c := newCLITest(cliTestParams{noServer: true})
defer c.cleanup()

c.RunWithArgs([]string{"sqlfmt", "-e", ";"})
c.RunWithArgs([]string{"sqlfmt", "-e", "delete from t"})
c.RunWithArgs([]string{"sqlfmt", "-e", "delete from t", "-e", "update t set a = 1"})
c.RunWithArgs([]string{"sqlfmt", "--print-width=10", "-e", "select 1,2,3 from a,b,c;;;select 4"})
c.RunWithArgs([]string{"sqlfmt", "--print-width=10", "--align", "-e", "select 1,2,3 from a,b,c;;;select 4"})
c.RunWithArgs([]string{"sqlfmt", "--print-width=10", "--tab-width=2", "--use-spaces", "-e", "select 1,2,3 from a,b,c;;;select 4"})
c.RunWithArgs([]string{"sqlfmt", "-e", "select (1+2)+3"})
c.RunWithArgs([]string{"sqlfmt", "--no-simplify", "-e", "select (1+2)+3"})

Output:

sqlfmt -e ;
sqlfmt -e delete from t
DELETE FROM t
sqlfmt -e delete from t -e update t set a = 1
DELETE FROM t;
UPDATE t SET a = 1;
sqlfmt --print-width=10 -e select 1,2,3 from a,b,c;;;select 4
SELECT
	1,
	2,
	3
FROM
	a,
	b,
	c;
SELECT 4;
sqlfmt --print-width=10 --align -e select 1,2,3 from a,b,c;;;select 4
SELECT 1,
       2,
       3
  FROM a,
       b,
       c;
SELECT 4;
sqlfmt --print-width=10 --tab-width=2 --use-spaces -e select 1,2,3 from a,b,c;;;select 4
SELECT
  1, 2, 3
FROM
  a, b, c;
SELECT 4;
sqlfmt -e select (1+2)+3
SELECT 1 + 2 + 3
sqlfmt --no-simplify -e select (1+2)+3
SELECT (1 + 2) + 3

Code:

c := newCLITest(cliTestParams{})
defer c.cleanup()

c.Run("user ls")
c.Run("user ls --format=table")
c.Run("user ls --format=tsv")
c.Run("user set FOO")
c.RunWithArgs([]string{"sql", "-e", "create user if not exists 'FOO'"})
c.Run("user set Foo")
c.Run("user set fOo")
c.Run("user set foO")
c.Run("user set foo")
c.Run("user set _foo")
c.Run("user set f_oo")
c.Run("user set foo_")
c.Run("user set ,foo")
c.Run("user set f,oo")
c.Run("user set foo,")
c.Run("user set 0foo")
c.Run("user set foo0")
c.Run("user set f0oo")
c.Run("user set foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof")
c.Run("user set foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo")
c.Run("user set Ομηρος")
// Try some reserved keywords.
c.Run("user set and")
c.Run("user set table")
// Don't use get, since the output of hashedPassword is random.
// c.Run("user get foo")
c.Run("user ls --format=table")
c.Run("user rm foo")
c.Run("user ls --format=table")

Output:

user ls
warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
user_name
root
user ls --format=table
warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
  user_name
+-----------+
  root
(1 row)
user ls --format=tsv
warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
user_name
root
user set FOO
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
sql -e create user if not exists 'FOO'
CREATE USER 0
user set Foo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 0
user set fOo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 0
user set foO
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 0
user set foo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 0
user set _foo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set f_oo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set foo_
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set ,foo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
pq: username ",foo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
user set f,oo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
pq: username "f,oo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
user set foo,
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
pq: username "foo," invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
user set 0foo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
pq: username "0foo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
user set foo0
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set f0oo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
pq: username "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
user set foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set Ομηρος
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set and
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user set table
warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
CREATE USER 1
user ls --format=table
warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
                             user_name
+-----------------------------------------------------------------+
  _foo
  and
  f0oo
  f_oo
  foo
  foo0
  foo_
  foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
  root
  table
  ομηρος
(11 rows)
user rm foo
warning: This command is deprecated. Use DROP USER or DROP ROLE in a SQL session.
DROP USER 1
user ls --format=table
warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
                             user_name
+-----------------------------------------------------------------+
  _foo
  and
  f0oo
  f_oo
  foo0
  foo_
  foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
  root
  table
  ομηρος
(10 rows)

Index

Examples

Package Files

backtrace.go cert.go cli.go client_url.go context.go debug.go debug_merge_logs.go debug_synctest.go demo.go disable_replication.go dump.go error.go examples.go flags.go flags_util.go format_table.go gen.go haproxy.go init.go keytype_string.go node.go sql.go sql_util.go sqlfmt.go start.go start_jemalloc.go start_unix.go systembench.go testutils.go user.go zip.go

Variables

var DebugCmd = &cobra.Command{
    Use:   "debug [command]",
    Short: "debugging commands",
    Long:  "" /* 150 byte string literal not displayed */,

    RunE: usageAndErr,
}

DebugCmd is the root of all debug commands. Exported to allow modification by CCL code.

var DebugCmdsForRocksDB = []*cobra.Command{
    debugCheckStoreCmd,
    debugCompactCmd,
    debugGCCmd,
    debugKeysCmd,
    debugRaftLogCmd,
    debugRangeDataCmd,
    debugRangeDescriptorsCmd,
    debugSSTablesCmd,
}

DebugCmdsForRocksDB lists debug commands that access rocksdb through the engine and need encryption flags (injected by CCL code). Note: do NOT include commands that just call rocksdb code without setting up an engine.

var GetAndApplyLicense func(dbConn *gosql.DB, clusterID uuid.UUID, org string) (bool, error)

GetAndApplyLicense is not implemented in order to keep OSS/BSL builds successful. The cliccl package sets this function if enterprise features are available to demo.

var PopulateRocksDBConfigHook func(*engine.RocksDBConfig) error

PopulateRocksDBConfigHook is a callback set by CCL code. It populates any needed fields in the RocksDBConfig. It must do nothing in OSS code.

var StartCmds = []*cobra.Command{startCmd, startSingleNodeCmd}

StartCmds exports startCmd and startSingleNodeCmds so that other packages can add flags to them.

func AddCmd Uses

func AddCmd(c *cobra.Command)

AddCmd adds a command to the cli.

func AddPersistentPreRunE Uses

func AddPersistentPreRunE(cmd *cobra.Command, fn func(*cobra.Command, []string) error)

AddPersistentPreRunE add 'fn' as a persistent pre-run function to 'cmd'. If the command has an existing pre-run function, it is saved and will be called at the beginning of 'fn'. This allows an arbitrary number of pre-run functions with ordering based on the order in which AddPersistentPreRunE is called (usually package init order).

func BoolFlag Uses

func BoolFlag(f *pflag.FlagSet, valPtr *bool, flagInfo cliflags.FlagInfo, defaultVal bool)

BoolFlag creates a bool flag and registers it with the FlagSet.

func DurationFlag Uses

func DurationFlag(
    f *pflag.FlagSet, valPtr *time.Duration, flagInfo cliflags.FlagInfo, defaultVal time.Duration,
)

DurationFlag creates a duration flag and registers it with the FlagSet.

func GetServerCfgStores Uses

func GetServerCfgStores() base.StoreSpecList

GetServerCfgStores provides direct public access to the StoreSpecList inside serverCfg. This is used by CCL code to populate some fields.

WARNING: consider very carefully whether you should be using this.

func IntFlag Uses

func IntFlag(f *pflag.FlagSet, valPtr *int, flagInfo cliflags.FlagInfo, defaultVal int)

IntFlag creates an int flag and registers it with the FlagSet.

func Main Uses

func Main()

Main is the entry point for the cli, with a single line calling it intended to be the body of an action package main `main` func elsewhere. It is abstracted for reuse by duplicated `main` funcs in different distributions.

func MaybeDecorateGRPCError Uses

func MaybeDecorateGRPCError(
    wrapped func(*cobra.Command, []string) error,
) func(*cobra.Command, []string) error

MaybeDecorateGRPCError catches grpc errors and provides a more helpful error message to the user.

func OpenEngine Uses

func OpenEngine(
    dir string, stopper *stop.Stopper, opts OpenEngineOptions,
) (*engine.RocksDB, error)

OpenEngine opens the RocksDB engine at 'dir'. Depending on the supplied options, an empty engine might be initialized.

func OpenExistingStore Uses

func OpenExistingStore(dir string, stopper *stop.Stopper, readOnly bool) (*engine.RocksDB, error)

OpenExistingStore opens the rocksdb engine rooted at 'dir'. If 'readOnly' is true, opens the store in read-only mode.

func Run Uses

func Run(args []string) error

Run ...

func RunCPUBench Uses

func RunCPUBench(cmd *cobra.Command, args []string) error

RunCPUBench runs the prime finding cpu benchmark.

func RunNetworkBench Uses

func RunNetworkBench(cmd *cobra.Command, args []string) error

RunNetworkBench runs the network benchmark.

func RunSeqWriteBench Uses

func RunSeqWriteBench(cmd *cobra.Command, args []string) error

RunSeqWriteBench runs a sequential write I/O benchmark.

func StringFlag Uses

func StringFlag(f *pflag.FlagSet, valPtr *string, flagInfo cliflags.FlagInfo, defaultVal string)

StringFlag creates a string flag and registers it with the FlagSet.

func StringSlice Uses

func StringSlice(
    f *pflag.FlagSet, valPtr *[]string, flagInfo cliflags.FlagInfo, defaultVal []string,
)

StringSlice creates a string slice flag and registers it with the FlagSet.

func TestingReset Uses

func TestingReset()

TestingReset resets global mutable state so that Run can be called multiple times from the same test process. It is public for cliccl.

func VarFlag Uses

func VarFlag(f *pflag.FlagSet, value pflag.Value, flagInfo cliflags.FlagInfo)

VarFlag creates a custom-variable flag and registers it with the FlagSet.

type OpenEngineOptions Uses

type OpenEngineOptions struct {
    ReadOnly  bool
    MustExist bool
}

OpenEngineOptions tunes the behavior of OpenEngine.

Directories

PathSynopsis
cliflags
syncbench
systembench
systembench/systembenchpb

Package cli imports 136 packages (graph) and is imported by 15 packages. Updated 2019-09-15. Refresh now. Tools for package owners.