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

Add run-if-checksum-changed-script #38

Closed
Closed
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,15 @@ commands:
```

##### `cmd`

`key: cmd`

`type: string or array of strings`

Actual command to run in shell.

You can access command name in your script, it stored in env var `LETS_CMD`

Can be either a string (also a multiline string) or an array of strings.

The main difference is that when specifying an array, all args, specified in terminal will be appended to cmd array
Expand Down Expand Up @@ -485,3 +488,21 @@ You can use Bash/Zsh/Oh-My-Zsh completion in you terminal
* **Zsh/Oh-My-Zsh**

There is a [repo](https://github.com/lets-cli/lets-zsh-plugin) with zsh plugin with instructions

### Useful scrips

You can use this bash scripts in your own `lets.yaml` in case, if you wish to implement some functionality, that does not implemented yet in Lets

- run cmd only if checksum was changed
> Do't forget to add `.lets/` into your `.gitignore `
```yaml
# lets.yaml
commands:
build:
cmd: scripts/run-if-checksum-changed.sh "docker build ."
# |-------path to bash script------|---cmd to run---|
run:
depends:
- build
cmd: docker-compose up
```
10 changes: 6 additions & 4 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package commands

import (
"fmt"
"github.com/docopt/docopt-go"
"github.com/lets-cli/lets/commands/command"
"github.com/lets-cli/lets/config"
"github.com/lets-cli/lets/logging"
"io"
"os"
"os/exec"
"strings"

"github.com/docopt/docopt-go"
"github.com/lets-cli/lets/commands/command"
"github.com/lets-cli/lets/config"
"github.com/lets-cli/lets/logging"
)

const (
Expand Down Expand Up @@ -112,6 +113,7 @@ func runCmd(cmdToRun command.Command, cfg *config.Config, out io.Writer, parentN
convertEnvMapToList(cmdToRun.CliOptions),
convertChecksumToEnvForCmd(cmdToRun.Checksum),
convertChecksumMapToEnvForCmd(cmdToRun.ChecksumMap),
[]string{makeEnvEntry("LETS_CMD", cmdToRun.Name)},
)
if !isChildCmd {
logging.Log.Debugf(
Expand Down
24 changes: 24 additions & 0 deletions useful-scripts/run-if-checksum-changed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

CMD=$1
NEW_CHECKSUM=$LETS_CHECKSUM
LABEL=$LETS_CMD
LETS_DIR=.lets

# ensure lets dir exists
if [[ ! -d "$LETS_DIR" ]]; then
mkdir $LETS_DIR
fi;
# ensure checksum file exists
if [[ ! -f "$LETS_DIR/$LABEL" ]]; then
touch $LETS_DIR/$LABEL
fi;

# read
PREV_CHECKSUM=$(head -n 1 $LETS_DIR/$LABEL)

if [[ "$NEW_CHECKSUM" != "$PREV_CHECKSUM" ]]; then
# update checksum
echo $NEW_CHECKSUM > $LETS_DIR/$LABEL
eval $CMD
fi;