Skip to content

Commit

Permalink
Merge pull request #170 from lets-cli/fix-ref-args
Browse files Browse the repository at this point in the history
Fix ref args
  • Loading branch information
kindermax committed Mar 21, 2022
2 parents ff6b775 + ccf964f commit 2d6b0b5
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 197 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
6 changes: 2 additions & 4 deletions cmd/subcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ func newCmdGeneric(command config.Command, conf *config.Config, out io.Writer) *
1,
)

commandToRun := command
if command.Ref != "" {
refCmd := command
commandToRun = conf.Commands[refCmd.Ref].FromRef(refCmd, conf)
command = conf.Commands[command.Ref].FromRef(command)
}

return runner.NewRunner(&commandToRun, conf, out).Execute(cmd.Context())
return runner.NewRunner(&command, conf, out).Execute(cmd.Context())
},
// we use docopt to parse flags on our own, so any flag is valid flag here
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
Expand Down
17 changes: 4 additions & 13 deletions config/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/lets-cli/lets/checksum"
)
Expand Down Expand Up @@ -59,7 +57,7 @@ type Command struct {
// ref is basically a command name to use with predefined args, env
Ref string
// can be specified only with ref
RefArgs string
RefArgs []string
}

// NewCommand creates new command struct.
Expand All @@ -78,20 +76,13 @@ func (cmd Command) WithArgs(args []string) Command {
return newCmd
}

func (cmd Command) FromRef(refCommand Command, cfg *Config) Command {
func (cmd Command) FromRef(refCommand Command) Command {
newCmd := cmd

// we have to expand env here on our own, since this args not came from users tty, and not expanded before lets
refArgsRaw := os.Expand(refCommand.RefArgs, func(key string) string {
return cfg.Env[key]
})

refArgs := strings.Split(refArgsRaw, " ")

if len(newCmd.Args) == 0 {
newCmd.Args = append([]string{cmd.Name}, refArgs...)
newCmd.Args = append([]string{cmd.Name}, refCommand.RefArgs...)
} else {
newCmd.Args = append(newCmd.Args, refArgs...)
newCmd.Args = append(newCmd.Args, refCommand.RefArgs...)
}

newCmd.CommandArgs = newCmd.Args[1:]
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
41 changes: 36 additions & 5 deletions config/parser/args.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
package parser

import (
"fmt"
"os"

"github.com/kballard/go-shellquote"
"github.com/lets-cli/lets/config/config"
)

func parseArgs(rawArgs interface{}, newCmd *config.Command) error {
args, ok := rawArgs.(string)
if !ok {
switch args := rawArgs.(type) {
case string:
argsList, err := shellquote.Split(args)
if err != nil {
return parseError(
"can not parse into args list",
newCmd.Name,
ARGS,
err.Error(),
)
}

newCmd.RefArgs = argsList
case []string:
newCmd.RefArgs = args
case []interface{}:
for _, arg := range args {
newCmd.RefArgs = append(newCmd.RefArgs, fmt.Sprintf("%s", arg))
}
default:
return parseError(
"must be a string",
"must be a string or a list of string",
newCmd.Name,
ARGS,
"",
)
}

newCmd.RefArgs = args

return nil
}

func postprocessRefArgs(cfg *config.Config) {
for _, cmd := range cfg.Commands {
for idx, arg := range cmd.RefArgs {
// we have to expand env here on our own, since this args not came from users tty, and not expanded before lets
cmd.RefArgs[idx] = os.Expand(arg, func(key string) string {
return cfg.Env[key]
})
}
}
}
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
42 changes: 22 additions & 20 deletions config/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,6 @@ func newConfigParseError(msg string, name string, field string) error {
}

func parseConfigGeneral(rawKeyValue map[string]interface{}, cfg *config.Config) error {
if cmds, ok := rawKeyValue[config.COMMANDS]; ok {
cmdsMap, ok := cmds.(map[string]interface{})
if !ok {
return newConfigParseError(
"must be a mapping",
config.COMMANDS,
"",
)
}

commands, err := parseCommands(cmdsMap, cfg)
if err != nil {
return err
}

for _, c := range commands {
cfg.Commands[c.Name] = c
}
}

rawEnv := make(map[string]interface{})

if env, ok := rawKeyValue[ENV]; ok {
Expand Down Expand Up @@ -123,6 +103,26 @@ func parseConfigGeneral(rawKeyValue map[string]interface{}, cfg *config.Config)
}
}

if cmds, ok := rawKeyValue[config.COMMANDS]; ok {
cmdsMap, ok := cmds.(map[string]interface{})
if !ok {
return newConfigParseError(
"must be a mapping",
config.COMMANDS,
"",
)
}

commands, err := parseCommands(cmdsMap, cfg)
if err != nil {
return err
}

for _, c := range commands {
cfg.Commands[c.Name] = c
}
}

return nil
}

Expand Down Expand Up @@ -176,6 +176,8 @@ func parseConfig(rawKeyValue map[string]interface{}, cfg *config.Config) error {
}
}

postprocessRefArgs(cfg)

return nil
}

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
4 changes: 4 additions & 0 deletions docs/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ title: Changelog

## [Unreleased]

## [0.0.45]

* `[Fixed]` **`Breaking change`** Fix duplicate files for checksum.
This will change checksum output if the same file has been read multiple times.
* `[Fixed]` Fix parsing for ref args when declared as string.
* `[Added]` ref `args` can be a list of string

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

Expand Down
Loading

0 comments on commit 2d6b0b5

Please sign in to comment.