Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read command to run with Bolt from a file #2125

Closed
nicklewis opened this issue Aug 26, 2020 · 1 comment · Fixed by #2181
Closed

Read command to run with Bolt from a file #2125

nicklewis opened this issue Aug 26, 2020 · 1 comment · Fixed by #2181
Assignees
Labels
Feature New features and improvements.

Comments

@nicklewis
Copy link
Contributor

Use Case

On certain systems, particularly network devices, files can't be written and therefore bolt script run doesn't work. In that case, it's useful to be able to run a long, multi-line script as a command.

Describe the Solution You Would Like

bolt command run @file.txt should read the command run a file named file.txt. If the file doesn't exist or can't be read, it should error.

@nicklewis nicklewis added the Feature New features and improvements. label Aug 26, 2020
@lucywyman lucywyman added this to 📝 To Do in DEPRECATED: old Bolt Kanban via automation Sep 2, 2020
@jpogran
Copy link
Contributor

jpogran commented Sep 3, 2020

Testing this approach in PowerShell proves we can't use @ without escaping it because that's the spaltting operator :

➜ function bolt { param($command) "I'm reading content of $Command"; gc $command }
➜ bolt @foo.txt
ParserError:
Line |
   1 |  bolt @foo.txt
     |        ~~~~
     | The splatting operator '@' cannot be used to reference variables in an expression. '@foo' can be used
     | only as an argument to a command. To reference variables in an expression use '$foo'.

You can use the backtick escape character to get PowerShell to skip using @ and have it successfully pass down:

➜ bolt `@foo.txt
I'm reading content of @foo.txt
Get-Content: Cannot find path 'C:\Users\james\src\puppetlabs\bolt\@foo.txt' because it does not exist.
➜

That suffices the raw bolt command, if leaves a little to be desired in UX.

Using PowerShell Language features, we could have the cmdlet handle it without using @. A super naïve approach follows:

➜ function bolt {
>>   param(
>>     $command
>>   )
>>   if(Test-Path $command){
>>     "I'm reading content of $Command";
>>     $text = gc $command
>>     # invoke bolt
>>   }else{
>>     # invoke bolt
>>   }
>> }

@beechtom beechtom self-assigned this Sep 15, 2020
@beechtom beechtom moved this from 📝 To Do to ⚡️ Doing in DEPRECATED: old Bolt Kanban Sep 15, 2020
beechtom added a commit to beechtom/bolt that referenced this issue Sep 16, 2020
This adds support for reading a command from a file or `stdin` using
the `bolt command run` command. To read a command from a file,
pass the path to the file with the `@` symbol:

```shell
$ bolt command run @script.sh --targets targets
```

The filepath is expanded relative to the current working directory.

To read a command from `stdin`, pass a single dash `-` as the command
and then pipe the command to Bolt:

```shell
$ echo whoami | bolt command run - --targets targets
```

!feature

* **Read command from a file or `stdin` using `bolt command run`**
  ([puppetlabs#2125](puppetlabs#2125))

  The `bolt command run` command can now read a command from a file or
  `stdin`.
beechtom added a commit to beechtom/bolt that referenced this issue Sep 16, 2020
This adds support for reading a command from a file or `stdin` using
the `bolt command run` command. To read a command from a file,
pass the path to the file with the `@` symbol:

```shell
$ bolt command run @script.sh --targets targets
```

The filepath is expanded relative to the current working directory.

To read a command from `stdin`, pass a single dash `-` as the command
and then pipe the command to Bolt:

```shell
$ echo whoami | bolt command run - --targets targets
```

!feature

* **Read command from a file or `stdin` using `bolt command run`**
  ([puppetlabs#2125](puppetlabs#2125))

  The `bolt command run` command can now read a command from a file or
  `stdin`.
beechtom added a commit to beechtom/bolt that referenced this issue Sep 16, 2020
This adds support for reading a command from a file or `stdin` using
the `bolt command run` command. To read a command from a file,
pass the path to the file with the `@` symbol:

```shell
$ bolt command run @script.sh --targets targets
```

The filepath is expanded relative to the current working directory.

To read a command from `stdin`, pass a single dash `-` as the command
and then pipe the command to Bolt:

```shell
$ echo whoami | bolt command run - --targets targets
```

!feature

* **Read command from a file or `stdin` using `bolt command run`**
  ([puppetlabs#2125](puppetlabs#2125))

  The `bolt command run` command can now read a command from a file or
  `stdin`.
@beechtom beechtom moved this from ⚡️ Doing to 🚧 Reviewing in DEPRECATED: old Bolt Kanban Sep 16, 2020
@beechtom beechtom added this to 📝 To Do in Bolt Kanban via automation Sep 17, 2020
@beechtom beechtom moved this from 📝 To Do to 🚧 Reviewing in Bolt Kanban Sep 17, 2020
@beechtom beechtom removed this from 🚧 Reviewing in DEPRECATED: old Bolt Kanban Sep 17, 2020
@beechtom beechtom linked a pull request Sep 17, 2020 that will close this issue
beechtom added a commit to beechtom/bolt that referenced this issue Sep 21, 2020
This adds support for reading a command from a file or `stdin` using
the `bolt command run` command. To read a command from a file,
pass the path to the file with the `@` symbol:

```shell
$ bolt command run @script.sh --targets targets
```

The filepath is expanded relative to the current working directory.

To read a command from `stdin`, pass a single dash `-` as the command
and then pipe the command to Bolt:

```shell
$ echo whoami | bolt command run - --targets targets
```

!feature

* **Read command from a file or `stdin` using `bolt command run`**
  ([puppetlabs#2125](puppetlabs#2125))

  The `bolt command run` command can now read a command from a file or
  `stdin`.
beechtom added a commit that referenced this issue Sep 21, 2020
@lucywyman lucywyman removed this from 🚧 Reviewing in Bolt Kanban Sep 21, 2020
dontlaugh pushed a commit to dontlaugh/bolt that referenced this issue Nov 22, 2020
This adds support for reading a command from a file or `stdin` using
the `bolt command run` command. To read a command from a file,
pass the path to the file with the `@` symbol:

```shell
$ bolt command run @script.sh --targets targets
```

The filepath is expanded relative to the current working directory.

To read a command from `stdin`, pass a single dash `-` as the command
and then pipe the command to Bolt:

```shell
$ echo whoami | bolt command run - --targets targets
```

!feature

* **Read command from a file or `stdin` using `bolt command run`**
  ([puppetlabs#2125](puppetlabs#2125))

  The `bolt command run` command can now read a command from a file or
  `stdin`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New features and improvements.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants