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

Replace '-' in bash variables with '_' #129

Merged
merged 8 commits into from
Mar 7, 2014

Conversation

william-richard
Copy link
Contributor

Fixed bug reported in issue #118, where bash variables are invalid if they have "-" in their name.

@dallasmarlow @joshrabinowitz @byxorna

Will Richard added 2 commits March 5, 2014 15:28
@@ -160,7 +160,7 @@ trait ApiResponse extends Controller {
v match {
case m: JsObject => formatBashResponse(m, "%s_".format(prefix + k))
case JsArray(list) => formatList(list, "%s_".format(prefix + k))
case o => "%s%s=%s;".format(prefix, k, formatBasic(o))
case o => "%s%s=%s;".format(prefix, k.replace('-', '_'), formatBasic(o)) //Variables with '-' in their name are invalid for bash.
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems kinda hacky. There are other chars that make the key not a valid POSIX environment variable name; we should handle them all. Ill make a branch with some changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Opened #130 to fix.

@william-richard
Copy link
Contributor Author

#130 is the right way to do this.

@william-richard
Copy link
Contributor Author

Didn't realizing #130 was pushing into this PR's branch.

@byxorna
Copy link
Contributor

byxorna commented Mar 6, 2014

May wanna add come other test cases, i.e. attributes starting with a number -> _, or unsupported characters are truncated to _ just for completeness? Otherwise LGTM. 🉑

@william-richard
Copy link
Contributor Author

Right now, the test that we do have is a bit hacky. IMHO, we shouldn't really be testing this functionality here. But, I didn't think there were enough tests to justify its own test suite. If we really want to test full POSIX key correctness when we give back shellscript output, then yeah, we should probably make a separate test spec for it.


// formats a key to an acceptable POSIX environment variable name
def formatPosixKey(key: String): String = if (!key.isEmpty) {
("""[^a-zA-Z_]""".r.replaceAllIn(key.head.toString,"_") + """[^a-zA-Z0-9_]""".r.replaceAllIn(key.tail,"_")).toUpperCase
Copy link
Contributor

Choose a reason for hiding this comment

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

@byxorna @Primer42 i'm not sure this is what we want here, i personally would rather get an exception if i ask for a bash response of an asset with an attribute of 99_balloons then get ___balloons. replacing special characters like - or | with underscores seems completely fine though.

Copy link
Contributor

Choose a reason for hiding this comment

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

This would make 99_balloons -> _9_balloons, but its still not ideal. Silently nuking user data in the response is bad, so maybe we can preserve it. Lets just prefix the key with a _ if the first character isnt POSIX compliant. That way the full key is preserved, but made posix compliant by stamping on as few characters as possible. (Special chars like ! and - will still be made to _). Ill open a PR onto this branch with my fix

Copy link
Contributor

Choose a reason for hiding this comment

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

@byxorna i agree, i think that's a really good solution. someone should also mention this in the docs as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add something about this to the API doc. PR imminent.

Copy link
Contributor

Choose a reason for hiding this comment

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

#131 fixes.

val posixTailRegex = """[^a-zA-Z0-9_]""".r
key.head.toString match {
case posixHeadRegex() => formatPosixKey("_" + key)
case _ => posixTailRegex.replaceAllIn(key,"_").toUpperCase
Copy link
Contributor

Choose a reason for hiding this comment

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

@Primer42 would you mind fixing the spacing here?

@dallasmarlow
Copy link
Contributor

lgtm 🍕

@william-richard
Copy link
Contributor Author

This PR currently breaks the text/x-shellscript endpoint.

@william-richard
Copy link
Contributor Author

Found the bug and tested. LGTM 🎸

@byxorna
Copy link
Contributor

byxorna commented Mar 7, 2014

💴 +1

william-richard pushed a commit that referenced this pull request Mar 7, 2014
…variables

Ensure variable names are valid POSIX names when using text/x-shellscript API endpoint.
@william-richard william-richard merged commit d5a28e0 into master Mar 7, 2014
@william-richard william-richard deleted the will-replace-dash-in-shellscript-variables branch March 7, 2014 20:13
dalehamel pushed a commit to Shopify/collins that referenced this pull request Jun 10, 2014
…cript-variables

Ensure variable names are valid POSIX names when using text/x-shellscript API endpoint.
dalehamel pushed a commit to Shopify/collins that referenced this pull request Jun 10, 2014
…cript-variables

Ensure variable names are valid POSIX names when using text/x-shellscript API endpoint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants