Skip to content

Commit

Permalink
Merge pull request #176 from lets-cli/upgrade-to-go-1.18
Browse files Browse the repository at this point in the history
go1.18
  • Loading branch information
kindermax authored Mar 21, 2022
2 parents f146ff6 + 2d6b0b5 commit e5f79b6
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 142 deletions.
45 changes: 21 additions & 24 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

name: Test
jobs:
test:
test-unit:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
Expand All @@ -20,39 +20,36 @@ jobs:
if: runner.os == 'macOS'
run: brew install bash
- name: Setup go
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: 1.17.x
go-version: 1.18.x
- name: Checkout code
uses: actions/checkout@v2
- name: Test unit
env:
LETS_CONFIG_DIR: ..
run: go test ./... -v

test-bats:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Lets
uses: lets-cli/[email protected]
with:
version: latest
- name: Test bats
env:
NO_COLOR: 1
run: |
git clone git://github.com/bats-core/bats-core
git clone git://github.com/bats-core/bats-support.git bats-core/bats-support
git clone git://github.com/bats-core/bats-assert.git bats-core/bats-assert
cd bats-core
./install.sh ../
cd ../
PATH=${PATH}:$(pwd)
go build -o lets *.go
BATS_UTILS_PATH=./bats-core ./bin/bats tests
run: lets test-bats

lint:
runs-on: ubuntu-latest
steps:
- name: Setup go
uses: actions/setup-go@v1
with:
go-version: 1.17.x
- name: Checkout code
- name: Checkout
uses: actions/checkout@v2
- name: Lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin v1.37.0
./bin/golangci-lint run -v -c .golangci.yaml
- name: Install Lets
uses: lets-cli/[email protected]
with:
version: latest
- name: Run lint
run: lets lint
11 changes: 10 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
run:
tests: false
go: 1.18

linters:
enable-all: true
disable:
- typecheck
- containedctx
- gochecknoglobals
- goimports
- funlen
Expand All @@ -19,6 +22,7 @@ linters:
- cyclop
- gocyclo
- gocognit
- tagliatelle

linters-settings:
gomnd:
Expand All @@ -27,9 +31,14 @@ linters-settings:
checks: case,condition,return
lll:
line-length: 120
varnamelen:
min-name-length: 2

issues:
exclude-rules:
- path: _test\.go
linters:
- gomnd
- gomnd
- path: set\.go
linters:
- typecheck
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ commands:

build-lint-image:
description: Build lets lint docker image
cmd: docker build -t lets-lint -f docker/Dockerfile.lint .
cmd: docker build -t lets-lint -f docker/lint.Dockerfile .
4 changes: 2 additions & 2 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,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) {
filesSet := set.NewStringSet()
filesSet := set.NewSet[string]()

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

filesSet.AddMany(matches)
filesSet.Add(matches...)
}
// sort files list
files := filesSet.ToList()
Expand Down
8 changes: 4 additions & 4 deletions config/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ var (
)

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

Expand Down
4 changes: 2 additions & 2 deletions config/parser/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
ARGS = "args"
)

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

// parseCommand parses and validates unmarshaled yaml.
func parseCommand(newCmd *config.Command, rawCommand map[string]interface{}, cfg *config.Config) error {
Expand Down
8 changes: 4 additions & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
FROM golang:1.17.5-buster
FROM golang:1.18-buster

ENV GOPROXY https://proxy.golang.org
WORKDIR /app

RUN apt-get update && apt-get install git gcc

RUN cd /tmp && \
git clone git://github.com/bats-core/bats-core && \
git clone git://github.com/bats-core/bats-support.git /bats/bats-support && \
git clone git://github.com/bats-core/bats-assert.git /bats/bats-assert && \
git clone https://github.com/bats-core/bats-core && \
git clone https://github.com/bats-core/bats-support.git /bats/bats-support && \
git clone https://github.com/bats-core/bats-assert.git /bats/bats-assert && \
cd bats-core && \
./install.sh /usr && \
echo Bats installed
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.lint → docker/lint.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM golangci/golangci-lint:v1.37-alpine
FROM golangci/golangci-lint:v1.45-alpine

RUN mkdir -p /.cache && chmod -R 777 /.cache
6 changes: 0 additions & 6 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import (
"strconv"
)

// GetConfigPathFromEnv return config file name and config dir
// LETS_CONFIG_DIR convenient to use in tests or when you want to run lets in another dir.
func GetConfigPathFromEnv() (string, string) {
return os.Getenv("LETS_CONFIG"), os.Getenv("LETS_CONFIG_DIR")
}

// IsDebug checks LETS_DEBUG env. If set to true or 1 - we in debug mode.
func IsDebug() bool {
debug, err := strconv.ParseBool(os.Getenv("LETS_DEBUG"))
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/lets-cli/lets

go 1.17
go 1.18

require (
github.com/codeclysm/extract v2.2.0+incompatible
Expand All @@ -18,7 +18,7 @@ require (
github.com/juju/testing v0.0.0-20201216035041-2be42bba85f3 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
40 changes: 13 additions & 27 deletions set/set.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
package set

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

func (s *StringSet) Add(value string) {
s.entryMap[value] = struct{}{}
}
type Set[T comparable] map[T]struct{}

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

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

return values
}

func (s *StringSet) Remove(value string) {
delete(s.entryMap, value)
func (s Set[T]) Remove(value T) {
delete(s, value)
}

func (s *StringSet) Contains(value string) bool {
_, c := s.entryMap[value]
func (s Set[T]) Contains(value T) bool {
_, c := s[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)
func NewSet[T comparable](values ...T) Set[T] {
set := make(Set[T])
set.Add(values...)

return set
}
36 changes: 24 additions & 12 deletions set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"testing"
)

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

set.Add("a")
set.Add("b")
Expand All @@ -22,9 +22,9 @@ func TestStringSet(t *testing.T) {
}
})
t.Run("add many strings at once to set", func(t *testing.T) {
set := NewStringSet()
set := NewSet[string]()

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

values := set.ToList()
Expand All @@ -35,11 +35,9 @@ func TestStringSet(t *testing.T) {
})

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

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

values := set.ToList()
Expand All @@ -50,14 +48,28 @@ func TestStringSet(t *testing.T) {
})

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

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

if !set.Contains("c") {
t.Errorf("set must contain element which was added, got: %s", set.ToList())
}
})
}

func TestIntSet(t *testing.T) {
t.Run("add int to set", func(t *testing.T) {
set := NewSet[int]()

set.Add(1)
set.Add(2)
set.Add(2)

values := set.ToList()
sort.Ints(values)
if !reflect.DeepEqual(values, []int{1, 2}) {
t.Errorf("set must contain only unique elements, got: %v", values)
}
})
}
Loading

0 comments on commit e5f79b6

Please sign in to comment.