Skip to content

Commit

Permalink
Merge pull request #138 from docker/token-activate
Browse files Browse the repository at this point in the history
Remove token update command, replaced with token activate and token deactivate
  • Loading branch information
rumpl authored Nov 23, 2020
2 parents 1901378 + 642bb7d commit 94bd9cb
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 89 deletions.
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

0 comments on commit 94bd9cb

Please sign in to comment.