-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy config parsing from Consul Template
- Loading branch information
Showing
8 changed files
with
1,010 additions
and
673 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,63 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// prefixVar implements the Flag.Value interface and allows the user | ||
// to specify multiple -prefix keys in the CLI where each option is parsed | ||
// as a dependency. | ||
type prefixVar []*Prefix | ||
// funcVar is a type of flag that accepts a function that is the string given | ||
// by the user. | ||
type funcVar func(s string) error | ||
|
||
func (pv *prefixVar) Set(value string) error { | ||
prefix, err := ParsePrefix(value) | ||
if err != nil { | ||
return err | ||
} | ||
func (f funcVar) Set(s string) error { return f(s) } | ||
func (f funcVar) String() string { return "" } | ||
func (f funcVar) IsBoolFlag() bool { return false } | ||
|
||
if *pv == nil { | ||
*pv = make([]*Prefix, 0, 1) | ||
} | ||
*pv = append(*pv, prefix) | ||
// funcBoolVar is a type of flag that accepts a function, converts the user's | ||
// value to a bool, and then calls the given function. | ||
type funcBoolVar func(b bool) error | ||
|
||
return nil | ||
} | ||
|
||
func (pv *prefixVar) String() string { | ||
list := make([]string, 0, len(*pv)) | ||
for _, prefix := range *pv { | ||
list = append(list, fmt.Sprintf("%s:%s", prefix.SourceRaw, prefix.Destination)) | ||
func (f funcBoolVar) Set(s string) error { | ||
v, err := strconv.ParseBool(s) | ||
if err != nil { | ||
return err | ||
} | ||
return strings.Join(list, ", ") | ||
return f(v) | ||
} | ||
func (f funcBoolVar) String() string { return "" } | ||
func (f funcBoolVar) IsBoolFlag() bool { return true } | ||
|
||
/// ------------------------- /// | ||
|
||
// authVar implements the Flag.Value interface and allows the user to specify | ||
// authentication in the username[:password] form. | ||
type authVar Auth | ||
// funcDurationVar is a type of flag that accepts a function, converts the | ||
// user's value to a duration, and then calls the given function. | ||
type funcDurationVar func(d time.Duration) error | ||
|
||
// Set sets the value for this authentication. | ||
func (a *authVar) Set(value string) error { | ||
a.Enabled = true | ||
|
||
if strings.Contains(value, ":") { | ||
split := strings.SplitN(value, ":", 2) | ||
a.Username = split[0] | ||
a.Password = split[1] | ||
} else { | ||
a.Username = value | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// String returns the string representation of this authentication. | ||
func (a *authVar) String() string { | ||
if a.Password == "" { | ||
return a.Username | ||
func (f funcDurationVar) Set(s string) error { | ||
v, err := time.ParseDuration(s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return fmt.Sprintf("%s:%s", a.Username, a.Password) | ||
return f(v) | ||
} | ||
func (f funcDurationVar) String() string { return "" } | ||
func (f funcDurationVar) IsBoolFlag() bool { return false } |
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 |
---|---|---|
@@ -1,16 +1,35 @@ | ||
package main // import "github.com/hashicorp/consul-replicate" | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Name is the exported name of this application. | ||
const Name = "consul-replicate" | ||
// The git commit that was compiled. This will be filled in by the compiler. | ||
var GitCommit string | ||
|
||
// Version is the current version of this application. | ||
const Version = "0.2.0.dev" | ||
const Name = "consul-replicate" | ||
const Version = "0.2.0" | ||
const VersionPrerelease = "dev" | ||
|
||
func main() { | ||
cli := NewCLI(os.Stdout, os.Stderr) | ||
os.Exit(cli.Run(os.Args)) | ||
} | ||
|
||
// formattedVersion returns a formatted version string which includes the git | ||
// commit and development information. | ||
func formattedVersion() string { | ||
var versionString bytes.Buffer | ||
fmt.Fprintf(&versionString, "%s v%s", Name, Version) | ||
|
||
if VersionPrerelease != "" { | ||
fmt.Fprintf(&versionString, "-%s", VersionPrerelease) | ||
|
||
if GitCommit != "" { | ||
fmt.Fprintf(&versionString, " (%s)", GitCommit) | ||
} | ||
} | ||
return versionString.String() | ||
} |
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