Skip to content

Commit

Permalink
Merge pull request #30 from ernoaapa/fix-discovery-bug
Browse files Browse the repository at this point in the history
Fix discovery bug
  • Loading branch information
ernoaapa authored Feb 15, 2018
2 parents b97fea4 + a61a267 commit 48b7e23
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
13 changes: 8 additions & 5 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func GetClient(config *config.Provider) *api.Client {
client := api.NewClient(config.GetNamespace(), endpoints[0])
info, err := client.GetInfo()
if err != nil {
logrus.Debugf("Connection failure: %s", err)
uiline.Fatalf("Failed connect to %s (%s)", endpoints[0].Name, endpoints[0].URL)
}
uiline.Donef("Connected to %s (%s)", info.Hostname, endpoints[0].URL)
Expand Down Expand Up @@ -138,10 +139,12 @@ func GetConfigProvider(clicontext *cli.Context) *config.Provider {

endpoints := []config.Endpoint{}
for _, device := range devices {
endpoints = append(endpoints, config.Endpoint{
Name: device.Hostname,
URL: utils.GetFirst(device.Addresses, ""),
})
if len(device.Addresses) > 0 {
endpoints = append(endpoints, config.Endpoint{
Name: device.Hostname,
URL: fmt.Sprintf("%s:%d", utils.GetFirst(device.Addresses, ""), device.GrpcPort),
})
}
}
provider.OverrideEndpoints(endpoints)
}
Expand Down Expand Up @@ -322,7 +325,7 @@ func expandTilde(path string) string {
usr, _ := user.Current()
dir := usr.HomeDir

if path[:2] == "~/" {
if len(path) >= 2 && path[:2] == "~/" {
return filepath.Join(dir, path[2:])
}
return path
Expand Down
16 changes: 16 additions & 0 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

containers "github.com/ernoaapa/eliot/pkg/api/services/containers/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -123,3 +124,18 @@ func TestEmptyResolveContainerIDFail(t *testing.T) {
_, err := ResolveContainerID([]*containers.ContainerStatus{}, "")
assert.Error(t, err)
}

func TestGetConfigProviderWithEndpointFlag(t *testing.T) {
flags := flag.NewFlagSet("test", 0)
flags.String("endpoint", "", "")

flags.Parse([]string{"--endpoint", "1.2.3.4:5000"})
clicontext := cli.NewContext(nil, flags, nil)

provider := GetConfigProvider(clicontext)

assert.Equal(t, []config.Endpoint{{
Name: "1.2.3.4:5000",
URL: "1.2.3.4:5000",
}}, provider.GetEndpoints(), "")
}
3 changes: 2 additions & 1 deletion pkg/printers/humanreadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (p *HumanReadablePrinter) PrintDevices(devices []*device.Info, writer io.Wr
fmt.Fprintln(writer, "\nHOSTNAME\tENDPOINT\tVERSION")

for _, device := range devices {
_, err := fmt.Fprintf(writer, "%s\t%s\t%s\n", device.Hostname, utils.GetFirst(device.Addresses, ""), device.Version)
endpoint := fmt.Sprintf("%s:%d", utils.GetFirst(device.Addresses, ""), device.GrpcPort)
_, err := fmt.Fprintf(writer, "%s\t%s\t%s\n", device.Hostname, endpoint, device.Version)
if err != nil {
return errors.Wrapf(err, "Error while writing device row")
}
Expand Down

0 comments on commit 48b7e23

Please sign in to comment.