Skip to content

Commit

Permalink
lakectl autocomplete with repository name (#4320)
Browse files Browse the repository at this point in the history
  • Loading branch information
nopcoder authored Oct 9, 2022
1 parent 220cf50 commit f212cad
Show file tree
Hide file tree
Showing 27 changed files with 275 additions and 156 deletions.
54 changes: 30 additions & 24 deletions cmd/lakectl/cmd/abuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ func readLines(path string) (lines []string, err error) {
}

var abuseRandomReadsCmd = &cobra.Command{
Use: "random-read <source ref uri>",
Short: "Read keys from a file and generate random reads from the source ref for those keys.",
Hidden: false,
Args: cobra.ExactArgs(1),
Use: "random-read <source ref uri>",
Short: "Read keys from a file and generate random reads from the source ref for those keys.",
Hidden: false,
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRefURI("source ref", args[0])
amount := MustInt(cmd.Flags().GetInt("amount"))
Expand Down Expand Up @@ -97,10 +98,11 @@ var abuseRandomReadsCmd = &cobra.Command{
}

var abuseLinkSameObjectCmd = &cobra.Command{
Use: "link-same-object <source ref uri>",
Short: "Link the same object in parallel.",
Hidden: false,
Args: cobra.ExactArgs(1),
Use: "link-same-object <source ref uri>",
Short: "Link the same object in parallel.",
Hidden: false,
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRefURI("source ref", args[0])
amount := MustInt(cmd.Flags().GetInt("amount"))
Expand Down Expand Up @@ -164,10 +166,11 @@ var abuseLinkSameObjectCmd = &cobra.Command{
}

var abuseRandomWritesCmd = &cobra.Command{
Use: "random-write <source branch uri>",
Short: "Generate random writes to the source branch",
Hidden: false,
Args: cobra.ExactArgs(1),
Use: "random-write <source branch uri>",
Short: "Generate random writes to the source branch",
Hidden: false,
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRefURI("source branch", args[0])
amount := MustInt(cmd.Flags().GetInt("amount"))
Expand Down Expand Up @@ -221,10 +224,11 @@ var abuseRandomWritesCmd = &cobra.Command{
}

var abuseCommitCmd = &cobra.Command{
Use: "commit <source ref uri>",
Short: "Commits to the source ref repeatedly",
Hidden: false,
Args: cobra.ExactArgs(1),
Use: "commit <source ref uri>",
Short: "Commits to the source ref repeatedly",
Hidden: false,
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRefURI("source ref", args[0])
amount := MustInt(cmd.Flags().GetInt("amount"))
Expand Down Expand Up @@ -272,10 +276,11 @@ var abuseCommitCmd = &cobra.Command{
}

var abuseCreateBranchesCmd = &cobra.Command{
Use: "create-branches <source ref uri>",
Short: "Create a lot of branches very quickly.",
Hidden: false,
Args: cobra.ExactArgs(1),
Use: "create-branches <source ref uri>",
Short: "Create a lot of branches very quickly.",
Hidden: false,
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRefURI("source ref", args[0])
cleanOnly := MustBool(cmd.Flags().GetBool("clean-only"))
Expand Down Expand Up @@ -361,10 +366,11 @@ var abuseCreateBranchesCmd = &cobra.Command{
}

var abuseListCmd = &cobra.Command{
Use: "list <source ref uri>",
Short: "List from the source ref",
Hidden: false,
Args: cobra.ExactArgs(1),
Use: "list <source ref uri>",
Short: "List from the source ref",
Hidden: false,
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRefURI("source ref", args[0])
amount := MustInt(cmd.Flags().GetInt("amount"))
Expand Down
4 changes: 1 addition & 3 deletions cmd/lakectl/cmd/action_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ var actionsValidateCmd = &cobra.Command{
Short: "Validate action file",
Long: `Tries to parse the input action file as lakeFS action file`,
Example: "lakectl actions validate <path>",
Args: cmdutils.ValidationChain(
cobra.ExactArgs(actionsValidateRequiredArgs),
),
Args: cmdutils.ValidationChain(cobra.ExactArgs(actionsValidateRequiredArgs)),
Run: func(cmd *cobra.Command, args []string) {
file := args[0]
reader := OpenByPath(file)
Expand Down
9 changes: 5 additions & 4 deletions cmd/lakectl/cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ type objectCommitData struct {
}

var annotateCmd = &cobra.Command{
Use: "annotate <path uri>",
Short: "List entries under a given path, annotating each with the latest modifying commit",
Aliases: []string{"blame"},
Args: cobra.ExactArgs(1),
Use: "annotate <path uri>",
Short: "List entries under a given path, annotating each with the latest modifying commit",
Aliases: []string{"blame"},
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
pathURI := MustParsePathURI("path", args[0])
recursive, _ := cmd.Flags().GetBool("recursive")
Expand Down
2 changes: 0 additions & 2 deletions cmd/lakectl/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ var authUsersPoliciesList = &cobra.Command{
ts := time.Unix(*policy.CreationDate, 0).String()
rows = append(rows, []interface{}{policy.Id, ts, i, statement.Resource, statement.Effect, strings.Join(statement.Action, ", ")})
}

}

pagination := resp.JSON200.Pagination
Expand Down Expand Up @@ -434,7 +433,6 @@ var authGroupsPoliciesList = &cobra.Command{
ts := time.Unix(*policy.CreationDate, 0).String()
rows = append(rows, []interface{}{policy.Id, ts, i, statement.Resource, statement.Effect, strings.Join(statement.Action, ", ")})
}

}

pagination := resp.JSON200.Pagination
Expand Down
43 changes: 26 additions & 17 deletions cmd/lakectl/cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ var branchCmd = &cobra.Command{
}

var branchListCmd = &cobra.Command{
Use: "list <repository uri>",
Short: "List branches in a repository",
Example: "lakectl branch list lakefs://<repository>",
Args: cobra.ExactArgs(1),
Use: "list <repository uri>",
Short: "List branches in a repository",
Example: "lakectl branch list lakefs://<repository>",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
amount := MustInt(cmd.Flags().GetInt("amount"))
after := MustString(cmd.Flags().GetString("after"))
Expand All @@ -51,10 +52,11 @@ var branchListCmd = &cobra.Command{
}

var branchCreateCmd = &cobra.Command{
Use: "create <branch uri> -s <source ref uri>",
Short: "Create a new branch in a repository",
Example: "lakectl branch create lakefs://example-repo/new-branch -s lakefs://example-repo/main",
Args: cobra.ExactArgs(1),
Use: "create <branch uri> -s <source ref uri>",
Short: "Create a new branch in a repository",
Example: "lakectl branch create lakefs://example-repo/new-branch -s lakefs://example-repo/main",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseBranchURI("branch", args[0])
client := getClient()
Expand All @@ -78,10 +80,11 @@ var branchCreateCmd = &cobra.Command{
}

var branchDeleteCmd = &cobra.Command{
Use: "delete <branch uri>",
Short: "Delete a branch in a repository, along with its uncommitted changes (CAREFUL)",
Example: "lakectl branch delete lakefs://example-repo/example-branch",
Args: cobra.ExactArgs(1),
Use: "delete <branch uri>",
Short: "Delete a branch in a repository, along with its uncommitted changes (CAREFUL)",
Example: "lakectl branch delete lakefs://example-repo/example-branch",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
confirmation, err := Confirm(cmd.Flags(), "Are you sure you want to delete branch")
if err != nil || !confirmation {
Expand All @@ -105,6 +108,9 @@ var branchRevertCmd = &cobra.Command{
branch revert lakefs://example-repo/example-branch HEAD~1 HEAD~2 HEAD~3
Revert the changes done by the second last commit to the fourth last commit in example-branch`,
Args: cobra.MinimumNArgs(branchRevertCmdArgs),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return validRepositoryToComplete(cmd.Context(), toComplete)
},
Run: func(cmd *cobra.Command, args []string) {
u := MustParseBranchURI("branch", args[0])
Fmt("Branch: %s\n", u.String())
Expand Down Expand Up @@ -140,7 +146,8 @@ var branchResetCmd = &cobra.Command{
1. reset all uncommitted changes - reset lakefs://myrepo/main
2. reset uncommitted changes under specific path - reset lakefs://myrepo/main --prefix path
3. reset uncommitted changes for specific object - reset lakefs://myrepo/main --object path`,
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
clt := getClient()
u := MustParseBranchURI("branch", args[0])
Expand Down Expand Up @@ -187,10 +194,11 @@ var branchResetCmd = &cobra.Command{
}

var branchShowCmd = &cobra.Command{
Use: "show <branch uri>",
Example: "lakectl branch show lakefs://example-repo/example-branch",
Short: "Show branch latest commit reference",
Args: cobra.ExactArgs(1),
Use: "show <branch uri>",
Example: "lakectl branch show lakefs://example-repo/example-branch",
Short: "Show branch latest commit reference",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
client := getClient()
u := MustParseBranchURI("branch", args[0])
Expand All @@ -217,6 +225,7 @@ func init() {

branchCreateCmd.Flags().StringP("source", "s", "", "source branch uri")
_ = branchCreateCmd.MarkFlagRequired("source")
_ = branchCreateCmd.RegisterFlagCompletionFunc("source", ValidArgsRepository)

branchResetCmd.Flags().String("prefix", "", "prefix of the objects to be reset")
branchResetCmd.Flags().String("object", "", "path to object to be reset")
Expand Down
32 changes: 18 additions & 14 deletions cmd/lakectl/cmd/branch_protect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ var branchProtectCmd = &cobra.Command{
Short: "Create and manage branch protection rules",
Long: "Define branch protection rules to prevent direct changes. Changes to protected branches can only be done by merging from other branches.",
}

var branchProtectListCmd = &cobra.Command{
Use: "list <repo uri>",
Short: "List all branch protection rules",
Example: "lakectl branch-protect list lakefs://<repository>",
Args: cobra.ExactArgs(1),
Use: "list <repo uri>",
Short: "List all branch protection rules",
Example: "lakectl branch-protect list lakefs://<repository>",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
client := getClient()
u := MustParseRepoURI("repository", args[0])
Expand All @@ -39,11 +41,12 @@ var branchProtectListCmd = &cobra.Command{
}

var branchProtectAddCmd = &cobra.Command{
Use: "add <repo uri> <pattern>",
Short: "Add a branch protection rule",
Long: "Add a branch protection rule for a given branch name pattern",
Example: "lakectl branch-protect add lakefs://<repository> 'stable_*'",
Args: cobra.ExactArgs(branchProtectAddCmdArgs),
Use: "add <repo uri> <pattern>",
Short: "Add a branch protection rule",
Long: "Add a branch protection rule for a given branch name pattern",
Example: "lakectl branch-protect add lakefs://<repository> 'stable_*'",
Args: cobra.ExactArgs(branchProtectAddCmdArgs),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
client := getClient()
u := MustParseRepoURI("repository", args[0])
Expand All @@ -55,11 +58,12 @@ var branchProtectAddCmd = &cobra.Command{
}

var branchProtectDeleteCmd = &cobra.Command{
Use: "delete <repo uri> <pattern>",
Short: "Delete a branch protection rule",
Long: "Delete a branch protection rule for a given branch name pattern",
Example: "lakectl branch-protect delete lakefs://<repository> stable_*",
Args: cobra.ExactArgs(branchProtectDeleteCmdArgs),
Use: "delete <repo uri> <pattern>",
Short: "Delete a branch protection rule",
Long: "Delete a branch protection rule for a given branch name pattern",
Example: "lakectl branch-protect delete lakefs://<repository> stable_*",
Args: cobra.ExactArgs(branchProtectDeleteCmdArgs),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
client := getClient()
u := MustParseRepoURI("repository", args[0])
Expand Down
11 changes: 6 additions & 5 deletions cmd/lakectl/cmd/cat_hook_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
const catHookOutputRequiredArgs = 3

var catHookOutputCmd = &cobra.Command{
Use: "cat-hook-output",
Short: "Cat actions hook output",
Hidden: true,
Example: "lakectl cat-hook-output lakefs://<repository> <run_id> <run_hook_id>",
Args: cobra.ExactArgs(catHookOutputRequiredArgs),
Use: "cat-hook-output",
Short: "Cat actions hook output",
Hidden: true,
Example: "lakectl cat-hook-output lakefs://<repository> <run_id> <run_hook_id>",
Args: cobra.ExactArgs(catHookOutputRequiredArgs),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
u := MustParseRepoURI("repository", args[0])
Fmt("Repository: %s\n", u.String())
Expand Down
7 changes: 4 additions & 3 deletions cmd/lakectl/cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ Parents: {{.Commit.Parents|join ", "}}
)

var commitCmd = &cobra.Command{
Use: "commit <branch uri>",
Short: "Commit changes on a given branch",
Args: cobra.ExactArgs(1),
Use: "commit <branch uri>",
Short: "Commit changes on a given branch",
Args: cobra.ExactArgs(1),
ValidArgsFunction: ValidArgsRepository,
Run: func(cmd *cobra.Command, args []string) {
// validate message
kvPairs, err := getKV(cmd, metaFlagName)
Expand Down
2 changes: 1 addition & 1 deletion cmd/lakectl/cmd/common_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestIsValidAccessKeyID(t *testing.T) {
{name: "access key id with extra char", args: args{accessKeyID: "AKIAJ123ZZZZZZZZZZZZQ"}, want: false},
{name: "access key id with missing char", args: args{accessKeyID: "AKIAJ1ZZZZZZZZZZZZQ"}, want: false},
{name: "access key id with wrong prefix", args: args{accessKeyID: "AKIAM12ZZZZZZZZZZZZQ"}, want: false},
{name: "access key id with wrong suffiix", args: args{accessKeyID: "AKIAJ12ZZZZZZZZZZZZA"}, want: false},
{name: "access key id with wrong suffix", args: args{accessKeyID: "AKIAJ12ZZZZZZZZZZZZA"}, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions cmd/lakectl/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (c *Config) GetMetastoreHiveURI() string {
func (c *Config) GetMetastoreGlueCatalogID() string {
return string(c.Values.Metastore.Glue.CatalogID)
}

func (c *Config) GetMetastoreType() string {
return c.Values.Metastore.Type
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/lakectl/cmd/dbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
mserrors "github.com/treeverse/lakefs/pkg/metastore/errors"
)

var schemaRegex = regexp.MustCompile(`schema: (.+)`)
var materializedSelection = []string{"config.materialized:table", "config.materialized:incremental"}
var (
schemaRegex = regexp.MustCompile(`schema: (.+)`)
materializedSelection = []string{"config.materialized:table", "config.materialized:incremental"}
)

const (
schemaEnvVarName = "LAKEFS_SCHEMA"
Expand Down Expand Up @@ -186,7 +188,7 @@ generate_schema_name.sql
{%- endmacro %}
`
//nolint:gosec
err := ioutil.WriteFile(macroPath, []byte(generateSchemaData), 0644) //nolint: gomnd
err := ioutil.WriteFile(macroPath, []byte(generateSchemaData), 0o644) //nolint: gomnd
if err != nil {
DieErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/lakectl/cmd/dbt_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestDbtDebug(t *testing.T) {
schemaRegex *regexp.Regexp
executor DummyCommandExecutor
}
var schemaRegex = regexp.MustCompile(`schema: (.+)`)
schemaRegex := regexp.MustCompile(`schema: (.+)`)

type dbtDebugTestCase struct {
Name string
Expand Down
6 changes: 6 additions & 0 deletions cmd/lakectl/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ var diffCmd = &cobra.Command{
Show changes between the tip of the main and the dev branch, including uncommitted changes on dev.`, twoWayFlagName, twoWayFlagName),

Args: cobra.RangeArgs(diffCmdMinArgs, diffCmdMaxArgs),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) >= diffCmdMaxArgs {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return validRepositoryToComplete(cmd.Context(), toComplete)
},
Run: func(cmd *cobra.Command, args []string) {
client := getClient()
if len(args) == diffCmdMinArgs {
Expand Down
Loading

0 comments on commit f212cad

Please sign in to comment.