Merge pull request #325 from docker/error_status_context_needs_login

Specific exit code when command fails because azure login is required
This commit is contained in:
Guillaume Tardif 2020-07-02 17:02:57 +02:00 committed by GitHub
commit ae76e0cf27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 3 deletions

View File

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

View File

@ -27,6 +27,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/docker/api/errdefs"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -174,15 +176,23 @@ func main() {
// Context should always be handled by new CLI // Context should always be handled by new CLI
requiredCmd, _, _ := root.Find(os.Args[1:]) requiredCmd, _, _ := root.Find(os.Args[1:])
if requiredCmd != nil && isOwnCommand(requiredCmd) { if requiredCmd != nil && isOwnCommand(requiredCmd) {
fatal(err) exit(err)
} }
mobycli.ExecIfDefaultCtxType(ctx) mobycli.ExecIfDefaultCtxType(ctx)
checkIfUnknownCommandExistInDefaultContext(err, currentContext) 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) { func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string) {
submatch := unknownCommandRegexp.FindSubmatch([]byte(err.Error())) submatch := unknownCommandRegexp.FindSubmatch([]byte(err.Error()))
if len(submatch) == 2 { if len(submatch) == 2 {

View File

@ -20,6 +20,12 @@ import (
"github.com/pkg/errors" "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 ( var (
// ErrNotFound is returned when an object is not found // ErrNotFound is returned when an object is not found
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
@ -31,6 +37,8 @@ var (
ErrUnknown = errors.New("unknown") ErrUnknown = errors.New("unknown")
// ErrLoginFailed is returned when login failed // ErrLoginFailed is returned when login failed
ErrLoginFailed = errors.New("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 // ErrNotImplemented is returned when a backend doesn't implement
// an action // an action
ErrNotImplemented = errors.New("not implemented") ErrNotImplemented = errors.New("not implemented")

View File

@ -18,11 +18,14 @@ package main
import ( import (
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"testing" "testing"
"time" "time"
"github.com/docker/api/errdefs"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -43,6 +46,14 @@ func (s *E2eSuite) TestContextHelp() {
Expect(output).To(ContainSubstring("--resource-group")) 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() { func (s *E2eSuite) TestListAndShowDefaultContext() {
output := s.NewDockerCommand("context", "show").ExecOrDie() output := s.NewDockerCommand("context", "show").ExecOrDie()
Expect(output).To(ContainSubstring("default")) Expect(output).To(ContainSubstring("default"))