Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 3 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
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,23 @@ If you'd like, make sure to include the following comment code tag in your READM
build and deploy your sample:

```text
[//]: # ({sst-run-unix})
[//]: # ({sst-run-bash})
```

For example:
````text
[//]: # ({sst-run-unix})
[//]: # ({sst-run-bash})
```
gcloud builds submit --tag=gcr.io/${GOOGLE_CLOUD_PROJECT}/run-mysql
```
````

When parsing the README for custom build and deploy commands, the serverless sample tester will include any commands
inside a code fence that is immediately preceded by a line containing `{sst-run-unix}`. You can use Markdown syntax
(e.g. `[//]: # ({sst-run-unix})`) to include this line without making it visible when rendered.
inside a code fence that is immediately preceded by a line containing `{sst-run-bash}`. You can use Markdown syntax
(e.g. `[//]: # ({sst-run-bash})`) to include this line without making it visible when rendered.

The parsed commands will not be run through a shell, meaning that the program will not perform any expansions,
pipelines, redirections or any other functions that shells are responsible for. This also means that popular shell
builtin commands like `cd`, `export`, and `echo` will not be available or may not work as expected.
The parsed commands will be run through the Bash shell, but each command will be run through a separate instance
of the shell, meaning that commands such as `cd` or `export` may not work as expected.
Copy link

Choose a reason for hiding this comment

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

Can you clarify how users are supposed to do control dir of execution or env variables, then? Can I chain commands via && for example?

Copy link
Author

@SaketramDurbha SaketramDurbha Aug 19, 2020

Choose a reason for hiding this comment

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

Thank you for your comment.

For environment variables, each command's process will uses the environment of the process from which users execute our program. So environment variables they have set in their shell where they call sst will be available for the commands that they write in their README. I'm going to adding a comment in our README to clarify this.

For the directory of execution, we're not currently supporting the option for users to control the directory where their README commands are executed; all commands will be executed in the sample's root directory (I going to a comment in our README to clarify this as well). But we're going to open an issue for this feature to make sure that it's tracked.

However, as you mentioned, you are able to get around this by chaining commands together, like cd .. && ls. But
I think we're not going to put this in our documentation because we believe a more permanent solution is preferred, and since each command is run in it's own separate instance of a shell, so you would have to chain each where you want a directory command with a cd.

Hope this makes sense. Did this answer your questions?


However, any environment variables referenced in the form of `$var` or `${var}` will expanded. In addition, bash-style
multiline commands (i.e. non-quoted backslashes at the end of a line that indicate a line continuation) will also be
Expand Down
22 changes: 12 additions & 10 deletions internal/lifecycle/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
const (
// The tag that should appear immediately before code blocks in a README to indicate that the enclosed commands
// are to be used by this program for building and deploying the sample.
codeTag = "{sst-run-unix}"
codeTag = "{sst-run-bash}"

// A non-quoted backslash in bash at the end of a line indicates a line continuation from the current line to the
// next line.
Expand All @@ -49,9 +49,9 @@ var (
// terminal commands inside of a Markdown code block.
type codeBlock []string

// toCommands extracts the terminal commands contained within the current codeBlock. It handles the expansion of
// environment variables and line continuations. It also detects Cloud Run service names Google Container Registry
// container image URLs and replaces them with the ones provided.
// toCommands extracts the terminal commands contained within the current codeBlock and creates `exec.Cmd`s for them
// that pass them through the Bash shell. It also detects Cloud Run service names Google Container Registry container
// image URLs and replaces them with the ones provided.
func (cb codeBlock) toCommands(serviceName, gcrURL string) ([]*exec.Cmd, error) {
var cmds []*exec.Cmd

Expand Down Expand Up @@ -79,17 +79,19 @@ func (cb codeBlock) toCommands(serviceName, gcrURL string) ([]*exec.Cmd, error)
line = line + l
}

line = os.ExpandEnv(line)
line = gcrURLRegexp.ReplaceAllString(line, gcrURL)
line = replaceServiceName(line, serviceName)
sp := strings.Split(line, " ")

var cmd *exec.Cmd
if sp[0] == "gcloud" {
a := append(util.GcloudCommonFlags, sp[1:]...)
cmd = exec.Command("gcloud", a...)
if gcloudCommandRegexp.MatchString(line) {
// Splitting command to add util.GcloudCommonFlags to front of command.
// TODO: Ideally want to use a library that splits commands according to bash specification.
sp := strings.Split(line, " ")
c := "gcloud " + strings.Join(append(util.GcloudCommonFlags, sp[1:]...), " ")

cmd = exec.Command("bash", "-c", c)
} else {
cmd = exec.Command(sp[0], sp[1:]...)
cmd = exec.Command("bash", "-c", line)
}

cmds = append(cmds, cmd)
Expand Down