Skip to content

Commit

Permalink
refactor: make config options be consistent with the cli style (#22)
Browse files Browse the repository at this point in the history
* refactor: make config options be consistent with the cli style

* refactor: remove Config struct

* refactor: postpone config process to run

Signed-off-by: Timon Wong <timon86.wang@gmail.com>

* revert some changes

Signed-off-by: Timon Wong <timon86.wang@gmail.com>

* chore: improve coverage

Signed-off-by: Timon Wong <timon86.wang@gmail.com>

* chore: fix lint

Signed-off-by: Timon Wong <timon86.wang@gmail.com>

* chore: Add a codecov file

Signed-off-by: Timon Wong <timon86.wang@gmail.com>

Signed-off-by: Timon Wong <timon86.wang@gmail.com>
Co-authored-by: Timon Wong <timon86.wang@gmail.com>
  • Loading branch information
ttys3 and timonwong committed Aug 29, 2022
1 parent ec4e3b8 commit ac84b09
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 217 deletions.
16 changes: 16 additions & 0 deletions .codecov.yml
@@ -0,0 +1,16 @@
coverage:
range: 70..90 # green if 90+, red if 70-
status:
patch:
# coverage status for pull request diff
default:
threshold: 0.5% # allow a little drop
project:
# coverage status for whole project
default:
target: auto # use coverage of base commit as target
threshold: 0.5% # allow a little drop

ignore:
- "plugin/**"
- "cmd/**"
22 changes: 0 additions & 22 deletions config.go

This file was deleted.

75 changes: 0 additions & 75 deletions flags.go

This file was deleted.

82 changes: 0 additions & 82 deletions flags_test.go

This file was deleted.

9 changes: 0 additions & 9 deletions rules/rules.go → internal/rules/rules.go
Expand Up @@ -16,15 +16,6 @@ var ErrInvalidRule = errors.New("invalid rule format")

type RulesetList []Ruleset

func (rl RulesetList) HasName(name string) bool {
for _, rs := range rl {
if rs.Name == name {
return true
}
}
return false
}

func (rl RulesetList) Names() []string {
keys := make([]string, len(rl))
visited := make(map[string]struct{})
Expand Down
File renamed without changes.
26 changes: 24 additions & 2 deletions sets/stringset.go → internal/sets/string.go
@@ -1,12 +1,15 @@
package sets

import "sort"
import (
"sort"
"strings"
)

type Empty struct{}

type StringSet map[string]Empty

func NewStringSet(items ...string) StringSet {
func NewString(items ...string) StringSet {
s := make(StringSet)
s.Insert(items...)
return s
Expand Down Expand Up @@ -35,3 +38,22 @@ func (s StringSet) List() []string {
sort.Strings(res)
return res
}

// Set implements flag.Value interface.
func (s *StringSet) Set(v string) error {
v = strings.TrimSpace(v)
if v == "" {
*s = nil
return nil
}

parts := strings.Split(v, ",")
set := NewString(parts...)
*s = set
return nil
}

// String implements flag.Value interface
func (s StringSet) String() string {
return strings.Join(s.List(), ",")
}
61 changes: 61 additions & 0 deletions internal/sets/string_test.go
@@ -0,0 +1,61 @@
package sets

import (
"flag"
"io"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestString(t *testing.T) {
t.Parallel()

set := NewString("logr", "logr", "klog")
assert.Equal(t, []string{"klog", "logr"}, set.List())
assert.Equal(t, "klog,logr", set.String())
assert.True(t, set.Has("logr"))
assert.True(t, set.Has("klog"))
assert.False(t, set.Has("zap"))
}

func TestString_Flag(t *testing.T) {
testCases := []struct {
name string
flagValue string
want []string
}{
{
name: "empty",
flagValue: "",
want: nil,
},
{
name: "klog",
flagValue: "klog",
want: []string{"klog"},
},
{
name: "klog-and-logr",
flagValue: "logr,klog",
want: []string{"klog", "logr"},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

f := StringSet{}
fs := flag.NewFlagSet("test", flag.ContinueOnError)
fs.SetOutput(io.Discard)
fs.Var(&f, "set", "")

err := fs.Parse([]string{"-set=" + tc.flagValue})
require.NoError(t, err)
assert.Equal(t, tc.want, f.List())
})
}
}

0 comments on commit ac84b09

Please sign in to comment.