-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract http flags from consul/command pkg
This decouples us from an internal Consul package. We've removed flags that we aren't using.
- Loading branch information
Showing
20 changed files
with
537 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.