Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions actions/search_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package action
import (
"context"
"fmt"
"strings"

"github.com/devdinu/gcloud-client/config"
"github.com/devdinu/gcloud-client/gcloud"
Expand Down Expand Up @@ -56,12 +57,27 @@ func (s searcher) searchInstances(c gcloud.Client, args config.Args, matcher sto

func (s searcher) formatInstances(insts []gcloud.Instance, args config.Args) error {
hostMapping := args.InstanceCmdArgs.HostMapping
ipOnly := args.InstanceCmdArgs.IPOnly

if hostMapping {
for _, ins := range insts {
fmt.Printf("%-16s %s\n", ins.IP(), ins.Name)
}
return nil
}

if ipOnly {
output := ""
sep := ","
for _, ins := range insts {
output += ins.IP() + sep
}
output = strings.TrimRight(output, ",")
fmt.Printf("%s", output)
fmt.Printf("\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can get the ip list with our custom type Instances []Instance with IPs defined on the struct.

ips := Instances(insts).IPs()
strings.Join(ips, ",")

return nil
}

for _, ins := range insts {
fmt.Printf("%s\n", ins)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. we should use bytes.Buffer
  2. ideally would prefer having custom type instances []instance and defining formatted outputs from it, since that's more extensible. you could refer projects

Expand Down
2 changes: 2 additions & 0 deletions config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type InstanceCmdArgs struct {
Regex string
Refresh bool
HostMapping bool
IPOnly bool
}

type Login struct {
Expand Down Expand Up @@ -71,6 +72,7 @@ func MustLoad() {
instanceCommand.StringVar(&instanceArgs.Prefix, "prefix", "", "search instances by common prefix")
instanceCommand.StringVar(&instanceArgs.Regex, "regex", "", "search instances by regex")
instanceCommand.BoolVar(&instanceArgs.HostMapping, "host_mapping", false, "return the search results in `/etc/hosts` file format")
instanceCommand.BoolVar(&instanceArgs.IPOnly, "ip_only", false, "returns comma separated list of IPs for matching hosts")
instanceCommand.StringVar(&args.Login.Session, "session", "login-session", "login sesssion name")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also mentioned host_mapping takes higher priority than ip_only

instanceCommand.StringVar(&args.Login.TemplatesDir, "templates", defaultCfg.TemplatesDir, "templates directory for tmuxinator")

Expand Down