Skip to content

refactor: conditionally use dotfiles in dogfood template #5332

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

Merged
merged 9 commits into from
Dec 13, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: use dotfiles if dotfiles var exists
This ensures the `coder dotfiles` command only runs if the dotfiles var
in the template is not empty.
  • Loading branch information
jsjoeio committed Dec 6, 2022
commit 3fbe100acefc8b9dfe1f95d42e65e56b62a87e8d
1 change: 1 addition & 0 deletions dogfood/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ resource "coder_agent" "dev" {
code-server --auth none --port 13337 &
sudo service docker start
coder dotfiles -y 2>&1 | tee ~/.personalize.log
"if [ -n ${var.dotfiles_uri} ]; then coder dotfiles var.dotfiles_uri -y 2>&1 | tee ~/.personalize.log; fi"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unsure if this is the exact way to do this interpolation within the Bash script. Smoke-tested locally using terraform validate. cc @bpmct

Ref: Conditionals

Copy link
Member

@johnstcn johnstcn Dec 7, 2022

Choose a reason for hiding this comment

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

I think you need to quote the variable in order for -n to work; I'm not sure if terraform will quote it for you.
At least, shellcheck seems to think so.
Also, I believe double brackets are generally preferred for conditionals over single brackets (ref: https://www.baeldung.com/linux/bash-single-vs-double-brackets) Edit: disregard, @mafredri points out that [[ is a bash-ism

Copy link
Member

Choose a reason for hiding this comment

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

@mafredri Shouldn't we have set pipefail in this script?

Copy link
Member

Choose a reason for hiding this comment

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

@mtojek Since we're supporting /bin/sh, we should avoid -o pipefail (it's a bash-ism).

Copy link
Contributor Author

@jsjoeio jsjoeio Dec 7, 2022

Choose a reason for hiding this comment

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

Okay found this and these are called Heredoc strings

Now I'm second guessing it. Is this what it should be?

Suggested change
"if [ -n ${var.dotfiles_uri} ]; then coder dotfiles var.dotfiles_uri -y 2>&1 | tee ~/.personalize.log; fi"
if [ -n "${var.dotfiles_uri}" ]; then coder dotfiles "${var.dotfiles_uri}" -y 2>&1 | tee ~/.personalize.log; fi

Not sure if the quotes will stay so could be ""${var.dotfilesa-uri}""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@mafredri mafredri Dec 9, 2022

Choose a reason for hiding this comment

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

@jsjoeio I think a great way to deal with variables is to assign them to a shell variable in the scripts, like this:

DOTFILES_URI=${var.dotfiles_uri}
if [ -n "$DOTFILES_URI" ]; then
   # ...
fi

This way escaping the value becomes a non-issue because the assignment supports basically any input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Genius! I'll see if I can wrap this up at the end of the day!

EOF
}

Expand Down