Skip to content

Commit

Permalink
Extract http flags from consul/command pkg
Browse files Browse the repository at this point in the history
This decouples us from an internal Consul package. We've removed flags
that we aren't using.
  • Loading branch information
lkysow committed May 27, 2020
1 parent 845d7fc commit bfa7f46
Show file tree
Hide file tree
Showing 20 changed files with 537 additions and 46 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ require (
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/json-iterator/go v1.1.8 // indirect
github.com/kr/text v0.1.0
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a
github.com/mitchellh/cli v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/onsi/ginkgo v1.10.3 // indirect
github.com/onsi/gomega v1.7.1 // indirect
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275
Expand Down
7 changes: 3 additions & 4 deletions subcommand/acl-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"time"

"github.com/hashicorp/consul-k8s/subcommand"
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/mitchellh/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -23,7 +22,7 @@ type Command struct {
UI cli.Ui

flags *flag.FlagSet
k8s *k8sflags.K8SFlags
k8s *flags.K8SFlags
flagSecretName string
flagInitType string
flagNamespace string
Expand All @@ -49,7 +48,7 @@ func (c *Command) init() {
c.flags.StringVar(&c.flagTokenSinkFile, "token-sink-file", "",
"Optional filepath to write acl token")

c.k8s = &k8sflags.K8SFlags{}
c.k8s = &flags.K8SFlags{}
flags.Merge(c.flags, c.k8s.Flags())
c.help = flags.Usage(help, c.flags)
}
Expand Down
12 changes: 5 additions & 7 deletions subcommand/create-federation-secret/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"github.com/cenkalti/backoff"
"github.com/hashicorp/consul-k8s/subcommand"
"github.com/hashicorp/consul-k8s/subcommand/common"
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
corev1 "k8s.io/api/core/v1"
Expand All @@ -38,7 +37,7 @@ var retryInterval = 1 * time.Second
type Command struct {
UI cli.Ui
flags *flag.FlagSet
k8s *k8sflags.K8SFlags
k8s *flags.K8SFlags
http *flags.HTTPFlags

// flagExportReplicationToken controls whether we include the acl replication
Expand Down Expand Up @@ -90,12 +89,11 @@ func (c *Command) init() {
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")

c.help = flags.Usage(help, c.flags)
c.http = &flags.HTTPFlags{}
c.k8s = &k8sflags.K8SFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.k8s = &flags.K8SFlags{}
flags.Merge(c.flags, c.http.Flags())
flags.Merge(c.flags, c.k8s.Flags())
c.help = flags.Usage(help, c.flags)
}

// Run creates a Kubernetes secret with data needed by secondary datacenters
Expand Down
7 changes: 3 additions & 4 deletions subcommand/delete-completed-job/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"flag"
"fmt"
"github.com/hashicorp/consul-k8s/subcommand"
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
v1 "k8s.io/api/batch/v1"
Expand All @@ -24,7 +23,7 @@ type Command struct {
UI cli.Ui

flags *flag.FlagSet
k8s *k8sflags.K8SFlags
k8s *flags.K8SFlags
flagNamespace string
flagTimeout string

Expand All @@ -39,7 +38,7 @@ type Command struct {
func (c *Command) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)

c.k8s = &k8sflags.K8SFlags{}
c.k8s = &flags.K8SFlags{}
c.flags.StringVar(&c.flagNamespace, "k8s-namespace", "",
"Name of Kubernetes namespace where the job is deployed")
c.flags.StringVar(&c.flagTimeout, "timeout", "30m",
Expand Down
64 changes: 64 additions & 0 deletions subcommand/flags/flag_duration_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package flags

import (
"reflect"
"time"

"github.com/mitchellh/mapstructure"
)

// Taken from https://github.com/hashicorp/consul/blob/master/command/flags/flag_duration_value.go
// This was done so we don't depend on internal Consul implementation.

// DurationValue provides a flag value that's aware if it has been set.
type DurationValue struct {
v *time.Duration
}

// Merge will overlay this value if it has been set.
func (d *DurationValue) Merge(onto *time.Duration) {
if d.v != nil {
*onto = *(d.v)
}
}

// Set implements the flag.Value interface.
func (d *DurationValue) Set(v string) error {
if d.v == nil {
d.v = new(time.Duration)
}
var err error
*(d.v), err = time.ParseDuration(v)
return err
}

// String implements the flag.Value interface.
func (d *DurationValue) String() string {
var current time.Duration
if d.v != nil {
current = *(d.v)
}
return current.String()
}

// StringToDurationValueFunc is a mapstructure hook that looks for an incoming
// string mapped to a DurationValue and does the translation.
func StringToDurationValueFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}

val := DurationValue{}
if t != reflect.TypeOf(val) {
return data, nil
}
if err := val.Set(data.(string)); err != nil {
return nil, err
}
return val, nil
}
}
40 changes: 40 additions & 0 deletions subcommand/flags/flag_map_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package flags

import (
"flag"
"fmt"
"strings"
)

// Taken from https://github.com/hashicorp/consul/blob/master/command/flags/flag_map_value.go
// This was done so we don't depend on internal Consul implementation.

// Ensure implements
var _ flag.Value = (*FlagMapValue)(nil)

// FlagMapValue is a flag implementation used to provide key=value semantics
// multiple times.
type FlagMapValue map[string]string

func (h *FlagMapValue) String() string {
return fmt.Sprintf("%v", *h)
}

func (h *FlagMapValue) Set(value string) error {
idx := strings.Index(value, "=")
if idx == -1 {
return fmt.Errorf("Missing \"=\" value in argument: %s", value)
}

key, value := value[0:idx], value[idx+1:]

if *h == nil {
*h = make(map[string]string)
}

headers := *h
headers[key] = value
*h = headers

return nil
}
83 changes: 83 additions & 0 deletions subcommand/flags/flag_map_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package flags

import (
"fmt"
"testing"
)

// Taken from https://github.com/hashicorp/consul/blob/master/command/flags/flag_map_value_test.go
// This was done so we don't depend on internal Consul implementation.

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

t.Run("missing =", func(t *testing.T) {

f := new(FlagMapValue)
if err := f.Set("foo"); err == nil {
t.Fatal("expected error, got nil")
}
})

t.Run("sets", func(t *testing.T) {

f := new(FlagMapValue)
if err := f.Set("foo=bar"); err != nil {
t.Fatal(err)
}

r, ok := (*f)["foo"]
if !ok {
t.Errorf("missing value: %#v", f)
}
if exp := "bar"; r != exp {
t.Errorf("expected %q to be %q", r, exp)
}
})

t.Run("sets multiple", func(t *testing.T) {

f := new(FlagMapValue)

r := map[string]string{
"foo": "bar",
"zip": "zap",
"cat": "dog",
}

for k, v := range r {
if err := f.Set(fmt.Sprintf("%s=%s", k, v)); err != nil {
t.Fatal(err)
}
}

for k, v := range r {
r, ok := (*f)[k]
if !ok {
t.Errorf("missing value %q: %#v", k, f)
}
if exp := v; r != exp {
t.Errorf("expected %q to be %q", r, exp)
}
}
})

t.Run("overwrites", func(t *testing.T) {

f := new(FlagMapValue)
if err := f.Set("foo=bar"); err != nil {
t.Fatal(err)
}
if err := f.Set("foo=zip"); err != nil {
t.Fatal(err)
}

r, ok := (*f)["foo"]
if !ok {
t.Errorf("missing value: %#v", f)
}
if exp := "zip"; r != exp {
t.Errorf("expected %q to be %q", r, exp)
}
})
}
23 changes: 23 additions & 0 deletions subcommand/flags/flag_slice_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package flags

import "strings"

// Taken from https://github.com/hashicorp/consul/blob/master/command/flags/flag_slice_value.go
// This was done so we don't depend on internal Consul implementation.

// AppendSliceValue implements the flag.Value interface and allows multiple
// calls to the same variable to append a list.
type AppendSliceValue []string

func (s *AppendSliceValue) String() string {
return strings.Join(*s, ",")
}

func (s *AppendSliceValue) Set(value string) error {
if *s == nil {
*s = make([]string, 0, 1)
}

*s = append(*s, value)
return nil
}
38 changes: 38 additions & 0 deletions subcommand/flags/flag_slice_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package flags

import (
"flag"
"reflect"
"testing"
)

// Taken from https://github.com/hashicorp/consul/blob/master/command/flags/flag_slice_value_test.go
// This was done so we don't depend on internal Consul implementation.

func TestAppendSliceValue_implements(t *testing.T) {
t.Parallel()
var raw interface{}
raw = new(AppendSliceValue)
if _, ok := raw.(flag.Value); !ok {
t.Fatalf("AppendSliceValue should be a Value")
}
}

func TestAppendSliceValueSet(t *testing.T) {
t.Parallel()
sv := new(AppendSliceValue)
err := sv.Set("foo")
if err != nil {
t.Fatalf("err: %s", err)
}

err = sv.Set("bar")
if err != nil {
t.Fatalf("err: %s", err)
}

expected := []string{"foo", "bar"}
if !reflect.DeepEqual([]string(*sv), expected) {
t.Fatalf("Bad: %#v", sv)
}
}
2 changes: 2 additions & 0 deletions subcommand/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package flags holds common flags that are shared between our commands.
package flags
Loading

0 comments on commit bfa7f46

Please sign in to comment.