Sfoglia il codice sorgente

Merge pull request #325 from docker/error_status_context_needs_login

Specific exit code when command fails because azure login is required
Guillaume Tardif 5 anni fa
parent
commit
ae76e0cf27
4 ha cambiato i file con 32 aggiunte e 3 eliminazioni
  1. 1 1
      azure/resourcegroup.go
  2. 12 2
      cli/main.go
  3. 8 0
      errdefs/errors.go
  4. 11 0
      tests/e2e/e2e_test.go

+ 1 - 1
azure/resourcegroup.go

@@ -127,7 +127,7 @@ func getSubscriptionsClient() (subscription.SubscriptionsClient, error) {
 	subc := subscription.NewSubscriptionsClient()
 	err := setupClient(&subc.Client)
 	if err != nil {
-		return subscription.SubscriptionsClient{}, errors.Wrap(errdefs.ErrLoginFailed, err.Error())
+		return subscription.SubscriptionsClient{}, errors.Wrap(errdefs.ErrLoginRequired, err.Error())
 	}
 	return subc, nil
 }

+ 12 - 2
cli/main.go

@@ -27,6 +27,8 @@ import (
 	"syscall"
 	"time"
 
+	"github.com/docker/api/errdefs"
+
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/cobra"
@@ -174,13 +176,21 @@ func main() {
 		// Context should always be handled by new CLI
 		requiredCmd, _, _ := root.Find(os.Args[1:])
 		if requiredCmd != nil && isOwnCommand(requiredCmd) {
-			fatal(err)
+			exit(err)
 		}
 		mobycli.ExecIfDefaultCtxType(ctx)
 
 		checkIfUnknownCommandExistInDefaultContext(err, currentContext)
-		fatal(err)
+		exit(err)
+	}
+}
+
+func exit(err error) {
+	if errors.Is(err, errdefs.ErrLoginRequired) {
+		fmt.Fprintln(os.Stderr, fmt.Errorf("%v", err))
+		os.Exit(errdefs.ExitCodeLoginRequired)
 	}
+	fatal(err)
 }
 
 func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string) {

+ 8 - 0
errdefs/errors.go

@@ -20,6 +20,12 @@ import (
 	"github.com/pkg/errors"
 )
 
+const (
+	//ExitCodeLoginRequired exit code when command cannot execute because it requires cloud login
+	// This will be used by VSCode to detect when creating context if the user needs to login first
+	ExitCodeLoginRequired = 5
+)
+
 var (
 	// ErrNotFound is returned when an object is not found
 	ErrNotFound = errors.New("not found")
@@ -31,6 +37,8 @@ var (
 	ErrUnknown = errors.New("unknown")
 	// ErrLoginFailed is returned when login failed
 	ErrLoginFailed = errors.New("login failed")
+	// ErrLoginRequired is returned when login is required for a specific action
+	ErrLoginRequired = errors.New("login required")
 	// ErrNotImplemented is returned when a backend doesn't implement
 	// an action
 	ErrNotImplemented = errors.New("not implemented")

+ 11 - 0
tests/e2e/e2e_test.go

@@ -18,11 +18,14 @@ package main
 
 import (
 	"os"
+	"os/exec"
 	"path/filepath"
 	"runtime"
 	"testing"
 	"time"
 
+	"github.com/docker/api/errdefs"
+
 	. "github.com/onsi/gomega"
 	"github.com/stretchr/testify/suite"
 
@@ -43,6 +46,14 @@ func (s *E2eSuite) TestContextHelp() {
 	Expect(output).To(ContainSubstring("--resource-group"))
 }
 
+func (s *E2eSuite) TestContextCreateAciExitWithErrorCodeIfLoginRequired() {
+	cmd := exec.Command("docker", "context", "create", "aci", "someContext")
+	output, err := cmd.CombinedOutput()
+	Expect(err).NotTo(BeNil())
+	Expect(string(output)).To(ContainSubstring("not logged in to azure, you need to run \"docker login azure\" first"))
+	Expect(cmd.ProcessState.ExitCode()).To(Equal(errdefs.ExitCodeLoginRequired))
+}
+
 func (s *E2eSuite) TestListAndShowDefaultContext() {
 	output := s.NewDockerCommand("context", "show").ExecOrDie()
 	Expect(output).To(ContainSubstring("default"))