Skip to content

Commit

Permalink
command/debug: move external to the bottom, addr parses better
Browse files Browse the repository at this point in the history
  • Loading branch information
langmartin committed Aug 11, 2020
1 parent 0c5e16d commit 000c5bf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
82 changes: 48 additions & 34 deletions command/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ type DebugCommand struct {
cancel context.CancelFunc
}

type external struct {
tls *api.TLSConfig
addrVal string
auth string
ssl bool
tokenVal string
tokenFile string
}

const (
userAgent = "nomad debug"
)
Expand Down Expand Up @@ -632,31 +623,6 @@ func (c *DebugCommand) collectVault(dir, vault string) error {
return nil
}

func (c *external) addr(addr string) string {
if c.addrVal == "" {
return addr
}
if c.ssl && c.addrVal[0:5] != "https" {
return "https" + c.addrVal[4:]
}
return c.addrVal
}

func (c *external) token() string {
if c.tokenVal != "" {
return c.tokenVal
}

if c.tokenFile != "" {
bs, err := ioutil.ReadFile(c.tokenFile)
if err == nil {
return strings.TrimSpace(string(bs))
}
}

return ""
}

// writeBytes writes a file to the archive, recording it in the manifest
func (c *DebugCommand) writeBytes(dir, file string, data []byte) error {
path := filepath.Join(dir, file)
Expand Down Expand Up @@ -851,3 +817,51 @@ func argNodes(input string) []string {
}
return out
}

// external holds address configuration for Consul and Vault APIs
type external struct {
tls *api.TLSConfig
addrVal string
auth string
ssl bool
tokenVal string
tokenFile string
}

func (e *external) addr(defaultAddr string) string {
if e.addrVal == "" {
return defaultAddr
}

if !e.ssl {
if strings.HasPrefix(e.addrVal, "http:") {
return e.addrVal
}
return "http://" + e.addrVal
}

if strings.HasPrefix(e.addrVal, "https:") {
return e.addrVal
}

if strings.HasPrefix(e.addrVal, "http:") {
return "https:" + e.addrVal[5:]
}

return "https://" + e.addrVal
}

func (e *external) token() string {
if e.tokenVal != "" {
return e.tokenVal
}

if e.tokenFile != "" {
bs, err := ioutil.ReadFile(e.tokenFile)
if err == nil {
return strings.TrimSpace(string(bs))
}
}

return ""
}
6 changes: 6 additions & 0 deletions command/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func TestDebugUtils(t *testing.T) {

e = &external{addrVal: "http://127.0.0.1:8500", ssl: false}
require.Equal(t, "http://127.0.0.1:8500", e.addr("foo"))

e = &external{addrVal: "127.0.0.1:8500", ssl: false}
require.Equal(t, "http://127.0.0.1:8500", e.addr("foo"))

e = &external{addrVal: "127.0.0.1:8500", ssl: true}
require.Equal(t, "https://127.0.0.1:8500", e.addr("foo"))
}

func TestDebugFails(t *testing.T) {
Expand Down

0 comments on commit 000c5bf

Please sign in to comment.