Skip to content

Commit

Permalink
Bugfix/2935 lakectl bug on not found html (#2966)
Browse files Browse the repository at this point in the history
* Changed all CLI commands to fail on unexpected status code returned and avoiding redirect automatically.
  • Loading branch information
idanovo authored Feb 28, 2022
1 parent 2e2af31 commit a8340bf
Show file tree
Hide file tree
Showing 25 changed files with 160 additions and 104 deletions.
10 changes: 5 additions & 5 deletions cmd/lakectl/cmd/abuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var abuseRandomWritesCmd = &cobra.Command{

client := getClient()
resp, err := client.GetRepositoryWithResponse(cmd.Context(), u.Repository)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

repo := resp.JSON200
storagePrefix := repo.StorageNamespace
Expand Down Expand Up @@ -173,19 +173,19 @@ var abuseCreateBranchesCmd = &cobra.Command{
currentOffset := api.PaginationAfter(branchPrefix)
amount := api.PaginationAmount(paginationAmount)
for {
res, err := client.ListBranchesWithResponse(cmd.Context(), u.Repository, &api.ListBranchesParams{
resp, err := client.ListBranchesWithResponse(cmd.Context(), u.Repository, &api.ListBranchesParams{
After: &currentOffset,
Amount: &amount,
})
DieOnResponseError(res, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

for _, ref := range res.JSON200.Results {
for _, ref := range resp.JSON200.Results {
if !strings.HasPrefix(ref.Id, branchPrefix) {
return
}
add(ref.Id) // this branch should be deleted!
}
pagination := res.JSON200.Pagination
pagination := resp.JSON200.Pagination
if !pagination.HasMore {
return
}
Expand Down
21 changes: 11 additions & 10 deletions cmd/lakectl/cmd/annotate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"net/http"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -31,33 +32,33 @@ var annotateCmd = &cobra.Command{
client := getClient()
pfx := api.PaginationPrefix(*pathURI.Path)
context := cmd.Context()
res, err := client.ListObjectsWithResponse(context, pathURI.Repository, pathURI.Ref, &api.ListObjectsParams{Prefix: &pfx})
DieOnResponseError(res, err)
resp, err := client.ListObjectsWithResponse(context, pathURI.Repository, pathURI.Ref, &api.ListObjectsParams{Prefix: &pfx})
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
var from string
for {
params := &api.ListObjectsParams{
Prefix: &pfx,
After: api.PaginationAfterPtr(from),
}
resp, err := client.ListObjectsWithResponse(context, pathURI.Repository, pathURI.Ref, params)
DieOnResponseError(resp, err)
listObjectsResp, err := client.ListObjectsWithResponse(context, pathURI.Repository, pathURI.Ref, params)
DieOnErrorOrUnexpectedStatusCode(listObjectsResp, err, http.StatusOK)
for _, obj := range resp.JSON200.Results {
logCommitsParams := &api.LogCommitsParams{
Amount: api.PaginationAmountPtr(1),
Objects: &[]string{obj.Path},
}
res, err := client.LogCommitsWithResponse(context, pathURI.Repository, pathURI.Ref, logCommitsParams)
DieOnResponseError(res, err)
logCommitsResp, err := client.LogCommitsWithResponse(context, pathURI.Repository, pathURI.Ref, logCommitsParams)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
data := objectCommitData{
Object: obj.Path,
}
if len(res.JSON200.Results) > 0 {
data.Commit = res.JSON200.Results[0]
data.CommitMessage = splitOnNewLine(stringTrimLen((res.JSON200.Results[0].Message), annotateMessageSize))
if len(logCommitsResp.JSON200.Results) > 0 {
data.Commit = logCommitsResp.JSON200.Results[0]
data.CommitMessage = splitOnNewLine(stringTrimLen((logCommitsResp.JSON200.Results[0].Message), annotateMessageSize))
}
Write(annotateTemplate, data)
}
pagination := resp.JSON200.Pagination
pagination := listObjectsResp.JSON200.Pagination
if !pagination.HasMore {
break
}
Expand Down
53 changes: 27 additions & 26 deletions cmd/lakectl/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"io"
"net/http"
"os"
"strings"
"time"
Expand Down Expand Up @@ -63,7 +64,7 @@ var authUsersList = &cobra.Command{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

users := resp.JSON200.Results
rows := make([][]interface{}, len(users))
Expand All @@ -87,7 +88,7 @@ var authUsersCreate = &cobra.Command{
resp, err := clt.CreateUserWithResponse(cmd.Context(), api.CreateUserJSONRequestBody{
Id: id,
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
user := resp.JSON201
Write(userCreatedTemplate, user)
},
Expand All @@ -101,7 +102,7 @@ var authUsersDelete = &cobra.Command{
clt := getClient()

resp, err := clt.DeleteUserWithResponse(cmd.Context(), id)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
Fmt("User deleted successfully\n")
},
}
Expand All @@ -125,7 +126,7 @@ var authUsersGroupsList = &cobra.Command{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

groups := resp.JSON200.Results
rows := make([][]interface{}, len(groups))
Expand Down Expand Up @@ -160,7 +161,7 @@ var authUsersPoliciesList = &cobra.Command{
Amount: api.PaginationAmountPtr(amount),
Effective: &effective,
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

policies := resp.JSON200.Results
rows := make([][]interface{}, 0)
Expand All @@ -185,7 +186,7 @@ var authUsersPoliciesAttach = &cobra.Command{
policy, _ := cmd.Flags().GetString("policy")
clt := getClient()
resp, err := clt.AttachPolicyToUserWithResponse(cmd.Context(), id, policy)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
Fmt("Policy attached successfully\n")
},
}
Expand All @@ -199,7 +200,7 @@ var authUsersPoliciesDetach = &cobra.Command{
clt := getClient()

resp, err := clt.DetachPolicyFromUserWithResponse(cmd.Context(), id, policy)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)

Fmt("Policy detached successfully\n")
},
Expand All @@ -219,12 +220,12 @@ var authUsersCredentialsCreate = &cobra.Command{

if id == "" {
resp, err := clt.GetCurrentUserWithResponse(cmd.Context())
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
id = resp.JSON200.User.Id
}

resp, err := clt.CreateCredentialsWithResponse(cmd.Context(), id)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)

credentials := resp.JSON201
Write(credentialsCreatedTemplate, credentials)
Expand All @@ -241,11 +242,11 @@ var authUsersCredentialsDelete = &cobra.Command{

if id == "" {
resp, err := clt.GetCurrentUserWithResponse(cmd.Context())
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
id = resp.JSON200.User.Id
}
resp, err := clt.DeleteCredentialsWithResponse(cmd.Context(), id, accessKeyID)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)

Fmt("Credentials deleted successfully\n")
},
Expand All @@ -262,15 +263,15 @@ var authUsersCredentialsList = &cobra.Command{
clt := getClient()
if id == "" {
resp, err := clt.GetCurrentUserWithResponse(cmd.Context())
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
id = resp.JSON200.User.Id
}

resp, err := clt.ListUserCredentialsWithResponse(cmd.Context(), id, &api.ListUserCredentialsParams{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

credentials := resp.JSON200.Results
rows := make([][]interface{}, len(credentials))
Expand Down Expand Up @@ -302,7 +303,7 @@ var authGroupsList = &cobra.Command{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

groups := resp.JSON200.Results
rows := make([][]interface{}, len(groups))
Expand All @@ -326,7 +327,7 @@ var authGroupsCreate = &cobra.Command{
resp, err := clt.CreateGroupWithResponse(cmd.Context(), api.CreateGroupJSONRequestBody{
Id: id,
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
group := resp.JSON201
Write(groupCreatedTemplate, group)
},
Expand All @@ -340,7 +341,7 @@ var authGroupsDelete = &cobra.Command{
clt := getClient()

resp, err := clt.DeleteGroupWithResponse(cmd.Context(), id)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
Fmt("Group deleted successfully\n")
},
}
Expand All @@ -364,7 +365,7 @@ var authGroupsListMembers = &cobra.Command{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

users := resp.JSON200.Results
rows := make([][]interface{}, len(users))
Expand All @@ -386,7 +387,7 @@ var authGroupsAddMember = &cobra.Command{
clt := getClient()

resp, err := clt.AddGroupMembershipWithResponse(cmd.Context(), id, user)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
Fmt("User successfully added\n")
},
}
Expand All @@ -400,7 +401,7 @@ var authGroupsRemoveMember = &cobra.Command{
clt := getClient()

resp, err := clt.DeleteGroupMembershipWithResponse(cmd.Context(), id, user)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
Fmt("User successfully removed\n")
},
}
Expand All @@ -424,7 +425,7 @@ var authGroupsPoliciesList = &cobra.Command{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

policies := resp.JSON200.Results
rows := make([][]interface{}, 0)
Expand All @@ -450,7 +451,7 @@ var authGroupsPoliciesAttach = &cobra.Command{
clt := getClient()

resp, err := clt.AttachPolicyToGroupWithResponse(cmd.Context(), id, policy)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)

Fmt("Policy attached successfully\n")
},
Expand All @@ -465,7 +466,7 @@ var authGroupsPoliciesDetach = &cobra.Command{
clt := getClient()

resp, err := clt.DetachPolicyFromGroupWithResponse(cmd.Context(), id, policy)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)

Fmt("Policy detached successfully\n")
},
Expand All @@ -490,7 +491,7 @@ var authPoliciesList = &cobra.Command{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

policies := resp.JSON200.Results
rows := make([][]interface{}, len(policies))
Expand Down Expand Up @@ -534,7 +535,7 @@ var authPoliciesCreate = &cobra.Command{
Id: id,
Statement: doc.Statement,
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)

createdPolicy := resp.JSON201
Write(policyCreatedTemplate, struct {
Expand All @@ -561,7 +562,7 @@ var authPoliciesShow = &cobra.Command{
clt := getClient()

resp, err := clt.GetPolicyWithResponse(cmd.Context(), id)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

policy := *resp.JSON200
Write(policyDetailsTemplate, struct {
Expand All @@ -584,7 +585,7 @@ var authPoliciesDelete = &cobra.Command{
clt := getClient()

resp, err := clt.DeletePolicyWithResponse(cmd.Context(), id)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
Fmt("Policy deleted successfully\n")
},
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/lakectl/cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"net/http"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,7 +37,7 @@ var branchListCmd = &cobra.Command{
After: api.PaginationAfterPtr(after),
Amount: api.PaginationAmountPtr(amount),
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)

refs := resp.JSON200.Results
rows := make([][]interface{}, len(refs))
Expand Down Expand Up @@ -70,7 +71,7 @@ var branchCreateCmd = &cobra.Command{
Name: u.Ref,
Source: sourceURI.Ref,
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
Fmt("created branch '%s' %s\n", u.Ref, string(resp.Body))
},
}
Expand All @@ -88,7 +89,7 @@ var branchDeleteCmd = &cobra.Command{
u := MustParseRefURI("branch", args[0])
Fmt("Branch: %s\n", u.String())
resp, err := client.DeleteBranchWithResponse(cmd.Context(), u.Repository, u.Ref)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
},
}

Expand Down Expand Up @@ -122,7 +123,7 @@ var branchRevertCmd = &cobra.Command{
ParentNumber: parentNumber,
Ref: commitRef,
})
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
Fmt("commit %s successfully reverted\n", commitRef)
}
},
Expand Down Expand Up @@ -178,7 +179,7 @@ var branchResetCmd = &cobra.Command{
return
}
resp, err := clt.ResetBranchWithResponse(cmd.Context(), u.Repository, u.Ref, api.ResetBranchJSONRequestBody(reset))
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
},
}

Expand All @@ -191,7 +192,7 @@ var branchShowCmd = &cobra.Command{
u := MustParseRefURI("branch", args[0])
Fmt("Branch: %s\n", u.String())
resp, err := client.GetBranchWithResponse(cmd.Context(), u.Repository, u.Ref)
DieOnResponseError(resp, err)
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
branch := resp.JSON200
Fmt("%s\n", branch)
},
Expand Down
Loading

0 comments on commit a8340bf

Please sign in to comment.