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 checksum_cmd directive to command #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ _lets
coverage.out
node_modules
TODO
.DS_Store
14 changes: 14 additions & 0 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"crypto/sha1"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"

"github.com/lets-cli/lets/set"
"github.com/lets-cli/lets/util"
Expand Down Expand Up @@ -99,6 +101,18 @@ func getChecksumsKeys(mapping map[string][]string) []string {
return keys
}

func CalculateChecksumFromCmd(shell string, workDir string, script string) (string, error) {
cmd := exec.Command(shell, "-c", script)

out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("can not calculate checksum from cmd: %s: %w", script, err)
}

res := string(out)
return strings.TrimSpace(res), nil
}

// CalculateChecksumFromSources calculates checksum from checksumSources.
func CalculateChecksumFromSources(workDir string, checksumSources map[string][]string) (map[string]string, error) {
checksumMap := make(map[string]string)
Expand Down
19 changes: 18 additions & 1 deletion config/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Command struct {
Depends *Deps
ChecksumMap map[string]string
PersistChecksum bool
ChecksumCmd string
// args from 'lets run --debug' will become [--debug]
Args []string

Expand Down Expand Up @@ -68,6 +69,7 @@ func (c *Command) UnmarshalYAML(unmarshal func(interface{}) error) error {
After string
Ref string
Checksum *Checksum
ChecksumCmd string `yaml:"checksum_cmd"`
PersistChecksum bool `yaml:"persist_checksum"`
}

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

if cmd.ChecksumCmd != "" {
c.ChecksumCmd = cmd.ChecksumCmd
}

c.PersistChecksum = cmd.PersistChecksum
if len(c.ChecksumSources) == 0 && c.PersistChecksum {
return errors.New("'persist_checksum' must be used with 'checksum'")
Expand Down Expand Up @@ -154,6 +160,7 @@ func (c *Command) Clone() *Command {
Depends: c.Depends.Clone(),
ChecksumMap: cloneMap(c.ChecksumMap),
PersistChecksum: c.PersistChecksum,
ChecksumCmd: c.ChecksumCmd,
ChecksumSources: cloneMapSlice(c.ChecksumSources),
persistedChecksums: cloneMap(c.persistedChecksums),
Args: cloneSlice(c.Args),
Expand Down Expand Up @@ -190,7 +197,17 @@ func (c *Command) Help() string {
return strings.TrimSuffix(buf.String(), "\n")
}

func (c *Command) ChecksumCalculator(workDir string) error {
func (c *Command) ChecksumCalculator(shell, workDir string) error {
if c.ChecksumCmd != "" {
checksumResult, err := checksum.CalculateChecksumFromCmd(shell, workDir, c.ChecksumCmd)
if err != nil {
return err
}
c.ChecksumMap = make(map[string]string, 1)
c.ChecksumMap[checksum.DefaultChecksumKey] = checksumResult
return nil
}

if len(c.ChecksumSources) == 0 {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (e *Executor) initCmd(ctx *Context) error {
}

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

Expand Down
4 changes: 4 additions & 0 deletions lets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ commands:
run-docs:
work_dir: docs
cmd: npm start

x:
checksum_cmd: echo xxx_checksum
cmd: echo checksum for x is ${LETS_CHECKSUM}