Skip to content

Commit

Permalink
do exit 1 if a snap that is already installed is asked to be installe…
Browse files Browse the repository at this point in the history
…d again

LP: #1622782
  • Loading branch information
mvo5 committed Nov 17, 2016
1 parent c42d61c commit 6d28db7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
13 changes: 7 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,13 @@ func (e *Error) Error() string {
}

const (
ErrorKindTwoFactorRequired = "two-factor-required"
ErrorKindTwoFactorFailed = "two-factor-failed"
ErrorKindLoginRequired = "login-required"
ErrorKindTermsNotAccepted = "terms-not-accepted"
ErrorKindNoPaymentMethods = "no-payment-methods"
ErrorKindPaymentDeclined = "payment-declined"
ErrorKindTwoFactorRequired = "two-factor-required"
ErrorKindTwoFactorFailed = "two-factor-failed"
ErrorKindLoginRequired = "login-required"
ErrorKindTermsNotAccepted = "terms-not-accepted"
ErrorKindNoPaymentMethods = "no-payment-methods"
ErrorKindPaymentDeclined = "payment-declined"
ErrorKindSnapAlreadyInstalled = "snap-already-installed"
)

// IsTwoFactorError returns whether the given error is due to problems
Expand Down
4 changes: 4 additions & 0 deletions cmd/snap/cmd_snap_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ func (x *cmdInstall) installOne(name string, opts *client.SnapOptions) error {
} else {
changeID, err = cli.Install(name, opts)
}
if e, ok := err.(*client.Error); ok && e.Kind == client.ErrorKindSnapAlreadyInstalled {
fmt.Fprintf(Stderr, "snap is already installed")
return nil
}
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,16 @@ func postSnap(c *Command, r *http.Request, user *auth.UserState) Response {
}

msg, tsets, err := impl(&inst, state)
if _, ok := err.(*snap.AlreadyInstalledError); ok {
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindSnapAlreadyInstalled,
},
Status: http.StatusBadRequest,
}, nil)
}
if err != nil {
return BadRequest("cannot %s %q: %v", inst.Action, inst.Snaps[0], err)
}
Expand Down
15 changes: 8 additions & 7 deletions daemon/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ func (r *resp) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
type errorKind string

const (
errorKindTwoFactorRequired = errorKind("two-factor-required")
errorKindTwoFactorFailed = errorKind("two-factor-failed")
errorKindLoginRequired = errorKind("login-required")
errorKindInvalidAuthData = errorKind("invalid-auth-data")
errorKindTermsNotAccepted = errorKind("terms-not-accepted")
errorKindNoPaymentMethods = errorKind("no-payment-methods")
errorKindPaymentDeclined = errorKind("payment-declined")
errorKindTwoFactorRequired = errorKind("two-factor-required")
errorKindTwoFactorFailed = errorKind("two-factor-failed")
errorKindLoginRequired = errorKind("login-required")
errorKindInvalidAuthData = errorKind("invalid-auth-data")
errorKindTermsNotAccepted = errorKind("terms-not-accepted")
errorKindNoPaymentMethods = errorKind("no-payment-methods")
errorKindPaymentDeclined = errorKind("payment-declined")
errorKindSnapAlreadyInstalled = errorKind("snap-already-installed")
)

type errorValue interface{}
Expand Down
2 changes: 1 addition & 1 deletion overlord/snapstate/snapstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func Install(s *state.State, name, channel string, revision snap.Revision, userI
return nil, err
}
if snapst.HasCurrent() {
return nil, fmt.Errorf("snap %q already installed", name)
return nil, &snap.AlreadyInstalledError{name}
}

snapInfo, err := snapInfo(s, name, channel, revision, userID, flags)
Expand Down
8 changes: 8 additions & 0 deletions snap/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,11 @@ func SplitSnapApp(snapApp string) (snap, app string) {
}
return l[0], l[1]
}

type AlreadyInstalledError struct {
Snap string
}

func (e AlreadyInstalledError) Error() string {
return fmt.Sprintf("snap %q already installed", e.Snap)
}
8 changes: 3 additions & 5 deletions tests/main/install-errors/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ execute: |
echo "============================================"
echo "Install a snap already installed fails"
if snap install $SNAP_NAME; then
echo "Trying to install an already installed snap should fail"
exit 1
fi
echo "Install a snap that is already installed shows a message"
echo "but does not fail (LP: #1622782)"
snap install $SNAP_NAME | MATCH "snap is already installed"

0 comments on commit 6d28db7

Please sign in to comment.