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

Install option escaping #362

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,37 @@ package {'mysql':
}
~~~

You can also use `chocholatey::install_options()` to perform the neccessary
escaping, and splitting, of arguments.

~~~puppet
package {'mysql':
ensure => latest,
provider => 'chocolatey',
install_options => chocolatey::install_options(
'-override',
'-installArgs',
'/INSTALLDIR="C:\Program Files\somewhere"',
),
}
~~~

The directory name itself can be escaped with `chocolatey::escape()`.

~~~puppet
$install_dir = 'C:\Program Files\somewhere'

package {'mysql':
ensure => latest,
provider => 'chocolatey',
install_options => chocolatey::install_options(
'-override',
'-installArgs',
"/INSTALLDIR=$(chocolatey::escape($install_dir))",
),
}
~~~

**Note:** The above is for Chocolatey v0.9.9+. You may need to look for an
alternative method to pass args if you have 0.9.8.x and below.

Expand Down
75 changes: 75 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
* [`chocolateyfeature`](#chocolateyfeature): Allows managing features for Chocolatey. Features are configuration that act as feature flippers to turn on or off certain aspects of how Cho
* [`chocolateysource`](#chocolateysource): Allows managing sources for Chocolatey. A source can be a folder, a CIFS share, a NuGet Http OData feed, or a full Package Gallery. Learn mor

### Functions

* [`chocolatey::escape`](#chocolatey--escape): Perform escaping and quoting for an install option
* [`chocolatey::install_options`](#chocolatey--install_options): Perform escaping, and splitting of a number of install options

### Tasks

* [`init`](#init): Manage a package
Expand Down Expand Up @@ -404,6 +409,76 @@ Default value: `''`
The specific backend to use for this `chocolateysource` resource. You will seldom need to specify this --- Puppet will
usually discover the appropriate provider for your platform.

## Functions

### <a name="chocolatey--escape"></a>`chocolatey::escape`

Type: Puppet Language

Escapes double quotes, and wraps values containing spaces in double quotes.

#### `chocolatey::escape(String $option)`

Escapes double quotes, and wraps values containing spaces in double quotes.

Returns: `String`

##### `option`

Data type: `String`

An install option to be escaped

### <a name="chocolatey--install_options"></a>`chocolatey::install_options`

Type: Puppet Language

Perform escaping, and splitting of a number of install options

#### Examples

#####

```puppet
package {'mysql':
ensure => latest,
provider => 'chocolatey',
install_options => chocolatey::install_options(
'-override',
'-installArgs',
"/INSTALLDIR=${chocolatey::escape('C:\Program Files\somewhere'}",
),
}
```

#### `chocolatey::install_options(Array[String] $options)`

The chocolatey::install_options function.

Returns: `Array[String]`

##### Examples

######

```puppet
package {'mysql':
ensure => latest,
provider => 'chocolatey',
install_options => chocolatey::install_options(
'-override',
'-installArgs',
"/INSTALLDIR=${chocolatey::escape('C:\Program Files\somewhere'}",
),
}
```

##### `options`

Data type: `Array[String]`

An install option to be escaped

## Tasks

### <a name="init"></a>`init`
Expand Down
11 changes: 11 additions & 0 deletions functions/escape.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# @summary Perform escaping and quoting for an install option
#
# Escapes double quotes, and wraps values containing spaces in double quotes.
#
# @param option
# An install option to be escaped
# @return
function chocolatey::escape(String $option) >> String {
$option.regsubst('"', '""', 'G')
.regsubst('^(.*\s.*)$', '"\1"')
}
23 changes: 23 additions & 0 deletions functions/install_options.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @summary Perform escaping, and splitting of a number of install options
#
# @example
# package {'mysql':
# ensure => latest,
# provider => 'chocolatey',
# install_options => chocolatey::install_options(
# '-override',
# '-installArgs',
# "/INSTALLDIR=${chocolatey::escape('C:\Program Files\somewhere'}",
# ),
# }
#
# @param options
# An install option to be escaped
# @return
function chocolatey::install_options(Array[String] *$options) >> Array[String] {
$options.map |$option| {
chocolatey::escape($option)
}.reduce([]) |$memo, $option| {
$memo + $option.split(' ')
}
}
9 changes: 9 additions & 0 deletions spec/functions/escape_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'chocolatey::escape' do
it { is_expected.to run.with_params('-installArgs').and_return('-installArgs') }
it { is_expected.to run.with_params('C:\Program Files').and_return('"C:\Program Files"') }
it { is_expected.to run.with_params('/INSTALLDIR="C:\Program Files"').and_return('"/INSTALLDIR=""C:\Program Files"""') }
end
35 changes: 35 additions & 0 deletions spec/functions/install_options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'chocolatey::install_options' do
it {
is_expected.to run.with_params(
'-override',
'-installArgs',
'/VERYSILENT /NORESTART'
).and_return(
[
'-override',
'-installArgs',
'"/VERYSILENT',
'/NORESTART"'
]
)
}

it {
is_expected.to run.with_params(
'-override',
'-installArgs',
'/INSTALLDIR="C:\Program Files\somewhere"',
).and_return(
[
'-override',
'-installArgs',
'"/INSTALLDIR=""C:\Program',
'Files\somewhere"""',
]
)
}
end