2018-05-22 19:43:36 +00:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2017-04-12 18:12:25 +00:00
|
|
|
|
|
|
|
package cmdutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-08-01 00:42:59 +00:00
|
|
|
"strings"
|
2017-04-12 18:12:25 +00:00
|
|
|
|
Improve the overall cloud CLI experience
This improves the overall cloud CLI experience workflow.
Now whether a stack is local or cloud is inherent to the stack
itself. If you interact with a cloud stack, we transparently talk
to the cloud; if you interact with a local stack, we just do the
right thing, and perform all operations locally. Aside from sometimes
seeing a cloud emoji pop-up ☁️, the experience is quite similar.
For example, to initialize a new cloud stack, simply:
$ pulumi login
Logging into Pulumi Cloud: https://pulumi.com/
Enter Pulumi access token: <enter your token>
$ pulumi stack init my-cloud-stack
Note that you may log into a specific cloud if you'd like. For
now, this is just for our own testing purposes, but someday when we
support custom clouds (e.g., Enterprise), you can just say:
$ pulumi login --cloud-url https://corp.acme.my-ppc.net:9873
The cloud is now the default. If you instead prefer a "fire and
forget" style of stack, you can skip the login and pass `--local`:
$ pulumi stack init my-faf-stack --local
If you are logged in and run `pulumi`, we tell you as much:
$ pulumi
Usage:
pulumi [command]
// as before...
Currently logged into the Pulumi Cloud ☁️
https://pulumi.com/
And if you list your stacks, we tell you which one is local or not:
$ pulumi stack ls
NAME LAST UPDATE RESOURCE COUNT CLOUD URL
my-cloud-stack 2017-12-01 ... 3 https://pulumi.com/
my-faf-stack n/a 0 n/a
And `pulumi stack` by itself prints information like your cloud org,
PPC name, and so on, in addition to the usuals.
I shall write up more details and make sure to document these changes.
This change also fairly significantly refactors the layout of cloud
versus local logic, so that the cmd/ package is resonsible for CLI
things, and the new pkg/backend/ package is responsible for the
backends. The following is the overall resulting package architecture:
* The backend.Backend interface can be implemented to substitute
a new backend. This has operations to get and list stacks,
perform updates, and so on.
* The backend.Stack struct is a wrapper around a stack that has
or is being manipulated by a Backend. It resembles our existing
Stack notions in the engine, but carries additional metadata
about its source. Notably, it offers functions that allow
operations like updating and deleting on the Backend from which
it came.
* There is very little else in the pkg/backend/ package.
* A new package, pkg/backend/local/, encapsulates all local state
management for "fire and forget" scenarios. It simply implements
the above logic and contains anything specific to the local
experience.
* A peer package, pkg/backend/cloud/, encapsulates all logic
required for the cloud experience. This includes its subpackage
apitype/ which contains JSON schema descriptions required for
REST calls against the cloud backend. It also contains handy
functions to list which clouds we have authenticated with.
* A subpackage here, pkg/backend/state/, is not a provider at all.
Instead, it contains all of the state management functions that
are currently shared between local and cloud backends. This
includes configuration logic -- including encryption -- as well
as logic pertaining to which stacks are known to the workspace.
This addresses pulumi/pulumi#629 and pulumi/pulumi#494.
2017-12-02 15:29:46 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2017-10-21 02:26:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-04-12 18:12:25 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
|
2023-09-01 19:01:16 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
2021-03-17 13:20:05 +00:00
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/util/result"
|
2017-04-12 18:12:25 +00:00
|
|
|
)
|
|
|
|
|
2017-10-23 12:27:26 +00:00
|
|
|
// DetailedError extracts a detailed error message, including stack trace, if there is one.
|
|
|
|
func DetailedError(err error) string {
|
Improve the overall cloud CLI experience
This improves the overall cloud CLI experience workflow.
Now whether a stack is local or cloud is inherent to the stack
itself. If you interact with a cloud stack, we transparently talk
to the cloud; if you interact with a local stack, we just do the
right thing, and perform all operations locally. Aside from sometimes
seeing a cloud emoji pop-up ☁️, the experience is quite similar.
For example, to initialize a new cloud stack, simply:
$ pulumi login
Logging into Pulumi Cloud: https://pulumi.com/
Enter Pulumi access token: <enter your token>
$ pulumi stack init my-cloud-stack
Note that you may log into a specific cloud if you'd like. For
now, this is just for our own testing purposes, but someday when we
support custom clouds (e.g., Enterprise), you can just say:
$ pulumi login --cloud-url https://corp.acme.my-ppc.net:9873
The cloud is now the default. If you instead prefer a "fire and
forget" style of stack, you can skip the login and pass `--local`:
$ pulumi stack init my-faf-stack --local
If you are logged in and run `pulumi`, we tell you as much:
$ pulumi
Usage:
pulumi [command]
// as before...
Currently logged into the Pulumi Cloud ☁️
https://pulumi.com/
And if you list your stacks, we tell you which one is local or not:
$ pulumi stack ls
NAME LAST UPDATE RESOURCE COUNT CLOUD URL
my-cloud-stack 2017-12-01 ... 3 https://pulumi.com/
my-faf-stack n/a 0 n/a
And `pulumi stack` by itself prints information like your cloud org,
PPC name, and so on, in addition to the usuals.
I shall write up more details and make sure to document these changes.
This change also fairly significantly refactors the layout of cloud
versus local logic, so that the cmd/ package is resonsible for CLI
things, and the new pkg/backend/ package is responsible for the
backends. The following is the overall resulting package architecture:
* The backend.Backend interface can be implemented to substitute
a new backend. This has operations to get and list stacks,
perform updates, and so on.
* The backend.Stack struct is a wrapper around a stack that has
or is being manipulated by a Backend. It resembles our existing
Stack notions in the engine, but carries additional metadata
about its source. Notably, it offers functions that allow
operations like updating and deleting on the Backend from which
it came.
* There is very little else in the pkg/backend/ package.
* A new package, pkg/backend/local/, encapsulates all local state
management for "fire and forget" scenarios. It simply implements
the above logic and contains anything specific to the local
experience.
* A peer package, pkg/backend/cloud/, encapsulates all logic
required for the cloud experience. This includes its subpackage
apitype/ which contains JSON schema descriptions required for
REST calls against the cloud backend. It also contains handy
functions to list which clouds we have authenticated with.
* A subpackage here, pkg/backend/state/, is not a provider at all.
Instead, it contains all of the state management functions that
are currently shared between local and cloud backends. This
includes configuration logic -- including encryption -- as well
as logic pertaining to which stacks are known to the workspace.
This addresses pulumi/pulumi#629 and pulumi/pulumi#494.
2017-12-02 15:29:46 +00:00
|
|
|
msg := errorMessage(err)
|
2017-10-23 12:27:26 +00:00
|
|
|
hasstack := false
|
|
|
|
for {
|
|
|
|
if stackerr, ok := err.(interface {
|
|
|
|
StackTrace() errors.StackTrace
|
|
|
|
}); ok {
|
|
|
|
msg += "\n"
|
|
|
|
if hasstack {
|
|
|
|
msg += "CAUSED BY...\n"
|
|
|
|
}
|
|
|
|
hasstack = true
|
|
|
|
|
|
|
|
// Append the stack trace.
|
|
|
|
for _, f := range stackerr.StackTrace() {
|
|
|
|
msg += fmt.Sprintf("%+v\n", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep going up the causer chain, if any.
|
|
|
|
cause := errors.Cause(err)
|
|
|
|
if cause == err || cause == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = cause
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2018-05-04 18:26:53 +00:00
|
|
|
// runPostCommandHooks runs any post-hooks present on the given cobra.Command. This logic is copied directly from
|
|
|
|
// cobra itself; see https://github.com/spf13/cobra/blob/4dab30cb33e6633c33c787106bafbfbfdde7842d/command.go#L768-L785
|
|
|
|
// for the original.
|
|
|
|
func runPostCommandHooks(c *cobra.Command, args []string) error {
|
|
|
|
if c.PostRunE != nil {
|
|
|
|
if err := c.PostRunE(c, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if c.PostRun != nil {
|
|
|
|
c.PostRun(c, args)
|
|
|
|
}
|
|
|
|
for p := c; p != nil; p = p.Parent() {
|
|
|
|
if p.PersistentPostRunE != nil {
|
|
|
|
if err := p.PersistentPostRunE(c, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else if p.PersistentPostRun != nil {
|
|
|
|
p.PersistentPostRun(c, args)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 01:00:49 +00:00
|
|
|
// RunFunc wraps an error-returning run func with standard Pulumi error handling. All Pulumi
|
|
|
|
// commands should wrap themselves in this or [RunResultFunc] to ensure consistent and appropriate
|
|
|
|
// error behavior. In particular, we want to avoid any calls to os.Exit in the middle of a
|
|
|
|
// callstack which might prohibit reaping of child processes, resources, etc. And we wish to avoid
|
|
|
|
// the default Cobra unhandled error behavior, because it is formatted incorrectly and needlessly
|
|
|
|
// prints usage.
|
Support bailing from RunFunc (#13804)
**Background**
The result.Result type is used by our CLI implementation to communicate
how we want to exit the program.
Most `result.Result` values (built from errors with `result.FromError`)
cause the program to print the message to stderr and exit the program
with exit code -1.
The exception is `result.Bail()`, which indicates that we've already
printed the error message, and we simply need to `exit(-1)` now.
Our CLI command implementation use `cmdutil.RunResultFunc` which takes a
`func(...) result.Result` to implement this logic.
`cmdutil` additionally includes a `cmdutil.RunFunc` which takes a
`func(...) error` and wraps it in `RunResultFunc`, relying on
`result.FromError` for the conversion:
func RunFunc(run func(...) error) func(...) {
return RunResultFunc(func(...) result.Result {
if err := run(...); err != nil {
return result.FromError(err)
}
return nil
})
}
**Problem**
In CLI contexts where we're using an `error`, and we want to print an
error message to the user and exit, it's desirable to use diag.Sink to
print the message to the user with the appropriate level (error,
warning, etc.) and exit without printing anything else.
However, the only way to do that currently is by converting that
function to return `result.Result`, turn all error returns to
`result.FromError`, and then return `result.Bail()`.
**Solution**
This change introduces a `result.BailError` error that gets converted
into a `result.Bail()` when it passes through `result.FromError`.
It allows commands implementations that use `error` to continue
returning errors and still provide an ideal CLI experience.
It relies on `errors.As` for matching, so even if an intermediate layer
wraps the error with `fmt.Errorf("..: %w", ErrBail)`, we'll recognize
the request to bail.
BailError keep track of the internal error that triggered it, which
(when everything is moved off of result and onto error) means we'll
still be able to see the internal errors that triggered a bail during
debugging.
Currently debugging engine tests is pretty horrible because you often
just get back a `result.Result{err:nil}` with no information where in
the engine stack that came from.
**Testing**
Besides unit tests, this includes an end-to-end test for using
RunResultFunc with a bail error.
The test operates by putting the mock behavior in a fake test, and
re-running the test binary to execute *just that test*.
**Demonstration**
This change also ports the following commands to use BailError: cancel,
convert, env, policy rm, stack rm.
These command implementations are simple and were able to switch easily,
without bubbling into a change to a bunch of other code.
2023-08-29 07:43:40 +00:00
|
|
|
//
|
|
|
|
// If run returns a BailError, we will not print an error message, but will still be a non-zero exit code.
|
2017-04-12 18:12:25 +00:00
|
|
|
func RunFunc(run func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) {
|
2019-03-14 01:00:49 +00:00
|
|
|
return func(cmd *cobra.Command, args []string) {
|
Replace `result.Result` with native errors (#17044)
`result.Result` is a type that was introduced to enable us to
distinguish between *expected* errors (or *bails* in Pulumi parlance)
and *unexpected* errors or exceptions. Prior to Go 1.13, this made a lot
of sense, since there was no standard story on "wrapping" errors, and
thus no way to answer the question "did I end up here (in an error path)
because I meant to, or did I end up here because of a program bug?".
With the introduction of `%w`, `interface { Unwrap() []error }` and
company in Go 1.13, a simpler interface is available: one can use
`errors.Is` and `errors.As` to ask if an error at any point wraps an
error of a certain type. With this, we can simply ask "is there a bail
at any point in this error tree?" rather than having to track this
explicitly using a type such as `result.Result`. The `IsBail` function
was introduced a while ago to this end, but its rollout was not
completed and several uses of `result.Result` remained.
This commit completes the rollout of this simplified interface,
replacing all uses of `result.Result` with native Go errors that may or
may not wrap bails, and all uses of e.g. `res.IsBail` with
`IsBail(err)`. Doing so allows us to remove `result.Result` entirely.
2024-08-22 14:39:59 +00:00
|
|
|
if err := run(cmd, args); err != nil {
|
2018-05-04 18:26:53 +00:00
|
|
|
// Sadly, the fact that we hard-exit below means that it's up to us to replicate the Cobra post-run
|
|
|
|
// behavior here.
|
|
|
|
if postRunErr := runPostCommandHooks(cmd, args); postRunErr != nil {
|
Replace `result.Result` with native errors (#17044)
`result.Result` is a type that was introduced to enable us to
distinguish between *expected* errors (or *bails* in Pulumi parlance)
and *unexpected* errors or exceptions. Prior to Go 1.13, this made a lot
of sense, since there was no standard story on "wrapping" errors, and
thus no way to answer the question "did I end up here (in an error path)
because I meant to, or did I end up here because of a program bug?".
With the introduction of `%w`, `interface { Unwrap() []error }` and
company in Go 1.13, a simpler interface is available: one can use
`errors.Is` and `errors.As` to ask if an error at any point wraps an
error of a certain type. With this, we can simply ask "is there a bail
at any point in this error tree?" rather than having to track this
explicitly using a type such as `result.Result`. The `IsBail` function
was introduced a while ago to this end, but its rollout was not
completed and several uses of `result.Result` remained.
This commit completes the rollout of this simplified interface,
replacing all uses of `result.Result` with native Go errors that may or
may not wrap bails, and all uses of e.g. `res.IsBail` with
`IsBail(err)`. Doing so allows us to remove `result.Result` entirely.
2024-08-22 14:39:59 +00:00
|
|
|
err = result.MergeBails(err, postRunErr)
|
2019-03-14 01:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we were asked to bail, that means we already printed out a message. We just need
|
|
|
|
// to quit at this point (with an error code so no one thinks we succeeded). Bailing
|
|
|
|
// always indicates a failure, just one we don't need to print a message for.
|
Replace `result.Result` with native errors (#17044)
`result.Result` is a type that was introduced to enable us to
distinguish between *expected* errors (or *bails* in Pulumi parlance)
and *unexpected* errors or exceptions. Prior to Go 1.13, this made a lot
of sense, since there was no standard story on "wrapping" errors, and
thus no way to answer the question "did I end up here (in an error path)
because I meant to, or did I end up here because of a program bug?".
With the introduction of `%w`, `interface { Unwrap() []error }` and
company in Go 1.13, a simpler interface is available: one can use
`errors.Is` and `errors.As` to ask if an error at any point wraps an
error of a certain type. With this, we can simply ask "is there a bail
at any point in this error tree?" rather than having to track this
explicitly using a type such as `result.Result`. The `IsBail` function
was introduced a while ago to this end, but its rollout was not
completed and several uses of `result.Result` remained.
This commit completes the rollout of this simplified interface,
replacing all uses of `result.Result` with native Go errors that may or
may not wrap bails, and all uses of e.g. `res.IsBail` with
`IsBail(err)`. Doing so allows us to remove `result.Result` entirely.
2024-08-22 14:39:59 +00:00
|
|
|
if result.IsBail(err) {
|
2019-03-14 01:00:49 +00:00
|
|
|
os.Exit(-1)
|
|
|
|
return
|
2018-05-04 18:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:28:00 +00:00
|
|
|
// If there is a stack trace, and logging is enabled, append it. Otherwise, debug logging it.
|
2017-10-23 12:27:26 +00:00
|
|
|
var msg string
|
2018-05-15 22:28:00 +00:00
|
|
|
if logging.LogToStderr {
|
2017-10-23 12:27:26 +00:00
|
|
|
msg = DetailedError(err)
|
|
|
|
} else {
|
Improve the overall cloud CLI experience
This improves the overall cloud CLI experience workflow.
Now whether a stack is local or cloud is inherent to the stack
itself. If you interact with a cloud stack, we transparently talk
to the cloud; if you interact with a local stack, we just do the
right thing, and perform all operations locally. Aside from sometimes
seeing a cloud emoji pop-up ☁️, the experience is quite similar.
For example, to initialize a new cloud stack, simply:
$ pulumi login
Logging into Pulumi Cloud: https://pulumi.com/
Enter Pulumi access token: <enter your token>
$ pulumi stack init my-cloud-stack
Note that you may log into a specific cloud if you'd like. For
now, this is just for our own testing purposes, but someday when we
support custom clouds (e.g., Enterprise), you can just say:
$ pulumi login --cloud-url https://corp.acme.my-ppc.net:9873
The cloud is now the default. If you instead prefer a "fire and
forget" style of stack, you can skip the login and pass `--local`:
$ pulumi stack init my-faf-stack --local
If you are logged in and run `pulumi`, we tell you as much:
$ pulumi
Usage:
pulumi [command]
// as before...
Currently logged into the Pulumi Cloud ☁️
https://pulumi.com/
And if you list your stacks, we tell you which one is local or not:
$ pulumi stack ls
NAME LAST UPDATE RESOURCE COUNT CLOUD URL
my-cloud-stack 2017-12-01 ... 3 https://pulumi.com/
my-faf-stack n/a 0 n/a
And `pulumi stack` by itself prints information like your cloud org,
PPC name, and so on, in addition to the usuals.
I shall write up more details and make sure to document these changes.
This change also fairly significantly refactors the layout of cloud
versus local logic, so that the cmd/ package is resonsible for CLI
things, and the new pkg/backend/ package is responsible for the
backends. The following is the overall resulting package architecture:
* The backend.Backend interface can be implemented to substitute
a new backend. This has operations to get and list stacks,
perform updates, and so on.
* The backend.Stack struct is a wrapper around a stack that has
or is being manipulated by a Backend. It resembles our existing
Stack notions in the engine, but carries additional metadata
about its source. Notably, it offers functions that allow
operations like updating and deleting on the Backend from which
it came.
* There is very little else in the pkg/backend/ package.
* A new package, pkg/backend/local/, encapsulates all local state
management for "fire and forget" scenarios. It simply implements
the above logic and contains anything specific to the local
experience.
* A peer package, pkg/backend/cloud/, encapsulates all logic
required for the cloud experience. This includes its subpackage
apitype/ which contains JSON schema descriptions required for
REST calls against the cloud backend. It also contains handy
functions to list which clouds we have authenticated with.
* A subpackage here, pkg/backend/state/, is not a provider at all.
Instead, it contains all of the state management functions that
are currently shared between local and cloud backends. This
includes configuration logic -- including encryption -- as well
as logic pertaining to which stacks are known to the workspace.
This addresses pulumi/pulumi#629 and pulumi/pulumi#494.
2017-12-02 15:29:46 +00:00
|
|
|
msg = errorMessage(err)
|
2024-08-28 07:57:38 +00:00
|
|
|
logging.V(3).Info(DetailedError(err))
|
2017-10-21 02:26:18 +00:00
|
|
|
}
|
2018-05-04 18:26:53 +00:00
|
|
|
|
2017-10-21 02:26:18 +00:00
|
|
|
ExitError(msg)
|
2017-04-12 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Improve the overall cloud CLI experience
This improves the overall cloud CLI experience workflow.
Now whether a stack is local or cloud is inherent to the stack
itself. If you interact with a cloud stack, we transparently talk
to the cloud; if you interact with a local stack, we just do the
right thing, and perform all operations locally. Aside from sometimes
seeing a cloud emoji pop-up ☁️, the experience is quite similar.
For example, to initialize a new cloud stack, simply:
$ pulumi login
Logging into Pulumi Cloud: https://pulumi.com/
Enter Pulumi access token: <enter your token>
$ pulumi stack init my-cloud-stack
Note that you may log into a specific cloud if you'd like. For
now, this is just for our own testing purposes, but someday when we
support custom clouds (e.g., Enterprise), you can just say:
$ pulumi login --cloud-url https://corp.acme.my-ppc.net:9873
The cloud is now the default. If you instead prefer a "fire and
forget" style of stack, you can skip the login and pass `--local`:
$ pulumi stack init my-faf-stack --local
If you are logged in and run `pulumi`, we tell you as much:
$ pulumi
Usage:
pulumi [command]
// as before...
Currently logged into the Pulumi Cloud ☁️
https://pulumi.com/
And if you list your stacks, we tell you which one is local or not:
$ pulumi stack ls
NAME LAST UPDATE RESOURCE COUNT CLOUD URL
my-cloud-stack 2017-12-01 ... 3 https://pulumi.com/
my-faf-stack n/a 0 n/a
And `pulumi stack` by itself prints information like your cloud org,
PPC name, and so on, in addition to the usuals.
I shall write up more details and make sure to document these changes.
This change also fairly significantly refactors the layout of cloud
versus local logic, so that the cmd/ package is resonsible for CLI
things, and the new pkg/backend/ package is responsible for the
backends. The following is the overall resulting package architecture:
* The backend.Backend interface can be implemented to substitute
a new backend. This has operations to get and list stacks,
perform updates, and so on.
* The backend.Stack struct is a wrapper around a stack that has
or is being manipulated by a Backend. It resembles our existing
Stack notions in the engine, but carries additional metadata
about its source. Notably, it offers functions that allow
operations like updating and deleting on the Backend from which
it came.
* There is very little else in the pkg/backend/ package.
* A new package, pkg/backend/local/, encapsulates all local state
management for "fire and forget" scenarios. It simply implements
the above logic and contains anything specific to the local
experience.
* A peer package, pkg/backend/cloud/, encapsulates all logic
required for the cloud experience. This includes its subpackage
apitype/ which contains JSON schema descriptions required for
REST calls against the cloud backend. It also contains handy
functions to list which clouds we have authenticated with.
* A subpackage here, pkg/backend/state/, is not a provider at all.
Instead, it contains all of the state management functions that
are currently shared between local and cloud backends. This
includes configuration logic -- including encryption -- as well
as logic pertaining to which stacks are known to the workspace.
This addresses pulumi/pulumi#629 and pulumi/pulumi#494.
2017-12-02 15:29:46 +00:00
|
|
|
// Exit exits with a given error.
|
|
|
|
func Exit(err error) {
|
|
|
|
ExitError(errorMessage(err))
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:12:25 +00:00
|
|
|
// ExitError issues an error and exits with a standard error exit code.
|
2019-08-01 00:42:59 +00:00
|
|
|
func ExitError(msg string) {
|
|
|
|
// Escape percent sign before passing the message as a format string (e.g., msg could contain %PATH% on Windows).
|
2023-02-23 20:51:11 +00:00
|
|
|
format := strings.ReplaceAll(msg, "%", "%%")
|
2019-08-01 00:42:59 +00:00
|
|
|
exitErrorCodef(-1, format)
|
2017-04-12 18:12:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 00:42:59 +00:00
|
|
|
// exitErrorCodef formats the message with arguments, issues an error and exists with the given error exit code.
|
|
|
|
func exitErrorCodef(code int, format string, args ...interface{}) {
|
|
|
|
Diag().Errorf(diag.Message("", format), args...)
|
2017-04-12 18:12:25 +00:00
|
|
|
os.Exit(code)
|
|
|
|
}
|
Improve the overall cloud CLI experience
This improves the overall cloud CLI experience workflow.
Now whether a stack is local or cloud is inherent to the stack
itself. If you interact with a cloud stack, we transparently talk
to the cloud; if you interact with a local stack, we just do the
right thing, and perform all operations locally. Aside from sometimes
seeing a cloud emoji pop-up ☁️, the experience is quite similar.
For example, to initialize a new cloud stack, simply:
$ pulumi login
Logging into Pulumi Cloud: https://pulumi.com/
Enter Pulumi access token: <enter your token>
$ pulumi stack init my-cloud-stack
Note that you may log into a specific cloud if you'd like. For
now, this is just for our own testing purposes, but someday when we
support custom clouds (e.g., Enterprise), you can just say:
$ pulumi login --cloud-url https://corp.acme.my-ppc.net:9873
The cloud is now the default. If you instead prefer a "fire and
forget" style of stack, you can skip the login and pass `--local`:
$ pulumi stack init my-faf-stack --local
If you are logged in and run `pulumi`, we tell you as much:
$ pulumi
Usage:
pulumi [command]
// as before...
Currently logged into the Pulumi Cloud ☁️
https://pulumi.com/
And if you list your stacks, we tell you which one is local or not:
$ pulumi stack ls
NAME LAST UPDATE RESOURCE COUNT CLOUD URL
my-cloud-stack 2017-12-01 ... 3 https://pulumi.com/
my-faf-stack n/a 0 n/a
And `pulumi stack` by itself prints information like your cloud org,
PPC name, and so on, in addition to the usuals.
I shall write up more details and make sure to document these changes.
This change also fairly significantly refactors the layout of cloud
versus local logic, so that the cmd/ package is resonsible for CLI
things, and the new pkg/backend/ package is responsible for the
backends. The following is the overall resulting package architecture:
* The backend.Backend interface can be implemented to substitute
a new backend. This has operations to get and list stacks,
perform updates, and so on.
* The backend.Stack struct is a wrapper around a stack that has
or is being manipulated by a Backend. It resembles our existing
Stack notions in the engine, but carries additional metadata
about its source. Notably, it offers functions that allow
operations like updating and deleting on the Backend from which
it came.
* There is very little else in the pkg/backend/ package.
* A new package, pkg/backend/local/, encapsulates all local state
management for "fire and forget" scenarios. It simply implements
the above logic and contains anything specific to the local
experience.
* A peer package, pkg/backend/cloud/, encapsulates all logic
required for the cloud experience. This includes its subpackage
apitype/ which contains JSON schema descriptions required for
REST calls against the cloud backend. It also contains handy
functions to list which clouds we have authenticated with.
* A subpackage here, pkg/backend/state/, is not a provider at all.
Instead, it contains all of the state management functions that
are currently shared between local and cloud backends. This
includes configuration logic -- including encryption -- as well
as logic pertaining to which stacks are known to the workspace.
This addresses pulumi/pulumi#629 and pulumi/pulumi#494.
2017-12-02 15:29:46 +00:00
|
|
|
|
|
|
|
// errorMessage returns a message, possibly cleaning up the text if appropriate.
|
|
|
|
func errorMessage(err error) string {
|
2023-09-01 19:01:16 +00:00
|
|
|
contract.Requiref(err != nil, "err", "must not be nil")
|
|
|
|
|
Clean up multi-error rendering (#17039)
When an error occurs in Go, there is often a need to "wrap" the error
and propagate it upwards. For instance, if a "file not found" error
occurs while trying to read a `Pulumi.yaml`, we want to surface both
those pieces of information to the consumer of the error. Since 1.13, Go
provides support for natively wrapping errors using e.g. the `%w` format
specified in `fmt.Errorf` and the `Unwrap() []error` interface method.
Prior to 1.13. libraries such as `multierror` provided a similar
function in the absence of standard-library support.
Due to its age, the `pulumi/pulumi` codebase uses both Go-native error
wrapping and `multierror`. It would likely be good to converge on the
former and lose a dependency/remove extraneous code paths. One key
difference between the two approaches is in how multiple errors are
combined into a new parent. In `multierror`, the `Append` function takes
a set of errors and combines them, _flattening any existing trees of
errors_ so that the result is always a flat list of errors. In contrast,
Go's built-in `errors.Join` does not flatten and so can produce trees of
errors. Practically speaking, this is unlikely to be a huge problem for
this codebase, but there are some changes we can make proactively to
reduce the risks of trees-vs-lists if and when we migrate. Specifically,
when rendering errors, we don't consider the possibility of there being
a tree at present. This means that an input such as:
```go
errors.Join(
errors.Join(
errors.New("foo"),
errors.New("bar"),
),
errors.New("baz"),
)
```
results in something like:
```
1) 2 errors occurred:
1) foo
2) bar
2) baz
```
This commit fixes this so that errors are flattened at the point of
rendering, meaning that it shouldn't matter as we move from
`multierror.Append` to `errors.Join` whether or not an error chain is a
tree or a list.
2024-08-22 10:32:45 +00:00
|
|
|
underlying := flattenErrors(err)
|
2023-09-01 19:01:16 +00:00
|
|
|
|
|
|
|
switch len(underlying) {
|
|
|
|
case 0:
|
|
|
|
return err.Error()
|
|
|
|
|
|
|
|
case 1:
|
Clean up multi-error rendering (#17039)
When an error occurs in Go, there is often a need to "wrap" the error
and propagate it upwards. For instance, if a "file not found" error
occurs while trying to read a `Pulumi.yaml`, we want to surface both
those pieces of information to the consumer of the error. Since 1.13, Go
provides support for natively wrapping errors using e.g. the `%w` format
specified in `fmt.Errorf` and the `Unwrap() []error` interface method.
Prior to 1.13. libraries such as `multierror` provided a similar
function in the absence of standard-library support.
Due to its age, the `pulumi/pulumi` codebase uses both Go-native error
wrapping and `multierror`. It would likely be good to converge on the
former and lose a dependency/remove extraneous code paths. One key
difference between the two approaches is in how multiple errors are
combined into a new parent. In `multierror`, the `Append` function takes
a set of errors and combines them, _flattening any existing trees of
errors_ so that the result is always a flat list of errors. In contrast,
Go's built-in `errors.Join` does not flatten and so can produce trees of
errors. Practically speaking, this is unlikely to be a huge problem for
this codebase, but there are some changes we can make proactively to
reduce the risks of trees-vs-lists if and when we migrate. Specifically,
when rendering errors, we don't consider the possibility of there being
a tree at present. This means that an input such as:
```go
errors.Join(
errors.Join(
errors.New("foo"),
errors.New("bar"),
),
errors.New("baz"),
)
```
results in something like:
```
1) 2 errors occurred:
1) foo
2) bar
2) baz
```
This commit fixes this so that errors are flattened at the point of
rendering, meaning that it shouldn't matter as we move from
`multierror.Append` to `errors.Join` whether or not an error chain is a
tree or a list.
2024-08-22 10:32:45 +00:00
|
|
|
return underlying[0].Error()
|
2023-09-01 19:01:16 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
msg := fmt.Sprintf("%d errors occurred:", len(underlying))
|
|
|
|
for i, werr := range underlying {
|
2018-08-06 18:58:06 +00:00
|
|
|
msg += fmt.Sprintf("\n %d) %s", i+1, errorMessage(werr))
|
Improve the overall cloud CLI experience
This improves the overall cloud CLI experience workflow.
Now whether a stack is local or cloud is inherent to the stack
itself. If you interact with a cloud stack, we transparently talk
to the cloud; if you interact with a local stack, we just do the
right thing, and perform all operations locally. Aside from sometimes
seeing a cloud emoji pop-up ☁️, the experience is quite similar.
For example, to initialize a new cloud stack, simply:
$ pulumi login
Logging into Pulumi Cloud: https://pulumi.com/
Enter Pulumi access token: <enter your token>
$ pulumi stack init my-cloud-stack
Note that you may log into a specific cloud if you'd like. For
now, this is just for our own testing purposes, but someday when we
support custom clouds (e.g., Enterprise), you can just say:
$ pulumi login --cloud-url https://corp.acme.my-ppc.net:9873
The cloud is now the default. If you instead prefer a "fire and
forget" style of stack, you can skip the login and pass `--local`:
$ pulumi stack init my-faf-stack --local
If you are logged in and run `pulumi`, we tell you as much:
$ pulumi
Usage:
pulumi [command]
// as before...
Currently logged into the Pulumi Cloud ☁️
https://pulumi.com/
And if you list your stacks, we tell you which one is local or not:
$ pulumi stack ls
NAME LAST UPDATE RESOURCE COUNT CLOUD URL
my-cloud-stack 2017-12-01 ... 3 https://pulumi.com/
my-faf-stack n/a 0 n/a
And `pulumi stack` by itself prints information like your cloud org,
PPC name, and so on, in addition to the usuals.
I shall write up more details and make sure to document these changes.
This change also fairly significantly refactors the layout of cloud
versus local logic, so that the cmd/ package is resonsible for CLI
things, and the new pkg/backend/ package is responsible for the
backends. The following is the overall resulting package architecture:
* The backend.Backend interface can be implemented to substitute
a new backend. This has operations to get and list stacks,
perform updates, and so on.
* The backend.Stack struct is a wrapper around a stack that has
or is being manipulated by a Backend. It resembles our existing
Stack notions in the engine, but carries additional metadata
about its source. Notably, it offers functions that allow
operations like updating and deleting on the Backend from which
it came.
* There is very little else in the pkg/backend/ package.
* A new package, pkg/backend/local/, encapsulates all local state
management for "fire and forget" scenarios. It simply implements
the above logic and contains anything specific to the local
experience.
* A peer package, pkg/backend/cloud/, encapsulates all logic
required for the cloud experience. This includes its subpackage
apitype/ which contains JSON schema descriptions required for
REST calls against the cloud backend. It also contains handy
functions to list which clouds we have authenticated with.
* A subpackage here, pkg/backend/state/, is not a provider at all.
Instead, it contains all of the state management functions that
are currently shared between local and cloud backends. This
includes configuration logic -- including encryption -- as well
as logic pertaining to which stacks are known to the workspace.
This addresses pulumi/pulumi#629 and pulumi/pulumi#494.
2017-12-02 15:29:46 +00:00
|
|
|
}
|
2017-12-29 00:51:53 +00:00
|
|
|
return msg
|
Improve the overall cloud CLI experience
This improves the overall cloud CLI experience workflow.
Now whether a stack is local or cloud is inherent to the stack
itself. If you interact with a cloud stack, we transparently talk
to the cloud; if you interact with a local stack, we just do the
right thing, and perform all operations locally. Aside from sometimes
seeing a cloud emoji pop-up ☁️, the experience is quite similar.
For example, to initialize a new cloud stack, simply:
$ pulumi login
Logging into Pulumi Cloud: https://pulumi.com/
Enter Pulumi access token: <enter your token>
$ pulumi stack init my-cloud-stack
Note that you may log into a specific cloud if you'd like. For
now, this is just for our own testing purposes, but someday when we
support custom clouds (e.g., Enterprise), you can just say:
$ pulumi login --cloud-url https://corp.acme.my-ppc.net:9873
The cloud is now the default. If you instead prefer a "fire and
forget" style of stack, you can skip the login and pass `--local`:
$ pulumi stack init my-faf-stack --local
If you are logged in and run `pulumi`, we tell you as much:
$ pulumi
Usage:
pulumi [command]
// as before...
Currently logged into the Pulumi Cloud ☁️
https://pulumi.com/
And if you list your stacks, we tell you which one is local or not:
$ pulumi stack ls
NAME LAST UPDATE RESOURCE COUNT CLOUD URL
my-cloud-stack 2017-12-01 ... 3 https://pulumi.com/
my-faf-stack n/a 0 n/a
And `pulumi stack` by itself prints information like your cloud org,
PPC name, and so on, in addition to the usuals.
I shall write up more details and make sure to document these changes.
This change also fairly significantly refactors the layout of cloud
versus local logic, so that the cmd/ package is resonsible for CLI
things, and the new pkg/backend/ package is responsible for the
backends. The following is the overall resulting package architecture:
* The backend.Backend interface can be implemented to substitute
a new backend. This has operations to get and list stacks,
perform updates, and so on.
* The backend.Stack struct is a wrapper around a stack that has
or is being manipulated by a Backend. It resembles our existing
Stack notions in the engine, but carries additional metadata
about its source. Notably, it offers functions that allow
operations like updating and deleting on the Backend from which
it came.
* There is very little else in the pkg/backend/ package.
* A new package, pkg/backend/local/, encapsulates all local state
management for "fire and forget" scenarios. It simply implements
the above logic and contains anything specific to the local
experience.
* A peer package, pkg/backend/cloud/, encapsulates all logic
required for the cloud experience. This includes its subpackage
apitype/ which contains JSON schema descriptions required for
REST calls against the cloud backend. It also contains handy
functions to list which clouds we have authenticated with.
* A subpackage here, pkg/backend/state/, is not a provider at all.
Instead, it contains all of the state management functions that
are currently shared between local and cloud backends. This
includes configuration logic -- including encryption -- as well
as logic pertaining to which stacks are known to the workspace.
This addresses pulumi/pulumi#629 and pulumi/pulumi#494.
2017-12-02 15:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
Clean up multi-error rendering (#17039)
When an error occurs in Go, there is often a need to "wrap" the error
and propagate it upwards. For instance, if a "file not found" error
occurs while trying to read a `Pulumi.yaml`, we want to surface both
those pieces of information to the consumer of the error. Since 1.13, Go
provides support for natively wrapping errors using e.g. the `%w` format
specified in `fmt.Errorf` and the `Unwrap() []error` interface method.
Prior to 1.13. libraries such as `multierror` provided a similar
function in the absence of standard-library support.
Due to its age, the `pulumi/pulumi` codebase uses both Go-native error
wrapping and `multierror`. It would likely be good to converge on the
former and lose a dependency/remove extraneous code paths. One key
difference between the two approaches is in how multiple errors are
combined into a new parent. In `multierror`, the `Append` function takes
a set of errors and combines them, _flattening any existing trees of
errors_ so that the result is always a flat list of errors. In contrast,
Go's built-in `errors.Join` does not flatten and so can produce trees of
errors. Practically speaking, this is unlikely to be a huge problem for
this codebase, but there are some changes we can make proactively to
reduce the risks of trees-vs-lists if and when we migrate. Specifically,
when rendering errors, we don't consider the possibility of there being
a tree at present. This means that an input such as:
```go
errors.Join(
errors.Join(
errors.New("foo"),
errors.New("bar"),
),
errors.New("baz"),
)
```
results in something like:
```
1) 2 errors occurred:
1) foo
2) bar
2) baz
```
This commit fixes this so that errors are flattened at the point of
rendering, meaning that it shouldn't matter as we move from
`multierror.Append` to `errors.Join` whether or not an error chain is a
tree or a list.
2024-08-22 10:32:45 +00:00
|
|
|
|
|
|
|
// Flattens an error into a slice of errors containing the supplied error and
|
|
|
|
// all errors it wraps. If the set of wrapped errors is a tree (as e.g. produced
|
|
|
|
// by errors.Join), the errors are flattened in a depth-first manner. This
|
|
|
|
// function supports both native wrapped errors and those produced by the
|
|
|
|
// multierror package.
|
|
|
|
func flattenErrors(err error) []error {
|
|
|
|
var errs []error
|
|
|
|
switch multi := err.(type) {
|
|
|
|
case *multierror.Error:
|
|
|
|
for _, e := range multi.Errors {
|
|
|
|
errs = append(errs, flattenErrors(e)...)
|
|
|
|
}
|
|
|
|
case interface{ Unwrap() []error }:
|
|
|
|
for _, e := range multi.Unwrap() {
|
|
|
|
errs = append(errs, flattenErrors(e)...)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|