Commit Graph

40 Commits

Author SHA1 Message Date
Ricard Solé 3d8538cdb8 build: runs gazelle 2024-03-19 09:15:36 +01:00
Thomas Gummerer 423d65cec1 more 2023-11-08 19:52:28 +01:00
Thomas Gummerer 6004ff2601 more 2023-11-08 19:13:05 +01:00
Thomas Gummerer f106b0f13e more 2023-11-08 18:47:37 +01:00
Thomas Gummerer 6d77e8a6d2 add bazel files 2023-11-08 18:26:23 +01:00
Abhinav Gupta 615bd0f0e7
cmdutil: Support errors.Join-based multi-errors ()
cmdutil has some special handling for hashicorp/go-multierror
so that multi-errors are printed cleanly in the form:

    %d errors occurred:
        1) foo
        2) bar
        ...

In Go 1.20, the errors package got a native `errors.Join` function.
This adds support for errors.Join-based multi-errors to this logic.

These errors implement an `Unwrap() []error` method
which can be used to access the full list of errors.
We use that and then implement the same logic for formatting as before.
2023-09-01 19:01:16 +00:00
Kyle Dixler 0f5dbd2a56
Revert "resource/plugin: Shut down plugins gracefully ()" ()
<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Fixes 

This reverts commit 25901d95c4.

On linux, `pulumi up` hung during a `pulumi-command provider` panic
after this change was introduced.
2023-08-31 16:29:55 +00:00
Abhinav Gupta b51caa6ab4
cmdutil.ReadConsole[NoEcho]: Use bubbletea ()
Switch the cmdutil.ReadConsole and cmdutil.ReadConsoleNoEcho functions
to use the bubbletea library to render the prompt,
using the textinput widget provided by the accompanying bubbles library.
The resulting input widgets support arrow keys, back space,
and some basic readline-style bindings including Ctrl-A, Alt-B, etc.

I went through all uses of ReadConsole or ReadConsoleNoEcho.
Only the one in new.go had a non-compliant prompt that I had to adjust.

Note: One divergence in behavior I opted for was that
password prompts will echo '*' characters as the user is typing
and then no echo once they've accepted or canceled the value.
Previously, the prompt did not echo anything in either case.

<details>

  <summary>
  Introduction if you're unfamiliar with bubbletea
  </summary>

  bubbletea operates by modeling the widget state as
  an immutable data structure that receives messages for events.
  On receiving a message (key press, e.g.) the model's Update method
  returns a new model instance representing its new state.
  Update may also optionally return additional commands for the program,
  e.g. stop running, or print something and move on.
  The model's View method returns what should be drawn in the terminal
  based on the model's current state.
This programming model makes it reasonably straightforward to unit test
  some of the core functionality of independent widgets
  as demonstrated in this PR.

</details>

Resolves 

---

Demos:

<details>
  <summary>Plain text</summary>
  

![prompt-plain](https://github.com/pulumi/pulumi/assets/41730/66258fc8-f772-4d01-bc7c-1f7b116aebaa)
</details>

<details>
  <summary>Secret</summary>


![prompt-secret](https://github.com/pulumi/pulumi/assets/41730/372f862e-9186-4d47-ba7d-0107c47f52f6)
</details>

<details>
  <summary>Secret prompt with padding</summary>


![prompt-secret-2](https://github.com/pulumi/pulumi/assets/41730/e9b7c253-4c9d-4235-9fa6-197aa0522033)
</details>
2023-08-30 17:08:44 +00:00
Abhinav Gupta 25901d95c4
resource/plugin: Shut down plugins gracefully ()
Uses the new TerminateProcessGroup functionality introduced in 
to shut down plugins gracefully.

Graceful shutdown takes the following form:

- Send a termination signal (SIGINT or CTRL_BREAK_EVENT)
- Wait up to 1 second for the plugin to exit
- Kill it with SIGKILL

Note that TerminateProcessGroup kills the entire group
so we don't need a separate KillChildren and cmd.Process.Kill().

This change also deprecates cmdutil.KillChildren
since we shouldn't really be using SIGKILL as a first resort anyway.

Note that this does not modify the behavior of individual plugins.
Those will exit as usual but with a SIGINT instead of SIGKILL
terminating them.

Resolves 
2023-08-29 12:02:54 +00:00
Fraser Waters 3d9ddb2981
Support bailing from RunFunc ()
**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
Abhinav Gupta b67c72149c
feat(cmdutil): TerminateProcessGroup for graceful termination ()
Adds a new **currently unused** function TerminateProcessGroup
that terminates all processes in a group gracefully.

It does so by first sending the process
a SIGINT on Unix systems, and CTRL_BREAK_EVENT on Windows,
and waiting a specified duration for the process to exit.
The choice of signals was very deliberate
and is documented in the comments for TerminateProcessGroup.

If the process does not exit in the given duration,
it and its child processes are forcibly terminated
with SIGKILL or equivalent.

Testing:
The core behaviors are tested against Python, Go, and Node.
The corner cases of signal handling are tested
against rogue Go processes.
The changes were experimented with in .

Refs 
2023-08-27 22:05:44 +00:00
Abhinav Gupta f0f38d6f18
chore(cmdutil): Rename build-tagged files ()
Files tagged with OS-specific build tags
should usually have a suffix in their name
indicating which OS they're narrowing to.

child_windows.go already does this,
but child.go aimed at Linux and macOS does not.
This change renames child.go to child_unix.go.

Further, it moves the go:build directives
outside the copyright block.
2023-08-25 02:14:34 +00:00
Fraser Waters 571fadae3f Use slice.Prealloc instead of make([]T, 0, ...)
Fixes https://github.com/pulumi/pulumi/issues/12738

https://github.com/pulumi/pulumi/pull/11834 turned on the prealloc
linter and changed a load of slice uses from just `var x T[]` to `x :=
make([]T, 0, preallocSize)`. This was good for performance but it turns
out there are a number of places in the codebase that treat a `nil`
slice as semnatically different to an empty slice.

Trying to test that, or even reason that through for every callsite is
untractable, so this PR replaces all expressions of the form `make([]T,
0, size)` with a call to `slice.Prealloc[T](size)`. When size is 0 that
returns a nil array, rather than an empty array.
2023-06-29 11:27:50 +01:00
Pat Gavlin a8f41f031b [sdk] Update uniseg
The latest version of this module dramatically improves its allocation
volume and offers a friendlier API for measuring string width.
2023-05-25 22:24:13 -07:00
Pat Gavlin 6a4740e8cb [cli] Better memory profiling
The current memory profile reports the live objects and their sources at
the time at which it is captured. This is almost never what we want:
instead, we want a profile of _all_ allocations over the lifetime of the
process. These changes replace the heap profile with an allocation
profile and add a parameter, --memprofilerate, to control the rate at
which allocations are profiled.
2023-05-25 09:20:12 -07:00
Abhinav Gupta b367bef179
go/cmdutil: Test for IsTruthy
Add a unit test for IsTruthy
because it appears to be lacking one.
2023-03-07 10:01:07 -08:00
Abhinav Gupta 7aa5b77a0c
all: Reformat with gofumpt
Per team discussion, switching to gofumpt.

[gofumpt][1] is an alternative, stricter alternative to gofmt.
It addresses other stylistic concerns that gofmt doesn't yet cover.

  [1]: https://github.com/mvdan/gofumpt

See the full list of [Added rules][2], but it includes:

- Dropping empty lines around function bodies
- Dropping unnecessary variable grouping when there's only one variable
- Ensuring an empty line between multi-line functions
- simplification (`-s` in gofmt) is always enabled
- Ensuring multi-line function signatures end with
  `) {` on a separate line.

  [2]: https://github.com/mvdan/gofumpt#Added-rules

gofumpt is stricter, but there's no lock-in.
All gofumpt output is valid gofmt output,
so if we decide we don't like it, it's easy to switch back
without any code changes.

gofumpt support is built into the tooling we use for development
so this won't change development workflows.

- golangci-lint includes a gofumpt check (enabled in this PR)
- gopls, the LSP for Go, includes a gofumpt option
  (see [installation instrutions][3])

  [3]: https://github.com/mvdan/gofumpt#installation

This change was generated by running:

```bash
gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error)
```

The following files were manually tweaked afterwards:

- pkg/cmd/pulumi/stack_change_secrets_provider.go:
  one of the lines overflowed and had comments in an inconvenient place
- pkg/cmd/pulumi/destroy.go:
  `var x T = y` where `T` wasn't necessary
- pkg/cmd/pulumi/policy_new.go:
  long line because of error message
- pkg/backend/snapshot_test.go:
  long line trying to assign three variables in the same assignment

I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 09:00:24 -08:00
Abhinav Gupta cc32691bc1
chore(all): strings.Replace(..., -1) => strings.Replace(...)
Replaces all instances of `strings.Replace(s, old, new, -1)`
with the equivalent `strings.ReplaceAll(s, old, new)`.
This function has been available since Go 1.12.
2023-03-01 13:22:33 -08:00
Abhinav Gupta ae13d3c84a
cmd/stack output: Print to an io.Writer
The implementation of `pulumi stack output` prints everything
to a global `io.Writer`.
This makes testing its output a bit hacky and unparalellizable
because we have to hijack `os.Stdout` to capture the output.

This changes it the command implementation
and all the functions it relies on
to accept an `io.Writer` as an argument.

This allows us to delete the os.Stdout hijacking hack
that was introduced in ,
and have those tests run in parallel, writing to an in-memory buffer.
2023-01-25 15:17:18 -08:00
Abhinav Gupta cf5fa128d4
crypto/ssh/terminal is deprecated
golang.org/x/crypto/ssh/terminal is deprecated.
golang.org/x/term is a near drop-in replacement for it.

In service to 
2023-01-12 09:07:34 -08:00
Ian Wahbe db1e293c2e Move over some env vars to the new lib
I modified cmdutil.Table to allow printing multi-line rows to allow vars
to have multi-line documentation.
2022-12-15 15:46:39 +01:00
Aaron Friel ab1ec4f682 revert(sdk/go): Revert changes to String & marshaling method receivers 2022-10-13 13:54:37 -07:00
杨成锴 7b09ad142a chore: Update doc comments, coding style, fix lint
* about error string: error string should not be capitalized or end with punctuation mark
* apply suggestions from code review
* lint: fix format error and simplify the code

Co-authored-by: Fraser Waters <frassle@gmail.com>
Co-authored-by: Aaron Friel <mayreply@aaronfriel.com>
2022-10-13 13:50:49 -07:00
Aaron Friel a4b1d6b2a7 ci: gofmt 1.18+ clean 2022-09-21 09:48:39 -07:00
Aaron Friel 4e19e95a83
Address engine data races detected by `go test -race` ()
* fix: data race on global var

* fix: data race on waiting for events to close

* fix: data race on URNs in test

* chore: note spurious data race, simplify outputstate

* chore: log error on reusing a resource state, as it will cause data races

* chore: remove note, reference PR number in comment
2022-07-12 09:39:07 -07:00
Anton Tayanovskyy 32b395814d
Fix --tracing flag propagation to resource plugins () 2022-07-01 16:58:00 -04:00
Aaron Friel ed2923653c ci: radical idea - what if slow tests & no stdout makes GH consider runner dead? 2022-03-06 14:52:13 -08:00
Kyle Dixler 6a8f7be1d5
re-enable and color dot spinner for non-interactive mode ()
* re-enabled dot spinner for non-interactive mode

Co-authored-by: Kyle Dixler <kyle@pulumi.com>
2022-02-15 14:37:04 -08:00
Fraser Waters b58c39476f
Fix cmdutil.PrintTable to handle ansi escapes and non-byte glyphs ()
Fixes two bugs in how padding was calculated in PrintTable.

Firstly we remove all ANSI escape codes from the string before measuring
how wide it is. Secondly we measure glyph count (using rivo/uniseg) not
byte or rune count of the string.

Together these fix the padding/alignment issues I saw when using
PrintTable with plan output. They also slightly change the layout of
"pulumi stack", for example the below is printed with current master and
has 6 characters of space for padding between SecurityGroup and
web-secgrp:

```
Current stack resources (4):
    TYPE                                        NAME
    pulumi:pulumi:Stack                         aws-cs-webserver-test
    ├─ aws:ec2/securityGroup:SecurityGroup      web-secgrp
    ├─ aws:ec2/instance:Instance                web-server-www
    └─ pulumi:providers:aws                     default_4_25_0
```

While printed with this commit you only get 2 characters of space for
padding (which is correct, the column gap is set to "  "):
```
Current stack resources (4):
    TYPE                                    NAME
    pulumi:pulumi:Stack                     aws-cs-webserver-test
    ├─ aws:ec2/securityGroup:SecurityGroup  web-secgrp
    ├─ aws:ec2/instance:Instance            web-server-www
    └─ pulumi:providers:aws                 default_4_25_0
```
2021-11-04 10:06:20 +00:00
Anton Tayanovskyy 27b1404d9e
Fix lint ()
* Fix lint

* Fix lint of pkg folder
2021-09-07 16:41:17 -04:00
Ian Wahbe f75ddfc01d Refactor data into get and display components
This allows us to give the output in json.
2021-08-23 00:48:22 -07:00
Pat Gavlin 2cc89defbc
Read passphrase from the terminal when rotating. ()
Rotating a passphrase requires that the old passphrase is available via
one of the `PULUMI_CONFIG_PASSPHRASE` or `PULUMI_CONFIG_PASSPHRASE_FILE`
environment variables. This confuses `readPassphrase` when reading a new
passphrase, since that function checks the aforementioned environment
variables prior to reading from the console. The overall effect is that
it is impossible to rotate the passphrase for a stack using the
passphrase provider. These changes fix this by always reading from the
console when rotating a passphrase.
2021-06-22 11:13:57 -07:00
Anton Tayanovskyy 30e999ff1a
Tracing enhancements for CLI perf metrics capture ()
* Allow ProgramTest Tracing flag to expand {command}

* Fix comment typos

* Memory stats collection
2021-06-15 13:25:03 -04:00
Anton Tayanovskyy 7ff1491397
Add trace proxying to fix sub-process trace collection into files ()
* Add trace proxying to fix sub-process trace collection when tracing to files

* Better func naming in test

* Avoid dealing with Windows path nightmare

* On Windows it is go.exe of course

* Rename operation to component to better align with existing trace output
2021-06-10 22:57:18 -04:00
pulumi-bot 73a66f48ea [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
Komal 4882c9fec5
[CLI] - Add commands for config set-all and rm-all () 2021-02-19 21:55:58 -08:00
CyrusNajmabadi 66bd3f4aa8
Breaking changes due to Feature 2.0 work
* Make `async:true` the default for `invoke` calls ()

* Switch away from native grpc impl. ()

* Remove usage of the 'deasync' library from @pulumi/pulumi. ()

* Only retry as long as we get unavailable back.  Anything else continues. ()

* Handle all errors for now. ()


* Do not assume --yes was present when using pulumi in non-interactive mode ()

* Upgrade all paths for sdk and pkg to v2

* Backport C# invoke classes and other recent gen changes ()

Adjust C# generation

* Replace IDeployment with a sealed class ()

Replace IDeployment with a sealed class

* .NET: default to args subtype rather than Args.Empty ()

* Adding system namespace for Dotnet code gen

This is required for using Obsolute attributes for deprecations

```
Iam/InstanceProfile.cs(142,10): error CS0246: The type or namespace name 'ObsoleteAttribute' could not be found (are you missing a using directive or an assembly reference?) [/Users/stack72/code/go/src/github.com/pulumi/pulumi-aws/sdk/dotnet/Pulumi.Aws.csproj]
Iam/InstanceProfile.cs(142,10): error CS0246: The type or namespace name 'Obsolete' could not be found (are you missing a using directive or an assembly reference?) [/Users/stack72/code/go/src/github.com/pulumi/pulumi-aws/sdk/dotnet/Pulumi.Aws.csproj]
```

* Fix the nullability of config type properties in C# codegen ()
2020-04-14 09:30:25 +01:00
evanboyle c1440e48d4 move pkg/util/result -> sdk/go/common/util 2020-03-18 15:45:42 -07:00
evanboyle fa348ceb1b move pkg/util/ciutil -> sdk/go/common/util/ciutil 2020-03-18 15:43:31 -07:00
evanboyle c1d3a8524b move pkg/util/cmdutil -> sdk/go/common/util/cmdutil 2020-03-18 15:39:00 -07:00