Skip to content

Commit

Permalink
Merge pull request #1895 from bartekpacia/update_ci
Browse files Browse the repository at this point in the history
Update GitHub Actions (bump versions, improve naming a bit)
  • Loading branch information
bartekpacia committed May 1, 2024
2 parents 955bc3e + 2df7fe4 commit 624c05a
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 126 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/golangci-lint.yml

This file was deleted.

49 changes: 49 additions & 0 deletions .github/workflows/lint.yml
@@ -0,0 +1,49 @@
name: Run lints

on:
push:
tags:
- v3.*
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
staticcheck:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.20"

- name: Clone repository
uses: actions/checkout@v4

- name: Set up staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run staticcheck
run: staticcheck ./...

golangci-lint:
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.20"

- name: Clone repository
uses: actions/checkout@v4

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v5
with:
version: latest
34 changes: 26 additions & 8 deletions .github/workflows/cli.yml → .github/workflows/test.yml
@@ -1,4 +1,5 @@
name: Run Tests
name: Run tests

on:
push:
branches:
Expand All @@ -8,20 +9,26 @@ on:
pull_request:
branches:
- main

permissions:
contents: read

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: [1.19.x, 1.20.x]

name: ${{ matrix.os }} @ Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}

steps:
- uses: actions/setup-go@v3
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

- name: Set PATH
run: echo "${GITHUB_WORKSPACE}/.local/bin" >>"${GITHUB_PATH}"
- uses: actions/checkout@v3
Expand All @@ -48,38 +55,49 @@ jobs:
name: test-docs
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
- uses: actions/setup-node@v3

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '16'
node-version: 16

- name: Set PATH
run: echo "${GITHUB_WORKSPACE}/.local/bin" >>"${GITHUB_PATH}"
- uses: actions/checkout@v3

- name: Clone repository
uses: actions/checkout@v4

- run: make ensure-gfmrun
- run: make gfmrun
env:
FLAGS: --walk docs/v3/
- run: make diffcheck

publish:
permissions:
contents: write
# TODO: switch once v3 is out of alpha {{
# if: startswith(github.ref, 'refs/tags/')
if: 'false'
if: false
# }}
name: publish
needs: [test-docs]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- run: make ensure-mkdocs
env:
FLAGS: --upgrade-pip

- run: make set-mkdocs-remote
env:
MKDOCS_REMOTE_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- run: make deploy-mkdocs
19 changes: 9 additions & 10 deletions command.go
Expand Up @@ -359,11 +359,11 @@ func (cmd *Command) ensureHelp() {
func (cmd *Command) parseArgsFromStdin() ([]string, error) {
type state int
const (
STATE_SEARCH_FOR_TOKEN state = -1
STATE_IN_STRING state = 0
stateSearchForToken state = -1
stateSearchForString state = 0
)

st := STATE_SEARCH_FOR_TOKEN
st := stateSearchForToken
linenum := 1
token := ""
args := []string{}
Expand All @@ -375,11 +375,11 @@ outer:
ch, _, err := breader.ReadRune()
if err == io.EOF {
switch st {
case STATE_SEARCH_FOR_TOKEN:
case stateSearchForToken:
if token != "--" {
args = append(args, token)
}
case STATE_IN_STRING:
case stateSearchForString:
// make sure string is not empty
for _, t := range token {
if !unicode.IsSpace(t) {
Expand All @@ -393,7 +393,7 @@ outer:
return nil, err
}
switch st {
case STATE_SEARCH_FOR_TOKEN:
case stateSearchForToken:
if unicode.IsSpace(ch) || ch == '"' {
if ch == '\n' {
linenum++
Expand All @@ -407,12 +407,12 @@ outer:
token = ""
}
if ch == '"' {
st = STATE_IN_STRING
st = stateSearchForString
}
continue
}
token += string(ch)
case STATE_IN_STRING:
case stateSearchForString:
if ch != '"' {
token += string(ch)
} else {
Expand All @@ -423,7 +423,7 @@ outer:
/*else {
//TODO. Should we pass in empty strings ?
}*/
st = STATE_SEARCH_FOR_TOKEN
st = stateSearchForToken
}
}
}
Expand Down Expand Up @@ -1181,7 +1181,6 @@ func hasCommand(commands []*Command, command *Command) bool {
}

func (cmd *Command) runFlagActions(ctx context.Context) error {

for _, fl := range cmd.appliedFlags {
isSet := false

Expand Down
6 changes: 3 additions & 3 deletions flag_bool.go
Expand Up @@ -37,7 +37,7 @@ func (cmd *Command) Bool(name string) bool {
// Below functions are to satisfy the ValueCreator interface

// Create creates the bool value
func (i boolValue) Create(val bool, p *bool, c BoolConfig) Value {
func (b boolValue) Create(val bool, p *bool, c BoolConfig) Value {
*p = val
if c.Count == nil {
c.Count = new(int)
Expand All @@ -49,8 +49,8 @@ func (i boolValue) Create(val bool, p *bool, c BoolConfig) Value {
}

// ToString formats the bool value
func (i boolValue) ToString(b bool) string {
return strconv.FormatBool(b)
func (b boolValue) ToString(value bool) string {
return strconv.FormatBool(value)
}

// Below functions are to satisfy the flag.Value interface
Expand Down
78 changes: 38 additions & 40 deletions flag_bool_with_inverse.go
Expand Up @@ -7,9 +7,7 @@ import (
"strings"
)

var (
DefaultInverseBoolPrefix = "no-"
)
var DefaultInverseBoolPrefix = "no-"

type BoolWithInverseFlag struct {
// The BoolFlag which the positive and negative flags are generated from
Expand All @@ -29,44 +27,42 @@ type BoolWithInverseFlag struct {
negDest *bool
}

func (s *BoolWithInverseFlag) Flags() []Flag {
return []Flag{s.positiveFlag, s.negativeFlag}
func (parent *BoolWithInverseFlag) Flags() []Flag {
return []Flag{parent.positiveFlag, parent.negativeFlag}
}

func (s *BoolWithInverseFlag) IsSet() bool {
return (*s.posCount > 0) || (s.positiveFlag.IsSet() || s.negativeFlag.IsSet())
func (parent *BoolWithInverseFlag) IsSet() bool {
return (*parent.posCount > 0) || (parent.positiveFlag.IsSet() || parent.negativeFlag.IsSet())
}

func (s *BoolWithInverseFlag) Value() bool {
return *s.posDest
func (parent *BoolWithInverseFlag) Value() bool {
return *parent.posDest
}

func (s *BoolWithInverseFlag) RunAction(ctx context.Context, cmd *Command) error {
if *s.negDest && *s.posDest {
return fmt.Errorf("cannot set both flags `--%s` and `--%s`", s.positiveFlag.Name, s.negativeFlag.Name)
func (parent *BoolWithInverseFlag) RunAction(ctx context.Context, cmd *Command) error {
if *parent.negDest && *parent.posDest {
return fmt.Errorf("cannot set both flags `--%s` and `--%s`", parent.positiveFlag.Name, parent.negativeFlag.Name)
}

if *s.negDest {
err := cmd.Set(s.positiveFlag.Name, "false")
if *parent.negDest {
err := cmd.Set(parent.positiveFlag.Name, "false")
if err != nil {
return err
}
}

if s.BoolFlag.Action != nil {
return s.BoolFlag.Action(ctx, cmd, s.Value())
if parent.BoolFlag.Action != nil {
return parent.BoolFlag.Action(ctx, cmd, parent.Value())
}

return nil
}

/*
initialize creates a new BoolFlag that has an inverse flag
consider a bool flag `--env`, there is no way to set it to false
this function allows you to set `--env` or `--no-env` and in the command action
it can be determined that BoolWithInverseFlag.IsSet()
*/
// Initialize creates a new BoolFlag that has an inverse flag
//
// consider a bool flag `--env`, there is no way to set it to false
// this function allows you to set `--env` or `--no-env` and in the command action
// it can be determined that BoolWithInverseFlag.IsSet().
func (parent *BoolWithInverseFlag) initialize() {
child := parent.BoolFlag

Expand Down Expand Up @@ -138,45 +134,47 @@ func (parent *BoolWithInverseFlag) inverseAliases() (aliases []string) {
return
}

func (s *BoolWithInverseFlag) Apply(set *flag.FlagSet) error {
if s.positiveFlag == nil {
s.initialize()
func (parent *BoolWithInverseFlag) Apply(set *flag.FlagSet) error {
if parent.positiveFlag == nil {
parent.initialize()
}

if err := s.positiveFlag.Apply(set); err != nil {
if err := parent.positiveFlag.Apply(set); err != nil {
return err
}

if err := s.negativeFlag.Apply(set); err != nil {
if err := parent.negativeFlag.Apply(set); err != nil {
return err
}

return nil
}

func (s *BoolWithInverseFlag) Names() []string {
func (parent *BoolWithInverseFlag) Names() []string {
// Get Names when flag has not been initialized
if s.positiveFlag == nil {
return append(s.BoolFlag.Names(), FlagNames(s.inverseName(), s.inverseAliases())...)
if parent.positiveFlag == nil {
return append(parent.BoolFlag.Names(), FlagNames(parent.inverseName(), parent.inverseAliases())...)
}

if *s.negDest {
return s.negativeFlag.Names()
if *parent.negDest {
return parent.negativeFlag.Names()
}

if *s.posDest {
return s.positiveFlag.Names()
if *parent.posDest {
return parent.positiveFlag.Names()
}

return append(s.negativeFlag.Names(), s.positiveFlag.Names()...)
return append(parent.negativeFlag.Names(), parent.positiveFlag.Names()...)
}

// String implements the standard Stringer interface.
//
// Example for BoolFlag{Name: "env"}
// --env (default: false) || --no-env (default: false)
func (s *BoolWithInverseFlag) String() string {
if s.positiveFlag == nil {
return fmt.Sprintf("%s || --%s", s.BoolFlag.String(), s.inverseName())
func (parent *BoolWithInverseFlag) String() string {
if parent.positiveFlag == nil {
return fmt.Sprintf("%s || --%s", parent.BoolFlag.String(), parent.inverseName())
}

return fmt.Sprintf("%s || %s", s.positiveFlag.String(), s.negativeFlag.String())
return fmt.Sprintf("%s || %s", parent.positiveFlag.String(), parent.negativeFlag.String())
}

0 comments on commit 624c05a

Please sign in to comment.