2018-05-22 21:43:36 +02: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-10-19 00:37:18 +02:00
|
|
|
|
2017-10-03 01:37:12 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-05-04 21:59:48 +02:00
|
|
|
"bytes"
|
2018-04-20 03:59:14 +02:00
|
|
|
"context"
|
2017-10-05 23:08:46 +02:00
|
|
|
"fmt"
|
2017-10-03 01:37:12 +02:00
|
|
|
"os"
|
2018-05-04 21:59:48 +02:00
|
|
|
"os/exec"
|
2018-04-20 03:59:14 +02:00
|
|
|
"os/signal"
|
2017-10-25 19:20:08 +02:00
|
|
|
"path/filepath"
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
"sort"
|
2017-10-25 19:20:08 +02:00
|
|
|
|
2018-05-08 03:23:03 +02:00
|
|
|
"github.com/opentracing/opentracing-go"
|
2017-10-08 16:18:53 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-05-01 00:27:53 +02:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2018-04-11 19:08:32 +02:00
|
|
|
survey "gopkg.in/AlecAivazis/survey.v1"
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
surveycore "gopkg.in/AlecAivazis/survey.v1/core"
|
2017-10-08 16:18:53 +02: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 16:29:46 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/backend"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend/cloud"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend/local"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend/state"
|
2018-05-08 19:37:42 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/diag"
|
2017-12-18 20:42:32 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/diag/colors"
|
2018-04-20 03:59:14 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/engine"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/cancel"
|
2017-12-13 19:46:54 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/cmdutil"
|
2017-10-05 23:08:46 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/contract"
|
2018-05-22 01:17:12 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/gitutil"
|
2018-05-16 00:28:00 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/logging"
|
2018-01-08 22:01:40 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
2017-10-03 01:37:12 +02:00
|
|
|
)
|
|
|
|
|
2018-05-23 02:02:45 +02:00
|
|
|
func hasDebugCommands() bool {
|
|
|
|
return cmdutil.IsTruthy(os.Getenv("PULUMI_DEBUG_COMMANDS"))
|
|
|
|
}
|
|
|
|
|
2018-04-05 00:31:01 +02:00
|
|
|
func currentBackend() (backend.Backend, error) {
|
|
|
|
creds, err := workspace.GetStoredCredentials()
|
2017-10-03 01:37:12 +02:00
|
|
|
if err != nil {
|
2018-04-05 00:31:01 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if local.IsLocalBackendURL(creds.Current) {
|
Remove the need to `pulumi init` for the local backend
This change removes the need to `pulumi init` when targeting the local
backend. A fair amount of the change lays the foundation that the next
set of changes to stop having `pulumi init` be used for cloud stacks
as well.
Previously, `pulumi init` logically did two things:
1. It created the bookkeeping directory for local stacks, this was
stored in `<repository-root>/.pulumi`, where `<repository-root>` was
the path to what we belived the "root" of your project was. In the
case of git repositories, this was the directory that contained your
`.git` folder.
2. It recorded repository information in
`<repository-root>/.pulumi/repository.json`. This was used by the
cloud backend when computing what project to interact with on
Pulumi.com
The new identity model will remove the need for (2), since we only
need an owner and stack name to fully qualify a stack on
pulumi.com, so it's easy enough to stop creating a folder just for
that.
However, for the local backend, we need to continue to retain some
information about stacks (e.g. checkpoints, history, etc). In
addition, we need to store our workspace settings (which today just
contains the selected stack) somehere.
For state stored by the local backend, we change the URL scheme from
`local://` to `local://<optional-root-path>`. When
`<optional-root-path>` is unset, it defaults to `$HOME`. We create our
`.pulumi` folder in that directory. This is important because stack
names now must be unique within the backend, but we have some tests
using local stacks which use fixed stack names, so each integration
test really wants its own "view" of the world.
For the workspace settings, we introduce a new `workspaces` directory
in `~/.pulumi`. In this folder we write the workspace settings file
for each project. The file name is the name of the project, combined
with the SHA1 of the path of the project file on disk, to ensure that
multiple pulumi programs with the same project name have different
workspace settings.
This does mean that moving a project's location on disk will cause the
CLI to "forget" what the selected stack was, which is unfortunate, but
not the end of the world. If this ends up being a big pain point, we
can certianly try to play games in the future (for example, if we saw
a .git folder in a parent folder, we could store data in there).
With respect to compatibility, we don't attempt to migrate older files
to their newer locations. For long lived stacks managed using the
local backend, we can provide information on where to move things
to. For all stacks (regardless of backend) we'll require the user to
`pulumi stack select` their stack again, but that seems like the
correct trade-off vs writing complicated upgrade code.
2018-04-17 01:15:10 +02:00
|
|
|
return local.New(cmdutil.Diag(), creds.Current), nil
|
2017-10-03 01:37:12 +02:00
|
|
|
}
|
2018-05-08 03:23:03 +02:00
|
|
|
return cloud.Login(commandContext(), cmdutil.Diag(), creds.Current)
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:44:44 +02:00
|
|
|
// This is used to control the contents of the tracing header.
|
|
|
|
var tracingHeader = os.Getenv("PULUMI_TRACING_HEADER")
|
|
|
|
|
2018-05-08 03:23:03 +02:00
|
|
|
func commandContext() context.Context {
|
|
|
|
ctx := context.Background()
|
2018-05-07 20:49:15 +02:00
|
|
|
if cmdutil.IsTracingEnabled() {
|
|
|
|
if cmdutil.TracingRootSpan != nil {
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, cmdutil.TracingRootSpan)
|
|
|
|
}
|
2018-05-14 22:44:44 +02:00
|
|
|
|
|
|
|
tracingOptions := backend.TracingOptions{
|
|
|
|
PropagateSpans: true,
|
|
|
|
TracingHeader: tracingHeader,
|
|
|
|
}
|
|
|
|
ctx = backend.ContextWithTracingOptions(ctx, tracingOptions)
|
2018-05-08 03:23:03 +02:00
|
|
|
}
|
|
|
|
return ctx
|
2018-04-05 00:31:01 +02:00
|
|
|
}
|
|
|
|
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
// createStack creates a stack with the given name, and selects it as the current.
|
2018-04-18 01:37:52 +02:00
|
|
|
func createStack(b backend.Backend, stackRef backend.StackReference, opts interface{}) (backend.Stack, error) {
|
2018-05-08 03:23:03 +02:00
|
|
|
stack, err := b.CreateStack(commandContext(), stackRef, opts)
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
if err != nil {
|
2018-05-08 00:31:27 +02:00
|
|
|
// If it's a StackAlreadyExistsError, don't wrap it.
|
|
|
|
if _, ok := err.(*backend.StackAlreadyExistsError); ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
return nil, errors.Wrapf(err, "could not create stack")
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:19:13 +02:00
|
|
|
if err = state.SetCurrentStack(stack.Name().String()); err != nil {
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// requireStack will require that a stack exists. If stackName is blank, the currently selected stack from
|
|
|
|
// the workspace is returned. If no stack with either the given name, or a currently selected stack, exists,
|
|
|
|
// and we are in an interactive terminal, the user will be prompted to create a new stack.
|
2018-04-18 01:37:52 +02:00
|
|
|
func requireStack(stackName string, offerNew bool) (backend.Stack, error) {
|
2017-11-01 22:55:16 +01:00
|
|
|
if stackName == "" {
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
return requireCurrentStack(offerNew)
|
2017-11-01 22:55:16 +01:00
|
|
|
}
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
|
2018-04-05 00:31:01 +02:00
|
|
|
b, err := currentBackend()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-18 01:37:52 +02:00
|
|
|
|
2018-04-20 08:16:07 +02:00
|
|
|
stackRef, err := b.ParseStackReference(stackName)
|
2018-04-18 01:37:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-08 03:23:03 +02:00
|
|
|
stack, err := b.GetStack(commandContext(), stackRef)
|
2017-10-17 01:01:34 +02:00
|
|
|
if err != nil {
|
2017-10-17 21:28:03 +02:00
|
|
|
return nil, err
|
2017-10-17 01:01:34 +02:00
|
|
|
}
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
if stack != nil {
|
|
|
|
return stack, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// No stack was found. If we're in a terminal, prompt to create one.
|
2018-04-14 19:01:39 +02:00
|
|
|
if offerNew && cmdutil.Interactive() {
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
fmt.Printf("The stack '%s' does not exist.\n", stackName)
|
|
|
|
fmt.Printf("\n")
|
2018-04-05 00:31:01 +02:00
|
|
|
_, err = cmdutil.ReadConsole("If you would like to create this stack now, please press <ENTER>, otherwise " +
|
|
|
|
"press ^C")
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:37:52 +02:00
|
|
|
return createStack(b, stackRef, nil)
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 19:01:39 +02:00
|
|
|
return nil, errors.Errorf("no stack named '%s' found", stackName)
|
2017-10-19 00:37:18 +02:00
|
|
|
}
|
|
|
|
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
func requireCurrentStack(offerNew bool) (backend.Stack, error) {
|
|
|
|
// Search for the current stack.
|
2018-04-05 00:31:01 +02:00
|
|
|
b, err := currentBackend()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-08 03:23:03 +02:00
|
|
|
stack, err := state.CurrentStack(commandContext(), b)
|
2017-10-19 00:37:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
} else if stack != nil {
|
|
|
|
return stack, nil
|
2017-10-19 00:37:18 +02:00
|
|
|
}
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
|
|
|
|
// If no current stack exists, and we are interactive, prompt to select or create one.
|
2018-04-05 00:31:01 +02:00
|
|
|
return chooseStack(b, offerNew)
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// chooseStack will prompt the user to choose amongst the full set of stacks in the given backends. If offerNew is
|
|
|
|
// true, then the option to create an entirely new stack is provided and will create one as desired.
|
2018-04-05 00:31:01 +02:00
|
|
|
func chooseStack(b backend.Backend, offerNew bool) (backend.Stack, error) {
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
// Prepare our error in case we need to issue it. Bail early if we're not interactive.
|
|
|
|
var chooseStackErr string
|
|
|
|
if offerNew {
|
|
|
|
chooseStackErr = "no stack selected; please use `pulumi stack select` or `pulumi stack init` to choose one"
|
|
|
|
} else {
|
|
|
|
chooseStackErr = "no stack selected; please use `pulumi stack select` to choose one"
|
|
|
|
}
|
|
|
|
if !cmdutil.Interactive() {
|
|
|
|
return nil, errors.New(chooseStackErr)
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:19:13 +02:00
|
|
|
proj, err := workspace.DetectProject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
// First create a list and map of stack names.
|
|
|
|
var options []string
|
|
|
|
stacks := make(map[string]backend.Stack)
|
2018-05-08 03:23:03 +02:00
|
|
|
allStacks, err := b.ListStacks(commandContext(), &proj.Name)
|
2018-04-05 00:31:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not query backend for stacks")
|
|
|
|
}
|
|
|
|
for _, stack := range allStacks {
|
2018-04-18 01:37:52 +02:00
|
|
|
name := stack.Name().String()
|
2018-04-05 00:31:01 +02:00
|
|
|
options = append(options, name)
|
|
|
|
stacks[name] = stack
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
}
|
|
|
|
sort.Strings(options)
|
|
|
|
|
|
|
|
// If we are offering to create a new stack, add that to the end of the list.
|
|
|
|
newOption := "<create a new stack>"
|
|
|
|
if offerNew {
|
|
|
|
options = append(options, newOption)
|
|
|
|
} else if len(options) == 0 {
|
|
|
|
// If no options are available, we can't offer a choice!
|
2018-04-14 19:01:39 +02:00
|
|
|
return nil, errors.New("this command requires a stack, but there are none")
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a stack is already selected, make that the default.
|
|
|
|
var current string
|
2018-05-08 03:23:03 +02:00
|
|
|
currStack, currErr := state.CurrentStack(commandContext(), b)
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
contract.IgnoreError(currErr)
|
|
|
|
if currStack != nil {
|
2018-04-18 01:37:52 +02:00
|
|
|
current = currStack.Name().String()
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Customize the prompt a little bit (and disable color since it doesn't match our scheme).
|
|
|
|
surveycore.DisableColor = true
|
|
|
|
surveycore.QuestionIcon = ""
|
|
|
|
surveycore.SelectFocusIcon = colors.ColorizeText(colors.BrightGreen + ">" + colors.Reset)
|
|
|
|
message := "\rPlease choose a stack"
|
|
|
|
if offerNew {
|
|
|
|
message += ", or create a new one:"
|
|
|
|
} else {
|
|
|
|
message += ":"
|
|
|
|
}
|
|
|
|
message = colors.ColorizeText(colors.BrightWhite + message + colors.Reset)
|
|
|
|
|
|
|
|
var option string
|
|
|
|
if err := survey.AskOne(&survey.Select{
|
|
|
|
Message: message,
|
|
|
|
Options: options,
|
|
|
|
Default: current,
|
|
|
|
}, &option, nil); err != nil {
|
|
|
|
return nil, errors.New(chooseStackErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if option == newOption {
|
|
|
|
stackName, err := cmdutil.ReadConsole("Please enter your desired stack name")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-20 08:16:07 +02:00
|
|
|
stackRef, err := b.ParseStackReference(stackName)
|
2018-04-18 01:37:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return createStack(b, stackRef, nil)
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return stacks[option], nil
|
2017-10-19 00:37:18 +02:00
|
|
|
}
|
2017-10-25 19:20:08 +02:00
|
|
|
|
2018-02-14 22:56:16 +01:00
|
|
|
// readProject attempts to detect and read the project for the current workspace. If an error occurs, it will be
|
|
|
|
// printed to Stderr, and the returned value will be nil. If the project is successfully detected and read, it
|
|
|
|
// is returned along with the path to its containing directory, which will be used as the root of the project's
|
2018-01-08 22:01:40 +01:00
|
|
|
// Pulumi program.
|
2018-02-14 22:56:16 +01:00
|
|
|
func readProject() (*workspace.Project, string, error) {
|
2018-01-08 22:01:40 +01:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we got here, we have a path, so we will try to load it.
|
2018-02-14 22:56:16 +01:00
|
|
|
path, err := workspace.DetectProjectPathFrom(pwd)
|
2018-01-08 22:01:40 +01:00
|
|
|
if err != nil {
|
2018-02-14 22:56:16 +01:00
|
|
|
return nil, "", errors.Wrapf(err,
|
|
|
|
"could not locate Pulumi.yaml project file (searching upwards from %s)", pwd)
|
|
|
|
} else if path == "" {
|
|
|
|
return nil, "", errors.Errorf(
|
|
|
|
"no Pulumi.yaml project file found (searching upwards from %s)", pwd)
|
2018-01-08 22:01:40 +01:00
|
|
|
}
|
2018-02-14 22:56:16 +01:00
|
|
|
proj, err := workspace.LoadProject(path)
|
2018-01-08 22:01:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2018-02-14 22:56:16 +01:00
|
|
|
return proj, filepath.Dir(path), nil
|
2018-01-08 22:01:40 +01:00
|
|
|
}
|
2018-01-18 20:10:15 +01:00
|
|
|
|
|
|
|
type colorFlag struct {
|
|
|
|
value colors.Colorization
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *colorFlag) String() string {
|
|
|
|
return string(cf.Colorization())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *colorFlag) Set(value string) error {
|
|
|
|
switch value {
|
|
|
|
case "always":
|
|
|
|
cf.value = colors.Always
|
|
|
|
case "never":
|
|
|
|
cf.value = colors.Never
|
|
|
|
case "raw":
|
|
|
|
cf.value = colors.Raw
|
|
|
|
// Backwards compat for old flag values.
|
|
|
|
case "auto":
|
|
|
|
cf.value = colors.Always
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported color option: '%s'. Supported values are: always, never, raw", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *colorFlag) Type() string {
|
|
|
|
return "colors.Colorization"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *colorFlag) Colorization() colors.Colorization {
|
|
|
|
if cf.value == "" {
|
|
|
|
return colors.Always
|
|
|
|
}
|
|
|
|
|
|
|
|
return cf.value
|
|
|
|
}
|
2018-01-25 03:22:41 +01:00
|
|
|
|
2018-05-04 21:59:48 +02:00
|
|
|
// anyWriter is an io.Writer that will set itself to `true` iff any call to `anyWriter.Write` is made with a
|
|
|
|
// non-zero-length slice. This can be used to determine whether or not any data was ever written to the writer.
|
|
|
|
type anyWriter bool
|
|
|
|
|
|
|
|
func (w *anyWriter) Write(d []byte) (int, error) {
|
|
|
|
if len(d) > 0 {
|
|
|
|
*w = true
|
|
|
|
}
|
|
|
|
return len(d), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isGitWorkTreeDirty returns true if the work tree for the current directory's repository is dirty.
|
|
|
|
func isGitWorkTreeDirty() (bool, error) {
|
|
|
|
gitBin, err := exec.LookPath("git")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: gas
|
|
|
|
gitStatusCmd := exec.Command(gitBin, "status", "--porcelain", "-z")
|
|
|
|
var anyOutput anyWriter
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
gitStatusCmd.Stdout = &anyOutput
|
|
|
|
gitStatusCmd.Stderr = &stderr
|
|
|
|
if err = gitStatusCmd.Run(); err != nil {
|
|
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
|
|
|
ee.Stderr = stderr.Bytes()
|
|
|
|
}
|
|
|
|
return false, errors.Wrapf(err, "'git status' failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bool(anyOutput), nil
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:22:41 +01:00
|
|
|
// getUpdateMetadata returns an UpdateMetadata object, with optional data about the environment
|
|
|
|
// performing the update.
|
|
|
|
func getUpdateMetadata(msg, root string) (backend.UpdateMetadata, error) {
|
|
|
|
m := backend.UpdateMetadata{
|
|
|
|
Message: msg,
|
|
|
|
Environment: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather git-related data as appropriate. (Returns nil, nil if no repo found.)
|
2018-05-22 01:17:12 +02:00
|
|
|
repo, err := gitutil.GetGitRepository(root)
|
2018-01-25 03:22:41 +01:00
|
|
|
if err != nil {
|
2018-05-08 19:37:42 +02:00
|
|
|
cmdutil.Diag().Warningf(diag.Message("", "could not detect Git repository: %v"), err)
|
2018-05-04 21:59:48 +02:00
|
|
|
}
|
|
|
|
if repo == nil {
|
2018-05-16 00:28:00 +02:00
|
|
|
logging.Infof("no git repository found")
|
2018-05-04 21:59:48 +02:00
|
|
|
return m, nil
|
|
|
|
}
|
2018-01-25 03:22:41 +01:00
|
|
|
|
2018-05-04 21:59:48 +02:00
|
|
|
// GitHub repo slug if applicable. We don't require GitHub, so swallow errors.
|
2018-05-22 01:17:12 +02:00
|
|
|
ghLogin, ghRepo, err := gitutil.GetGitHubProjectForOriginByRepo(repo)
|
2018-05-04 21:59:48 +02:00
|
|
|
if err != nil {
|
2018-05-08 19:37:42 +02:00
|
|
|
cmdutil.Diag().Warningf(diag.Message("", "could not detect GitHub project information: %v"), err)
|
2018-05-04 21:59:48 +02:00
|
|
|
} else {
|
|
|
|
m.Environment[backend.GitHubLogin] = ghLogin
|
|
|
|
m.Environment[backend.GitHubRepo] = ghRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit at HEAD
|
|
|
|
head, err := repo.Head()
|
|
|
|
if err != nil {
|
2018-05-08 19:37:42 +02:00
|
|
|
cmdutil.Diag().Warningf(diag.Message("", "could not fetch Git repository HEAD info: %v"), err)
|
2018-05-04 21:59:48 +02:00
|
|
|
} else {
|
2018-05-08 18:58:33 +02:00
|
|
|
hash := head.Hash()
|
|
|
|
m.Environment[backend.GitHead] = hash.String()
|
|
|
|
commit, commitErr := repo.CommitObject(hash)
|
|
|
|
if commitErr != nil {
|
2018-05-08 19:37:42 +02:00
|
|
|
cmdutil.Diag().Warningf(
|
|
|
|
diag.Message("", "could not fetch Git repository HEAD commit info: %v"), commitErr)
|
2018-05-08 18:58:33 +02:00
|
|
|
} else {
|
|
|
|
m.Environment[backend.GitCommitter] = commit.Committer.Name
|
|
|
|
m.Environment[backend.GitCommitterEmail] = commit.Committer.Email
|
|
|
|
m.Environment[backend.GitAuthor] = commit.Author.Name
|
|
|
|
m.Environment[backend.GitAuthorEmail] = commit.Author.Email
|
|
|
|
}
|
2018-05-04 21:59:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isDirty, err := isGitWorkTreeDirty()
|
|
|
|
if err != nil {
|
2018-05-08 19:37:42 +02:00
|
|
|
cmdutil.Diag().Warningf(diag.Message("", "could not Git repository dirty worktree info: %v"), err)
|
2018-05-04 21:59:48 +02:00
|
|
|
} else {
|
|
|
|
m.Environment[backend.GitDirty] = fmt.Sprint(isDirty)
|
2018-01-25 03:22:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
2018-04-05 00:31:01 +02:00
|
|
|
|
2018-04-20 03:59:14 +02:00
|
|
|
type cancellationScope struct {
|
|
|
|
context *cancel.Context
|
|
|
|
sigint chan os.Signal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cancellationScope) Context() *cancel.Context {
|
|
|
|
return s.context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cancellationScope) Close() {
|
|
|
|
signal.Stop(s.sigint)
|
|
|
|
close(s.sigint)
|
|
|
|
}
|
|
|
|
|
|
|
|
type cancellationScopeSource int
|
|
|
|
|
|
|
|
var cancellationScopes = backend.CancellationScopeSource(cancellationScopeSource(0))
|
|
|
|
|
|
|
|
func (cancellationScopeSource) NewScope(events chan<- engine.Event, isPreview bool) backend.CancellationScope {
|
|
|
|
cancelContext, cancelSource := cancel.NewContext(context.Background())
|
|
|
|
|
|
|
|
c := &cancellationScope{
|
|
|
|
context: cancelContext,
|
|
|
|
sigint: make(chan os.Signal),
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for range c.sigint {
|
|
|
|
// If we haven't yet received a SIGINT, call the cancellation func. Otherwise call the termination
|
|
|
|
// func.
|
|
|
|
if cancelContext.CancelErr() == nil {
|
|
|
|
message := "^C received; cancelling. If you would like to terminate immediately, press ^C again.\n"
|
|
|
|
if !isPreview {
|
|
|
|
message += colors.BrightRed + "Note that terminating immediately may lead to orphaned resources " +
|
|
|
|
"and other inconsistencies.\n" + colors.Reset
|
|
|
|
}
|
|
|
|
events <- engine.Event{
|
|
|
|
Type: engine.StdoutColorEvent,
|
|
|
|
Payload: engine.StdoutEventPayload{
|
|
|
|
Message: message,
|
|
|
|
Color: colors.Always,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelSource.Cancel()
|
|
|
|
} else {
|
|
|
|
message := colors.BrightRed + "^C received; terminating" + colors.Reset
|
|
|
|
events <- engine.Event{
|
|
|
|
Type: engine.StdoutColorEvent,
|
|
|
|
Payload: engine.StdoutEventPayload{
|
|
|
|
Message: message,
|
|
|
|
Color: colors.Always,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelSource.Terminate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
signal.Notify(c.sigint, os.Interrupt)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
2018-05-01 00:27:53 +02:00
|
|
|
|
Revise the way previews are controlled
I found the flag --force to be a strange name for skipping a preview,
since that name is usually reserved for operations that might be harmful
and yet you're coercing a tool to do it anyway, knowing there's a chance
you're going to shoot yourself in the foot.
I also found that what I almost always want in the situation where
--force was being used is to actually just run a preview and have the
confirmation auto-accepted. Going straight to --force isn't the right
thing in a CI scenario, where you actually want to run a preview first,
just to ensure there aren't any issues, before doing the update.
In a sense, there are four options here:
1. Run a preview, ask for confirmation, then do an update (the default).
2. Run a preview, auto-accept, and then do an update (the CI scenario).
3. Just run a preview with neither a confirmation nor an update (dry run).
4. Just do an update, without performing a preview beforehand (rare).
This change enables all four workflows in our CLI.
Rather than have an explosion of flags, we have a single flag,
--preview, which can specify the mode that we're operating in. The
following are the values which correlate to the above four modes:
1. "": default (no --preview specified)
2. "auto": auto-accept preview confirmation
3. "only": only run a preview, don't confirm or update
4. "skip": skip the preview altogether
As part of this change, I redid a bit of how the preview modes
were specified. Rather than booleans, which had some illegal
combinations, this change introduces a new enum type. Furthermore,
because the engine is wholly ignorant of these flags -- and only the
backend understands them -- it was confusing to me that
engine.UpdateOptions stored this flag, especially given that all
interesting engine options _also_ accepted a dryRun boolean. As of
this change, the backend.PreviewBehavior controls the preview options.
2018-04-28 23:50:17 +02:00
|
|
|
// isInteractive returns true if the environment and command line options indicate we should
|
2018-05-01 00:27:53 +02:00
|
|
|
// do things interactively
|
2018-05-05 20:57:09 +02:00
|
|
|
func isInteractive(nonInteractive bool) bool {
|
2018-05-01 00:27:53 +02:00
|
|
|
return !nonInteractive && terminal.IsTerminal(int(os.Stdout.Fd())) && !isCI()
|
|
|
|
}
|
Revise the way previews are controlled
I found the flag --force to be a strange name for skipping a preview,
since that name is usually reserved for operations that might be harmful
and yet you're coercing a tool to do it anyway, knowing there's a chance
you're going to shoot yourself in the foot.
I also found that what I almost always want in the situation where
--force was being used is to actually just run a preview and have the
confirmation auto-accepted. Going straight to --force isn't the right
thing in a CI scenario, where you actually want to run a preview first,
just to ensure there aren't any issues, before doing the update.
In a sense, there are four options here:
1. Run a preview, ask for confirmation, then do an update (the default).
2. Run a preview, auto-accept, and then do an update (the CI scenario).
3. Just run a preview with neither a confirmation nor an update (dry run).
4. Just do an update, without performing a preview beforehand (rare).
This change enables all four workflows in our CLI.
Rather than have an explosion of flags, we have a single flag,
--preview, which can specify the mode that we're operating in. The
following are the values which correlate to the above four modes:
1. "": default (no --preview specified)
2. "auto": auto-accept preview confirmation
3. "only": only run a preview, don't confirm or update
4. "skip": skip the preview altogether
As part of this change, I redid a bit of how the preview modes
were specified. Rather than booleans, which had some illegal
combinations, this change introduces a new enum type. Furthermore,
because the engine is wholly ignorant of these flags -- and only the
backend understands them -- it was confusing to me that
engine.UpdateOptions stored this flag, especially given that all
interesting engine options _also_ accepted a dryRun boolean. As of
this change, the backend.PreviewBehavior controls the preview options.
2018-04-28 23:50:17 +02:00
|
|
|
|
2018-05-05 20:57:09 +02:00
|
|
|
// updateFlagsToOptions ensures that the given update flags represent a valid combination. If so, an UpdateOptions
|
|
|
|
// is returned with a nil-error; otherwise, the non-nil error contains information about why the combination is invalid.
|
|
|
|
func updateFlagsToOptions(interactive, skipPreview, yes bool) (backend.UpdateOptions, error) {
|
|
|
|
if !interactive && !yes {
|
|
|
|
return backend.UpdateOptions{},
|
|
|
|
errors.New("--yes must be passed in non-interactive mode")
|
Revise the way previews are controlled
I found the flag --force to be a strange name for skipping a preview,
since that name is usually reserved for operations that might be harmful
and yet you're coercing a tool to do it anyway, knowing there's a chance
you're going to shoot yourself in the foot.
I also found that what I almost always want in the situation where
--force was being used is to actually just run a preview and have the
confirmation auto-accepted. Going straight to --force isn't the right
thing in a CI scenario, where you actually want to run a preview first,
just to ensure there aren't any issues, before doing the update.
In a sense, there are four options here:
1. Run a preview, ask for confirmation, then do an update (the default).
2. Run a preview, auto-accept, and then do an update (the CI scenario).
3. Just run a preview with neither a confirmation nor an update (dry run).
4. Just do an update, without performing a preview beforehand (rare).
This change enables all four workflows in our CLI.
Rather than have an explosion of flags, we have a single flag,
--preview, which can specify the mode that we're operating in. The
following are the values which correlate to the above four modes:
1. "": default (no --preview specified)
2. "auto": auto-accept preview confirmation
3. "only": only run a preview, don't confirm or update
4. "skip": skip the preview altogether
As part of this change, I redid a bit of how the preview modes
were specified. Rather than booleans, which had some illegal
combinations, this change introduces a new enum type. Furthermore,
because the engine is wholly ignorant of these flags -- and only the
backend understands them -- it was confusing to me that
engine.UpdateOptions stored this flag, especially given that all
interesting engine options _also_ accepted a dryRun boolean. As of
this change, the backend.PreviewBehavior controls the preview options.
2018-04-28 23:50:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-05 20:57:09 +02:00
|
|
|
return backend.UpdateOptions{
|
|
|
|
AutoApprove: yes,
|
|
|
|
SkipPreview: skipPreview,
|
|
|
|
}, nil
|
Revise the way previews are controlled
I found the flag --force to be a strange name for skipping a preview,
since that name is usually reserved for operations that might be harmful
and yet you're coercing a tool to do it anyway, knowing there's a chance
you're going to shoot yourself in the foot.
I also found that what I almost always want in the situation where
--force was being used is to actually just run a preview and have the
confirmation auto-accepted. Going straight to --force isn't the right
thing in a CI scenario, where you actually want to run a preview first,
just to ensure there aren't any issues, before doing the update.
In a sense, there are four options here:
1. Run a preview, ask for confirmation, then do an update (the default).
2. Run a preview, auto-accept, and then do an update (the CI scenario).
3. Just run a preview with neither a confirmation nor an update (dry run).
4. Just do an update, without performing a preview beforehand (rare).
This change enables all four workflows in our CLI.
Rather than have an explosion of flags, we have a single flag,
--preview, which can specify the mode that we're operating in. The
following are the values which correlate to the above four modes:
1. "": default (no --preview specified)
2. "auto": auto-accept preview confirmation
3. "only": only run a preview, don't confirm or update
4. "skip": skip the preview altogether
As part of this change, I redid a bit of how the preview modes
were specified. Rather than booleans, which had some illegal
combinations, this change introduces a new enum type. Furthermore,
because the engine is wholly ignorant of these flags -- and only the
backend understands them -- it was confusing to me that
engine.UpdateOptions stored this flag, especially given that all
interesting engine options _also_ accepted a dryRun boolean. As of
this change, the backend.PreviewBehavior controls the preview options.
2018-04-28 23:50:17 +02:00
|
|
|
}
|