Skip to content

Commit

Permalink
Add --storage-id flag to Repo Create CLI (#8557)
Browse files Browse the repository at this point in the history
  • Loading branch information
itaigilo authored Feb 10, 2025
1 parent ac82f88 commit 502432d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
18 changes: 11 additions & 7 deletions cmd/lakectl/cmd/repo_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
DefaultBranch = "main"

SampleDataFlag = "sample-data"
defaultBranchFlagName = "default-branch"
defaultBranchFlagValue = "main"
sampleDataFlagName = "sample-data"

repoCreateCmdArgs = 2
)
Expand All @@ -27,13 +27,16 @@ var repoCreateCmd = &cobra.Command{
clt := getClient()
u := MustParseRepoURI("repository URI", args[0])
fmt.Println("Repository:", u)
defaultBranch := Must(cmd.Flags().GetString("default-branch"))
sampleData := Must(cmd.Flags().GetBool(SampleDataFlag))

defaultBranch := Must(cmd.Flags().GetString(defaultBranchFlagName))
sampleData := Must(cmd.Flags().GetBool(sampleDataFlagName))
storageID := Must(cmd.Flags().GetString(storageIDFlagName))

resp, err := clt.CreateRepositoryWithResponse(cmd.Context(),
&apigen.CreateRepositoryParams{},
apigen.CreateRepositoryJSONRequestBody{
Name: u.Repository,
StorageId: &storageID,
StorageNamespace: args[1],
DefaultBranch: &defaultBranch,
SampleData: &sampleData,
Expand All @@ -53,8 +56,9 @@ var repoCreateCmd = &cobra.Command{

//nolint:gochecknoinits
func init() {
repoCreateCmd.Flags().StringP("default-branch", "d", DefaultBranch, "the default branch of this repository")
repoCreateCmd.Flags().Bool(SampleDataFlag, false, "create sample data in the repository")
repoCreateCmd.Flags().StringP(defaultBranchFlagName, "d", defaultBranchFlagValue, "the default branch of this repository")
repoCreateCmd.Flags().Bool(sampleDataFlagName, false, "create sample data in the repository")
withStorageID(repoCreateCmd)

repoCmd.AddCommand(repoCreateCmd)
}
13 changes: 8 additions & 5 deletions cmd/lakectl/cmd/repo_create_bare.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ var repoCreateBareCmd = &cobra.Command{
clt := getClient()
u := MustParseRepoURI("repository URI", args[0])
fmt.Println("Repository:", u)
defaultBranch, err := cmd.Flags().GetString("default-branch")
if err != nil {
DieErr(err)
}

defaultBranch := Must(cmd.Flags().GetString(defaultBranchFlagName))
storageID := Must(cmd.Flags().GetString(storageIDFlagName))

bareRepo := true

resp, err := clt.CreateRepositoryWithResponse(cmd.Context(), &apigen.CreateRepositoryParams{
Bare: &bareRepo,
}, apigen.CreateRepositoryJSONRequestBody{
DefaultBranch: &defaultBranch,
Name: u.Repository,
StorageId: &storageID,
StorageNamespace: args[1],
})
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
Expand All @@ -43,7 +45,8 @@ var repoCreateBareCmd = &cobra.Command{

//nolint:gochecknoinits
func init() {
repoCreateBareCmd.Flags().StringP("default-branch", "d", DefaultBranch, "the default branch name of this repository (will not be created)")
repoCreateBareCmd.Flags().StringP(defaultBranchFlagName, "d", defaultBranchFlagValue, "the default branch name of this repository (will not be created)")
withStorageID(repoCreateBareCmd)

repoCmd.AddCommand(repoCreateBareCmd)
}
8 changes: 8 additions & 0 deletions cmd/lakectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ var (
const (
recursiveFlagName = "recursive"
recursiveFlagShort = "r"
storageIDFlagName = "storage-id"
presignFlagName = "pre-sign"
parallelismFlagName = "parallelism"
noProgressBarFlagName = "no-progress"
Expand Down Expand Up @@ -187,6 +188,13 @@ func withRecursiveFlag(cmd *cobra.Command, usage string) {
cmd.Flags().BoolP(recursiveFlagName, recursiveFlagShort, false, usage)
}

func withStorageID(cmd *cobra.Command) {
cmd.Flags().String(storageIDFlagName, "", "")
if err := cmd.Flags().MarkHidden(storageIDFlagName); err != nil {
DieErr(err)
}
}

func withParallelismFlag(cmd *cobra.Command) {
cmd.Flags().IntP(parallelismFlagName, "p", defaultParallelism,
"Max concurrent operations to perform")
Expand Down
3 changes: 3 additions & 0 deletions esti/golden/lakectl_repo_create_with_storage_id.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Repository: lakefs://${REPO}
storage id: invalid value: validation error
400 Bad Request
13 changes: 13 additions & 0 deletions esti/lakectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ func TestLakectlBasicRepoActions(t *testing.T) {
RunCmdAndVerifySuccessWithFile(t, Lakectl()+" repo create lakefs://"+repoName3+" "+storage3+" --sample-data", false, "lakectl_repo_create_sample", vars)
}

func TestLakectlRepoCreateWithStorageID(t *testing.T) {
// Validate the --storage-id flag (currently only allowed to be empty)
repoName := generateUniqueRepositoryName()
storage := generateUniqueStorageNamespace(repoName)
vars := map[string]string{
"REPO": repoName,
"STORAGE": storage,
"BRANCH": mainBranch,
}
RunCmdAndVerifyFailureWithFile(t, Lakectl()+" repo create lakefs://"+repoName+" "+storage+" --storage-id storage1", false, "lakectl_repo_create_with_storage_id", vars)
RunCmdAndVerifySuccessWithFile(t, Lakectl()+" repo create lakefs://"+repoName+" "+storage+" --storage-id \"\"", false, "lakectl_repo_create", vars)
}

func TestLakectlPreSignUpload(t *testing.T) {
repoName := generateUniqueRepositoryName()
storage := generateUniqueStorageNamespace(repoName)
Expand Down

0 comments on commit 502432d

Please sign in to comment.