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"})
c.RunWithCAArgs([]string{"cert", "create-client", ",foo"})

Output:

cert create-client foo
cert create-client Ομηρος
cert create-client 0foo
cert create-client ,foo
ERROR: failed to generate client certificate and key: username ",foo" invalid
SQLSTATE: 42602
HINT: Usernames are case insensitive, must start with a letter, digit or underscore, may contain letters, digits, dashes, periods, 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`},
    // Test that if we start with --insecure we cannot perform
    // commands that require a secure cluster.
    {`demo`, `-e`, `CREATE USER test WITH PASSWORD 'testpass'`},
    {`demo`, `--insecure`, `-e`, `CREATE USER test WITH PASSWORD 'testpass'`},
    {`demo`, `--geo-partitioned-replicas`, `--disable-demo-license`},
}
setCLIDefaultsForTests()
// We must reset the security asset loader here, otherwise the dummy
// asset loader that is set by default in tests will not be able to
// find the certs that demo sets up.
security.ResetAssetLoader()
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"
ERROR: column "nonexistent" does not exist
SQLSTATE: 42703
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)
demo -e CREATE USER test WITH PASSWORD 'testpass'
CREATE ROLE
demo --insecure -e CREATE USER test WITH PASSWORD 'testpass'
ERROR: setting or updating a password is not supported in insecure mode
SQLSTATE: 28P01
demo --geo-partitioned-replicas --disable-demo-license
ERROR: enterprise features are needed for this demo (--geo-partitioned-replicas)

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")
c.RunWithArgs([]string{"sql", "-e", "drop database defaultdb"})
c.Run("node ls")

Output:

node ls
id
1
node ls --format=table
  id
------
   1
(1 row)
node status 10000
ERROR: node 10000 doesn't exist
sql -e drop database defaultdb
DROP DATABASE
node ls
id
1

Code:

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

file, cleanUp := createTestFile("test.csv", "content")
defer cleanUp()

c.Run(fmt.Sprintf("nodelocal upload %s /test/file1.csv", file))
c.Run(fmt.Sprintf("nodelocal upload %s /test/file2.csv", file))
c.Run(fmt.Sprintf("nodelocal upload %s /test/file1.csv", file))
c.Run(fmt.Sprintf("nodelocal upload %s /test/../../file1.csv", file))
c.Run(fmt.Sprintf("nodelocal upload notexist.csv /test/file1.csv"))

Output:

nodelocal upload test.csv /test/file1.csv
successfully uploaded to nodelocal://1/test/file1.csv
nodelocal upload test.csv /test/file2.csv
successfully uploaded to nodelocal://1/test/file2.csv
nodelocal upload test.csv /test/file1.csv
ERROR: destination file already exists for /test/file1.csv
nodelocal upload test.csv /test/../../file1.csv
ERROR: local file access to paths outside of external-io-dir is not allowed: ../file1.csv
nodelocal upload notexist.csv /test/file1.csv
ERROR: open notexist.csv: no such file or directory

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`})
// --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"`})
// Check that partial results + error get reported together.
c.RunWithArgs([]string{`sql`, `-e`, `select 1/(@1-3) from generate_series(1,4)`})

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
ERROR: woops! COPY has confused this client! Suggestion: use 'psql' for COPY
sql --set=errexit=0 -e select nonexistent -e select 123 as "123"
ERROR: column "nonexistent" does not exist
SQLSTATE: 42703
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.
ERROR: invalid syntax
sql -e select 1/(@1-3) from generate_series(1,4)
?column?
-0.5
-1
(error encountered after some results were delivered)
ERROR: division by zero
SQLSTATE: 22012

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

Index

Examples

Package Files

auth.go cert.go cli.go client_url.go context.go debug.go debug_check_store.go debug_merge_logs.go debug_synctest.go demo.go demo_cluster.go demo_telemetry.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 mt.go mt_cert.go mt_start_sql.go node.go nodelocal.go quit.go sql.go sql_util.go sqlfmt.go start.go start_jemalloc.go start_unix.go statement_diag.go systembench.go testutils.go tsdump.go userfile.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(*base.StorageConfig) 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.

var VarFlag = varFlag

VarFlag is exported for use in package cliccl.

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 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. If you are not writing CCL code that performs command-line flag parsing, you probably should not be using this.

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) (storage.Engine, error)

OpenEngine opens the 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) (storage.Engine, 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 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.

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 150 packages (graph) and is imported by 69 packages. Updated 2020-08-05. Refresh now. Tools for package owners.