Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove token update command, replaced with token activate and token deactivate #138

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions internal/commands/token/activate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2020 Docker Hub Tool authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package token

import (
"fmt"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/google/uuid"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
)

const (
activateName = "activate"
)

func newActivateCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
cmd := &cobra.Command{
Use: activateName + " TOKEN_UUID",
Short: "Activate a Personal Access Token",
Args: cli.ExactArgs(1),
DisableFlagsInUseLine: true,
Annotations: map[string]string{
"sudo": "true",
},
PreRun: func(cmd *cobra.Command, args []string) {
metrics.Send(parent, activateName)
},
RunE: func(_ *cobra.Command, args []string) error {
return runActivate(streams, hubClient, args[0])
},
}
return cmd
}

func runActivate(streams command.Streams, hubClient *hub.Client, tokenUUID string) error {
u, err := uuid.Parse(tokenUUID)
if err != nil {
return err
}
if _, err := hubClient.UpdateToken(u.String(), "", true); err != nil {
return err
}
fmt.Fprintf(streams.Out(), ansi.Emphasise("%s is active\n"), u.String())
return nil
}
3 changes: 2 additions & 1 deletion internal/commands/token/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func NewTokenCmd(streams command.Streams, hubClient *hub.Client) *cobra.Command
newCreateCmd(streams, hubClient, tokenName),
newInspectCmd(streams, hubClient, tokenName),
newListCmd(streams, hubClient, tokenName),
newUpdateCmd(streams, hubClient, tokenName),
newActivateCmd(streams, hubClient, tokenName),
newDeactivateCmd(streams, hubClient, tokenName),
newRmCmd(streams, hubClient, tokenName),
)
return cmd
Expand Down
65 changes: 65 additions & 0 deletions internal/commands/token/deactivate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2020 Docker Hub Tool authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package token

import (
"fmt"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/google/uuid"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
)

const (
deactivateName = "deactivate"
)

func newDeactivateCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
cmd := &cobra.Command{
Use: deactivateName + " TOKEN_UUID",
Short: "Deactivate a Personal Access Token",
Args: cli.ExactArgs(1),
DisableFlagsInUseLine: true,
Annotations: map[string]string{
"sudo": "true",
},
PreRun: func(cmd *cobra.Command, args []string) {
metrics.Send(parent, deactivateName)
},
RunE: func(_ *cobra.Command, args []string) error {
return runDeactivate(streams, hubClient, args[0])
},
}
return cmd
}

func runDeactivate(streams command.Streams, hubClient *hub.Client, tokenUUID string) error {
u, err := uuid.Parse(tokenUUID)
if err != nil {
return err
}
if _, err := hubClient.UpdateToken(u.String(), "", false); err != nil {
return err
}
fmt.Fprintf(streams.Out(), ansi.Emphasise("%s is inactive\n"), u.String())
return nil
}
86 changes: 0 additions & 86 deletions internal/commands/token/update.go

This file was deleted.

8 changes: 6 additions & 2 deletions internal/hub/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ func (c *Client) GetToken(tokenUUID string) (*Token, error) {

// UpdateToken updates a token's description and activeness
func (c *Client) UpdateToken(tokenUUID, description string, isActive bool) (*Token, error) {
data, err := json.Marshal(hubTokenRequest{Description: description, IsActive: isActive})
tokenRequest := hubTokenRequest{IsActive: isActive}
if description != "" {
tokenRequest.Description = description
}
data, err := json.Marshal(tokenRequest)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -187,7 +191,7 @@ func (c *Client) getTokensPage(url string) ([]Token, int, string, error) {

type hubTokenRequest struct {
Description string `json:"token_label,omitempty"`
IsActive bool `json:"is_active,omitempty"`
IsActive bool `json:"is_active"`
}

type hubTokenResponse struct {
Expand Down