azurelogout.go 916 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package logout
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/spf13/cobra"
  7. "github.com/docker/api/client"
  8. "github.com/docker/api/errdefs"
  9. )
  10. // AzureLogoutCommand returns the azure logout command
  11. func AzureLogoutCommand() *cobra.Command {
  12. cmd := &cobra.Command{
  13. Use: "azure",
  14. Short: "Logout from Azure",
  15. Args: cobra.MaximumNArgs(0),
  16. RunE: func(cmd *cobra.Command, args []string) error {
  17. return cloudLogout(cmd, "aci")
  18. },
  19. }
  20. return cmd
  21. }
  22. func cloudLogout(cmd *cobra.Command, backendType string) error {
  23. ctx := cmd.Context()
  24. cs, err := client.GetCloudService(ctx, backendType)
  25. if err != nil {
  26. return errors.Wrap(errdefs.ErrLoginFailed, "cannot connect to backend")
  27. }
  28. err = cs.Logout(ctx)
  29. if errors.Is(err, context.Canceled) {
  30. return errors.New("logout canceled")
  31. }
  32. if err != nil {
  33. return err
  34. }
  35. fmt.Println("Removing login credentials for Azure")
  36. return nil
  37. }