Skip to content

Conversation

@kenyon
Copy link
Member

@kenyon kenyon commented Nov 19, 2020

Improve shell quoting for safety and readability. Noticed these while reviewing #435 but didn't get to this before that was merged.

Copy link
Contributor

@igalic igalic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙋🏻‍♀️

$onlyif_command = $facts['os']['family'] ? {
'Windows' => "${cmd_exe_path} /C FOR /F %G IN ('${npm_path} config get globalconfig') DO IF EXIST %G (FINDSTR /B /C:\"${$config_setting}\" %G) ELSE (EXIT 1)",
default => "test -f $(${npm_path} config get globalconfig) && grep -qe \"^${$config_setting}\" $(${npm_path} config get globalconfig)",
default => "test -f \"$(${npm_path} config get globalconfig)\" && grep -qe '^${$config_setting}' \"$(${npm_path} config get globalconfig)\"",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't understand the logic of your choices: when did you pick single quotes and when did you pick double quotes, and, why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igalic when we don't need the shell to do any expansion or interpolation, we can use single quotes, which also saves us some escaping of double quotes. Command substitutions should usually be quoted to prevent word splitting and filename expansion on the results. Probably this doesn't matter for the expected values here, but might as well. Also, here

default => "test \"$(${npm_path} get ${config_setting} --global | tr -d '\n')\" != \"${value}\"",

we already had proper quoting of the command substitution, so I wanted to make the other ones consistent.

Shell programming is the worst 😉

I wonder if this should be using inifile instead, but I guess this works. 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, using inifile would make this infinitely more readable, and easier to transport from one OS to another, and simplify our testing, greatly, as the platform abstraction would already be handled by inifile itself

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, what do we do?
who has time for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just rebased on current master)

@igalic maybe I can get to a conversion to inifile eventually. I created #439 as a reminder.

@kenyon
Copy link
Member Author

kenyon commented Aug 28, 2025

The preferred way to do this these days is to use arrays instead of strings, so I'll close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants