Merge pull request #138 from docker/token-activate

Remove token update command, replaced with token activate and token deactivate
This commit is contained in:
Djordje Lukic 2020-11-23 11:04:33 +01:00 коммит произвёл GitHub
Родитель 190137849c 642bb7dbcd
Коммит 94bd9cb11f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 138 добавлений и 89 удалений

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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
}

Просмотреть файл

@ -1,86 +0,0 @@
/*
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 (
updateName = "update"
)
type updateOptions struct {
description string
setActive bool
}
func newUpdateCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
var opts updateOptions
cmd := &cobra.Command{
Use: updateName + " [OPTIONS] TOKEN_UUID",
Short: "Update 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, updateName)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runUpdate(streams, hubClient, cmd, opts, args[0])
},
}
cmd.Flags().StringVar(&opts.description, "description", "", "Set token's description")
cmd.Flags().BoolVar(&opts.setActive, "set-active", true, "Activate or deactivate the token")
return cmd
}
func runUpdate(streams command.Streams, hubClient *hub.Client, cmd *cobra.Command, opts updateOptions, tokenUUID string) error {
u, err := uuid.Parse(tokenUUID)
if err != nil {
return err
}
token, err := hubClient.GetToken(u.String())
if err != nil {
return err
}
description := token.Description
if cmd.Flag("description").Changed {
description = opts.description
}
isActive := token.IsActive
if cmd.Flag("set-active").Changed {
isActive = opts.setActive
}
_, err = hubClient.UpdateToken(u.String(), description, isActive)
if err != nil {
return err
}
fmt.Fprintln(streams.Out(), ansi.Emphasise("Updated"), u.String())
return nil
}

Просмотреть файл

@ -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
}
@ -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 {