Implement azure logout

This commit is contained in:
Guillaume Tardif 2020-07-08 16:01:54 +02:00
parent 78cb0a87c7
commit 492ce96dd4
10 changed files with 130 additions and 23 deletions

View file

@ -35,7 +35,7 @@ import (
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "login [OPTIONS] [SERVER]",
Short: "Log in to a Docker registry",
Short: "Log in to a Docker registry or cloud backend",
Long: "Log in to a Docker registry or cloud backend.\nIf no registry server is specified, the default is defined by the daemon.",
Args: cobra.MaximumNArgs(1),
RunE: runLogin,

View file

@ -0,0 +1,42 @@
package logout
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/docker/api/client"
"github.com/docker/api/errdefs"
)
// AzureLogoutCommand returns the azure logout command
func AzureLogoutCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "azure",
Short: "Logout from Azure",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return cloudLogout(cmd, "aci")
},
}
return cmd
}
func cloudLogout(cmd *cobra.Command, backendType string) error {
ctx := cmd.Context()
cs, err := client.GetCloudService(ctx, backendType)
if err != nil {
return errors.Wrap(errdefs.ErrLoginFailed, "cannot connect to backend")
}
err = cs.Logout(ctx)
if errors.Is(err, context.Canceled) {
return errors.New("logout canceled")
}
if err != nil {
return err
}
fmt.Println("Removing login credentials for Azure")
return nil
}

25
cli/cmd/logout/logout.go Normal file
View file

@ -0,0 +1,25 @@
package logout
import (
"github.com/spf13/cobra"
"github.com/docker/api/cli/mobycli"
)
// Command returns the login command
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "logout [SERVER]",
Short: "Log out from a Docker registry or cloud backend",
Long: "Log out from a Docker registry or cloud backend.\nIf no server is specified, the default is defined by the daemon.",
Args: cobra.MaximumNArgs(0),
RunE: runLogout,
}
cmd.AddCommand(AzureLogoutCommand())
return cmd
}
func runLogout(cmd *cobra.Command, args []string) error {
return mobycli.ExecCmd(cmd)
}

View file

@ -27,6 +27,8 @@ import (
"syscall"
"time"
"github.com/docker/api/cli/cmd/logout"
"github.com/docker/api/errdefs"
"github.com/pkg/errors"
@ -59,6 +61,7 @@ var (
ownCommands = map[string]struct{}{
"context": {},
"login": {},
"logout": {},
"serve": {},
"version": {},
}
@ -117,6 +120,7 @@ func main() {
cmd.InspectCommand(),
compose.Command(),
login.Command(),
logout.Command(),
cmd.VersionCommand(version),
)