Skip to content

Commit

Permalink
properly propagate "already installed" error; fixes #166
Browse files Browse the repository at this point in the history
Signed-off-by: Denys Smirnov <[email protected]>
  • Loading branch information
Denys Smirnov authored and dennwc committed Oct 3, 2018
1 parent 9b91d41 commit 8e3fa88
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
27 changes: 21 additions & 6 deletions cmd/bblfshctl/cmd/driver_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"sync"
"time"

"github.com/bblfsh/bblfshd/daemon"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/bblfsh/bblfshd/daemon/protocol"
"gopkg.in/bblfsh/sdk.v2/driver/manifest/discovery"

Expand Down Expand Up @@ -105,6 +109,7 @@ type DriverInstallCommand struct {
Update bool `long:"update" description:"replace the current image for the language if any"`
All bool `long:"all" description:"installs all the official drivers"`
Recommended bool `long:"recommended" description:"install the recommended official drivers"`
Force bool `short:"f" long:"force" description:"ignore already installed errors"`

DriverCommand
}
Expand All @@ -123,7 +128,7 @@ func (c *DriverInstallCommand) Execute(args []string) error {
// TODO: go-flags does not support optional arguments in first positions
c.Args.Language, c.Args.ImageReference = "", c.Args.Language
}
return c.installSingleDriver(driverRef{Lang: c.Args.Language, Ref: c.Args.ImageReference})
return c.installDrivers([]driverRef{{Lang: c.Args.Language, Ref: c.Args.ImageReference}})
}

var (
Expand Down Expand Up @@ -211,9 +216,13 @@ func (c *DriverInstallCommand) installDrivers(refs []driverRef) error {
todo--
str := " + "
if r.Err != nil {
last = r.Err
errs = append(errs, r.Err)
str = "ERR"
if daemon.ErrAlreadyInstalled.Is(r.Err) && c.Force {
done++
} else {
last = r.Err
errs = append(errs, r.Err)
str = "ERR"
}
} else {
done++
}
Expand Down Expand Up @@ -288,7 +297,9 @@ func (c *DriverInstallCommand) installDriver(ctx context.Context, ref driverRef)
ImageReference: ref.Ref,
Update: c.Update,
})
if err == nil && len(r.Errors) == 0 {
if st, ok := status.FromError(err); ok && st.Code() == codes.AlreadyExists {
return daemon.ErrAlreadyInstalled.New(ref.Lang, ref.Ref)
} else if err == nil && len(r.Errors) == 0 {
return nil
}
if err == nil {
Expand All @@ -302,7 +313,7 @@ func (c *DriverInstallCommand) installSingleDriver(ref driverRef) error {
if ref.Lang != "" {
ltext = fmt.Sprintf("%s language ", ref.Lang)
}
fmt.Printf("Installing %sdriver from %q... ", ltext, ref)
fmt.Printf("Installing %sdriver from %q... ", ltext, ref.Ref)
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) // Build our new spinner
s.Start()

Expand All @@ -313,6 +324,10 @@ func (c *DriverInstallCommand) installSingleDriver(ref driverRef) error {
fmt.Println("Done")
return nil
}
if daemon.ErrAlreadyInstalled.Is(err) && c.Force {
fmt.Fprintf(os.Stderr, "Warning: %s\n", err)
return nil
}
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
ErrUnexpected = errors.NewKind("unexpected error")
ErrMissingDriver = errors.NewKind("missing driver for language %q")
ErrRuntime = errors.NewKind("runtime failure")
ErrAlreadyInstalled = errors.NewKind("driver already installed: %s (image reference: %s)")
ErrAlreadyInstalled = protocol.ErrAlreadyInstalled
ErrLanguageDetection = errors.NewKind("could not autodetect language")
)

Expand Down
11 changes: 10 additions & 1 deletion daemon/protocol/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import (

xcontext "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gopkg.in/src-d/go-errors.v1"

"gopkg.in/bblfsh/sdk.v1/protocol"
)

var (
ErrAlreadyInstalled = errors.NewKind("driver already installed: %s (image reference: %s)")
)

type Service interface {
InstallDriver(language string, image string, update bool) error
RemoveDriver(language string) error
Expand Down Expand Up @@ -112,7 +119,9 @@ func (s *protocolServiceServer) InstallDriver(ctx xcontext.Context, req *Install
req.Update,
)

if err != nil {
if ErrAlreadyInstalled.Is(err) {
return nil, status.New(codes.AlreadyExists, err.Error()).Err()
} else if err != nil {
return nil, err
}
return resp, nil
Expand Down

0 comments on commit 8e3fa88

Please sign in to comment.