Skip to content

Commit

Permalink
Merge pull request #169 from lets-cli/fix-duplicate-files-for-checksum
Browse files Browse the repository at this point in the history
fix duplicated files for checksum
  • Loading branch information
kindermax authored Feb 16, 2022
2 parents 9b6b6eb + 7445276 commit ff6b775
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 36 deletions.
6 changes: 4 additions & 2 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"sort"

"github.com/lets-cli/lets/set"
"github.com/lets-cli/lets/util"
)

Expand All @@ -26,7 +27,7 @@ var checksumCache = make(map[string][]byte)
//
// return sorted list of files read by glob patterns.
func readFilesFromPatterns(workDir string, patterns []string) ([]string, error) {
var files []string
filesSet := set.NewStringSet()

for _, pattern := range patterns {
absPatternPath := pattern
Expand All @@ -39,9 +40,10 @@ func readFilesFromPatterns(workDir string, patterns []string) ([]string, error)
return []string{}, fmt.Errorf("can not read file to calculate checksum: %w", err)
}

files = append(files, matches...)
filesSet.AddMany(matches)
}
// sort files list
files := filesSet.ToList()
sort.Strings(files)

return files, nil
Expand Down
10 changes: 8 additions & 2 deletions config/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "github.com/lets-cli/lets/set"

var (
// COMMANDS is a top-level directive. Includes all commands to run.
COMMANDS = "commands"
Expand All @@ -12,8 +14,12 @@ var (
)

var (
ValidConfigFields = []string{COMMANDS, SHELL, ENV, EvalEnv, MIXINS, VERSION, BEFORE}
ValidMixinConfigFields = []string{COMMANDS, ENV, EvalEnv, BEFORE}
ValidConfigDirectives = set.NewStringSetWithValues(
[]string{COMMANDS, SHELL, ENV, EvalEnv, MIXINS, VERSION, BEFORE},
)
ValidMixinConfigDirectives = set.NewStringSetWithValues(
[]string{COMMANDS, ENV, EvalEnv, BEFORE},
)
)

// Config is a struct for loaded config file.
Expand Down
12 changes: 6 additions & 6 deletions config/parser/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/lets-cli/lets/config/config"
"github.com/lets-cli/lets/util"
"github.com/lets-cli/lets/set"
log "github.com/sirupsen/logrus"
)

Expand All @@ -24,7 +24,7 @@ var (
ARGS = "args"
)

var validFields = []string{
var directives = set.NewStringSetWithValues([]string{
CMD,
DESCRIPTION,
WORKDIR,
Expand All @@ -38,11 +38,11 @@ var validFields = []string{
AFTER,
REF,
ARGS,
}
})

// parseCommand parses and validates unmarshaled yaml.
func parseCommand(newCmd *config.Command, rawCommand map[string]interface{}, cfg *config.Config) error {
if err := validateCommandFields(rawCommand, validFields); err != nil {
if err := validateCommendDirectives(rawCommand); err != nil {
return err
}

Expand Down Expand Up @@ -158,9 +158,9 @@ func parseCommand(newCmd *config.Command, rawCommand map[string]interface{}, cfg
return nil
}

func validateCommandFields(rawKeyValue map[string]interface{}, validFields []string) error {
func validateCommendDirectives(rawKeyValue map[string]interface{}) error {
for key := range rawKeyValue {
if !util.IsStringInList(key, validFields) {
if !directives.Contains(key) {
return fmt.Errorf("unknown command field '%s'", key)
}
}
Expand Down
18 changes: 6 additions & 12 deletions config/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,13 @@ func parseConfigGeneral(rawKeyValue map[string]interface{}, cfg *config.Config)
return nil
}

func validateTopLevelFields(rawKeyValue map[string]interface{}, validFields []string) error {
func parseConfig(rawKeyValue map[string]interface{}, cfg *config.Config) error {
for key := range rawKeyValue {
if !util.IsStringInList(key, validFields) {
if !config.ValidConfigDirectives.Contains(key) {
return fmt.Errorf("unknown top-level field '%s'", key)
}
}

return nil
}

func parseConfig(rawKeyValue map[string]interface{}, cfg *config.Config) error {
if err := validateTopLevelFields(rawKeyValue, config.ValidConfigFields); err != nil {
return err
}

if err := parseConfigGeneral(rawKeyValue, cfg); err != nil {
return err
}
Expand Down Expand Up @@ -243,8 +235,10 @@ func parseMixinConfig(data []byte, mixinCfg *config.Config) error {
return fmt.Errorf("can not decode mixin config file: %w", err)
}

if err := validateTopLevelFields(rawKeyValue, config.ValidMixinConfigFields); err != nil {
return err
for key := range rawKeyValue {
if !config.ValidMixinConfigDirectives.Contains(key) {
return fmt.Errorf("unknown top-level field '%s'", key)
}
}

return parseConfigGeneral(rawKeyValue, mixinCfg)
Expand Down
7 changes: 5 additions & 2 deletions docs/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ title: Changelog

## [Unreleased]

* `[Fixed]` **`Breaking change`** Fix duplicate files for checksum.
This will change checksum output if the same file has been read multiple times.

## [0.0.44](https://github.com/lets-cli/lets/releases/tag/v0.0.44)

* ``[Fixed]`` Run ref declared in `depends` directive.
* `[Fixed]` Run ref declared in `depends` directive.

## [0.0.43](https://github.com/lets-cli/lets/releases/tag/v0.0.43)

* `[Noop]` Same as 0.0.42, deployed by accident.

## [0.0.42](https://github.com/lets-cli/lets/releases/tag/v0.0.42)

* ``[Fixed]`` Fixed publish to `aur` repository.
* `[Fixed]` Fixed publish to `aur` repository.

## [0.0.41](https://github.com/lets-cli/lets/releases/tag/v0.0.41)

Expand Down
49 changes: 49 additions & 0 deletions set/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package set

type StringSet struct {
entryMap map[string]struct{}
}

func (s *StringSet) Add(value string) {
s.entryMap[value] = struct{}{}
}

func (s *StringSet) AddMany(values []string) {
for _, value := range values {
s.entryMap[value] = struct{}{}
}
}

func (s *StringSet) ToList() []string {
values := make([]string, 0, len(s.entryMap))
for k := range s.entryMap {
values = append(values, k)
}

return values
}

func (s *StringSet) Remove(value string) {
delete(s.entryMap, value)
}

func (s *StringSet) Contains(value string) bool {
_, c := s.entryMap[value]

return c
}

func NewStringSet() *StringSet {
return &StringSet{
entryMap: make(map[string]struct{}),
}
}

func NewStringSetWithValues(values []string) *StringSet {
set := &StringSet{
entryMap: make(map[string]struct{}),
}
set.AddMany(values)

return set
}
63 changes: 63 additions & 0 deletions set/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package set

import (
"reflect"
"sort"
"testing"
)

func TestStringSet(t *testing.T) {
t.Run("add string to set", func(t *testing.T) {
set := NewStringSet()

set.Add("a")
set.Add("b")
set.Add("a")
set.Add("c")

values := set.ToList()
sort.Strings(values)
if !reflect.DeepEqual(values, []string{"a", "b", "c"}) {
t.Errorf("set must contain only unique elements, got: %s", values)
}
})
t.Run("add many strings at once to set", func(t *testing.T) {
set := NewStringSet()

set.AddMany([]string{"a", "b", "c"})
set.Add("c")

values := set.ToList()
sort.Strings(values)
if !reflect.DeepEqual(values, []string{"a", "b", "c"}) {
t.Errorf("set must contain only unique elements, got: %s", values)
}
})

t.Run("remove string from set", func(t *testing.T) {
set := NewStringSet()

set.Add("a")
set.Add("b")
set.Add("c")
set.Remove("c")

values := set.ToList()
sort.Strings(values)
if !reflect.DeepEqual(values, []string{"a", "b"}) {
t.Errorf("set contains element which must be deleted, got: %s", values)
}
})

t.Run("remove string from set", func(t *testing.T) {
set := NewStringSet()

set.Add("a")
set.Add("b")
set.Add("c")

if !set.Contains("c") {
t.Errorf("set must contain element which was added, got: %s", set.ToList())
}
})
}
12 changes: 0 additions & 12 deletions util/list.go

This file was deleted.

0 comments on commit ff6b775

Please sign in to comment.