Skip to content

Commit eacfb16

Browse files
committed
add checksum_cmd directive to command
1 parent 568bc46 commit eacfb16

5 files changed

Lines changed: 38 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ _lets
2626
coverage.out
2727
node_modules
2828
TODO
29+
.DS_Store

checksum/checksum.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"crypto/sha1"
66
"fmt"
77
"os"
8+
"os/exec"
89
"path/filepath"
910
"sort"
11+
"strings"
1012

1113
"github.com/lets-cli/lets/set"
1214
"github.com/lets-cli/lets/util"
@@ -99,6 +101,18 @@ func getChecksumsKeys(mapping map[string][]string) []string {
99101
return keys
100102
}
101103

104+
func CalculateChecksumFromCmd(shell string, workDir string, script string) (string, error) {
105+
cmd := exec.Command(shell, "-c", script)
106+
107+
out, err := cmd.Output()
108+
if err != nil {
109+
return "", fmt.Errorf("can not calculate checksum from cmd: %s: %w", script, err)
110+
}
111+
112+
res := string(out)
113+
return strings.TrimSpace(res), nil
114+
}
115+
102116
// CalculateChecksumFromSources calculates checksum from checksumSources.
103117
func CalculateChecksumFromSources(workDir string, checksumSources map[string][]string) (map[string]string, error) {
104118
checksumMap := make(map[string]string)

config/config/command.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Command struct {
3333
Depends *Deps
3434
ChecksumMap map[string]string
3535
PersistChecksum bool
36+
ChecksumCmd string
3637
// args from 'lets run --debug' will become [--debug]
3738
Args []string
3839

@@ -68,6 +69,7 @@ func (c *Command) UnmarshalYAML(unmarshal func(interface{}) error) error {
6869
After string
6970
Ref string
7071
Checksum *Checksum
72+
ChecksumCmd string `yaml:"checksum_cmd"`
7173
PersistChecksum bool `yaml:"persist_checksum"`
7274
}
7375

@@ -109,6 +111,10 @@ func (c *Command) UnmarshalYAML(unmarshal func(interface{}) error) error {
109111
c.ChecksumSources = *cmd.Checksum
110112
}
111113

114+
if cmd.ChecksumCmd != "" {
115+
c.ChecksumCmd = cmd.ChecksumCmd
116+
}
117+
112118
c.PersistChecksum = cmd.PersistChecksum
113119
if len(c.ChecksumSources) == 0 && c.PersistChecksum {
114120
return errors.New("'persist_checksum' must be used with 'checksum'")
@@ -154,6 +160,7 @@ func (c *Command) Clone() *Command {
154160
Depends: c.Depends.Clone(),
155161
ChecksumMap: cloneMap(c.ChecksumMap),
156162
PersistChecksum: c.PersistChecksum,
163+
ChecksumCmd: c.ChecksumCmd,
157164
ChecksumSources: cloneMapSlice(c.ChecksumSources),
158165
persistedChecksums: cloneMap(c.persistedChecksums),
159166
Args: cloneSlice(c.Args),
@@ -190,7 +197,17 @@ func (c *Command) Help() string {
190197
return strings.TrimSuffix(buf.String(), "\n")
191198
}
192199

193-
func (c *Command) ChecksumCalculator(workDir string) error {
200+
func (c *Command) ChecksumCalculator(shell, workDir string) error {
201+
if c.ChecksumCmd != "" {
202+
checksumResult, err := checksum.CalculateChecksumFromCmd(shell, workDir, c.ChecksumCmd)
203+
if err != nil {
204+
return err
205+
}
206+
c.ChecksumMap = make(map[string]string, 1)
207+
c.ChecksumMap[checksum.DefaultChecksumKey] = checksumResult
208+
return nil
209+
}
210+
194211
if len(c.ChecksumSources) == 0 {
195212
return nil
196213
}

executor/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (e *Executor) initCmd(ctx *Context) error {
173173
}
174174

175175
// calculate checksum if needed
176-
if err := cmd.ChecksumCalculator(e.cfg.WorkDir); err != nil {
176+
if err := cmd.ChecksumCalculator(e.cfg.Shell, e.cfg.WorkDir); err != nil {
177177
return fmt.Errorf("failed to calculate checksum for command '%s': %w", cmd.Name, err)
178178
}
179179

lets.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,7 @@ commands:
115115
run-docs:
116116
work_dir: docs
117117
cmd: npm start
118+
119+
x:
120+
checksum_cmd: echo xxx_checksum
121+
cmd: echo checksum for x is ${LETS_CHECKSUM}

0 commit comments

Comments
 (0)