From eacfb16a7849e0a55f9e833282b2f73e2035eef9 Mon Sep 17 00:00:00 2001 From: Kindritskiy Maksym Date: Mon, 7 Aug 2023 18:12:25 +0300 Subject: [PATCH] add checksum_cmd directive to command --- .gitignore | 1 + checksum/checksum.go | 14 ++++++++++++++ config/config/command.go | 19 ++++++++++++++++++- executor/executor.go | 2 +- lets.yaml | 4 ++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 56e00689..ead9e9ea 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ _lets coverage.out node_modules TODO +.DS_Store \ No newline at end of file diff --git a/checksum/checksum.go b/checksum/checksum.go index ef44a426..d437ab6c 100644 --- a/checksum/checksum.go +++ b/checksum/checksum.go @@ -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" @@ -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) diff --git a/config/config/command.go b/config/config/command.go index 444ae2ea..c23c638f 100644 --- a/config/config/command.go +++ b/config/config/command.go @@ -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 @@ -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"` } @@ -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'") @@ -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), @@ -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 } diff --git a/executor/executor.go b/executor/executor.go index eccc3415..dffb4a14 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -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) } diff --git a/lets.yaml b/lets.yaml index 2ff4ff19..b71308c8 100644 --- a/lets.yaml +++ b/lets.yaml @@ -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} \ No newline at end of file