azure.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package logout
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/pkg/errors"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/errdefs"
  21. )
  22. // AzureLogoutCommand returns the azure logout command
  23. func AzureLogoutCommand() *cobra.Command {
  24. cmd := &cobra.Command{
  25. Use: "azure",
  26. Short: "Logout from Azure",
  27. Args: cobra.MaximumNArgs(0),
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. return cloudLogout(cmd, "aci")
  30. },
  31. }
  32. return cmd
  33. }
  34. func cloudLogout(cmd *cobra.Command, backendType string) error {
  35. ctx := cmd.Context()
  36. cs, err := client.GetCloudService(ctx, backendType)
  37. if err != nil {
  38. return errors.Wrap(errdefs.ErrLoginFailed, "cannot connect to backend")
  39. }
  40. err = cs.Logout(ctx)
  41. if errors.Is(err, context.Canceled) {
  42. return errors.New("logout canceled")
  43. }
  44. if err != nil {
  45. return err
  46. }
  47. fmt.Println("Removing login credentials for Azure")
  48. return nil
  49. }