Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Merged
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
108 changes: 53 additions & 55 deletions cmd/cli/flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ import (

func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {

name := ""
var flavorPlugin flavor.Plugin

cmd := &cobra.Command{
Use: "flavor",
Short: "Access flavor plugin",
PersistentPreRunE: func(c *cobra.Command, args []string) error {
}
name := cmd.PersistentFlags().String("name", "", "Name of plugin")
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this really need to be a PersistentFlags()?
It doesn't seem that the name is passed down to any sub-children, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does need to be, as you want to specify a plugin name for most of the subcommands being used. IMHO it really should be a positional (required) argument rather than a flag, but that's not something i'm willing to tackle in this patch.


endpoint, err := plugins().Find(name)
if err != nil {
return err
}
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {

endpoint, err := plugins().Find(*name)
if err != nil {
return err
}

flavorPlugin = flavor_plugin.NewClient(endpoint.Address)
flavorPlugin = flavor_plugin.NewClient(endpoint.Address)

return nil
},
return nil
}
cmd.PersistentFlags().StringVar(&name, "name", name, "Name of plugin")

logicalIDs := []string{}
groupSize := uint(0)
Expand Down Expand Up @@ -86,6 +86,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
},
}
addAllocationMethodFlags(validate)
cmd.AddCommand(validate)

prepare := &cobra.Command{
Use: "prepare <flavor configuration file> <instance Spec JSON file>",
Expand Down Expand Up @@ -129,61 +130,58 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
},
}
addAllocationMethodFlags(prepare)
cmd.AddCommand(prepare)

tags := []string{}
id := ""
logicalID := ""
healthy := &cobra.Command{
Use: "healthy <flavor configuration file>",
Short: "checks if an instance is considered healthy",
RunE: func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", flavorPlugin)

if len(args) != 1 {
cmd.Usage()
os.Exit(1)
}
}
tags := healthy.Flags().StringSlice("tags", []string{}, "Tags to filter")
id := healthy.Flags().String("id", "", "ID of resource")
logicalID := healthy.Flags().String("logical-id", "", "Logical ID of resource")
healthy.RunE = func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", flavorPlugin)

if len(args) != 1 {
cmd.Usage()
os.Exit(1)
}

flavorProperties, err := ioutil.ReadFile(args[0])
if err != nil {
log.Error(err)
os.Exit(1)
}
flavorProperties, err := ioutil.ReadFile(args[0])
if err != nil {
log.Error(err)
os.Exit(1)
}

filter := map[string]string{}
for _, t := range tags {
p := strings.Split(t, "=")
if len(p) == 2 {
filter[p[0]] = p[1]
} else {
filter[p[0]] = ""
}
filter := map[string]string{}
for _, t := range *tags {
p := strings.Split(t, "=")
if len(p) == 2 {
filter[p[0]] = p[1]
} else {
filter[p[0]] = ""
}
}

desc := instance.Description{}
if len(filter) > 0 {
desc.Tags = filter
}
if id != "" {
desc.ID = instance.ID(id)
}
if logicalID != "" {
logical := instance.LogicalID(logicalID)
desc.LogicalID = &logical
}
desc := instance.Description{}
if len(filter) > 0 {
desc.Tags = filter
}
if *id != "" {
desc.ID = instance.ID(*id)
}
if *logicalID != "" {
logical := instance.LogicalID(*logicalID)
desc.LogicalID = &logical
}

healthy, err := flavorPlugin.Healthy(json.RawMessage(flavorProperties), desc)
if err == nil {
fmt.Printf("%v\n", healthy)
}
return err
},
healthy, err := flavorPlugin.Healthy(json.RawMessage(flavorProperties), desc)
if err == nil {
fmt.Printf("%v\n", healthy)
}
return err
}
healthy.Flags().StringSliceVar(&tags, "tags", tags, "Tags to filter")
healthy.Flags().StringVar(&id, "id", id, "ID of resource")
healthy.Flags().StringVar(&logicalID, "logical-id", logicalID, "Logical ID of resource")

cmd.AddCommand(validate, prepare, healthy)
cmd.AddCommand(healthy)

return cmd
}
99 changes: 48 additions & 51 deletions cmd/cli/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,25 @@ const (

func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {

name := DefaultGroupPluginName
var groupPlugin group.Plugin

cmd := &cobra.Command{
Use: "group",
Short: "Access group plugin",
PersistentPreRunE: func(c *cobra.Command, args []string) error {
}
name := cmd.PersistentFlags().String("name", DefaultGroupPluginName, "Name of plugin")
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {

endpoint, err := plugins().Find(name)
if err != nil {
return err
}
endpoint, err := plugins().Find(*name)
if err != nil {
return err
}

groupPlugin = group_plugin.NewClient(endpoint.Address)
groupPlugin = group_plugin.NewClient(endpoint.Address)

return nil
},
return nil
}

cmd.PersistentFlags().StringVar(&name, "name", name, "Name of plugin")

commit := cobra.Command{
Use: "commit <group configuration>",
Short: "commit a group configuration",
Expand Down Expand Up @@ -99,44 +97,43 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
},
})

var quiet bool
describe := &cobra.Command{
Use: "describe <group ID>",
Short: "describe the live instances that make up a group",
RunE: func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", groupPlugin)
}
quietDescribe := describe.Flags().BoolP("quiet", "q", false, "Print rows without column headers")
describe.RunE = func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", groupPlugin)

if len(args) != 1 {
cmd.Usage()
os.Exit(1)
}
if len(args) != 1 {
cmd.Usage()
os.Exit(1)
}

groupID := group.ID(args[0])
desc, err := groupPlugin.DescribeGroup(groupID)
groupID := group.ID(args[0])
desc, err := groupPlugin.DescribeGroup(groupID)

if err == nil {
if !quiet {
fmt.Printf("%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS")
if err == nil {
if !*quietDescribe {
fmt.Printf("%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS")
}
for _, d := range desc.Instances {
logical := " - "
if d.LogicalID != nil {
logical = string(*d.LogicalID)
}
for _, d := range desc.Instances {
logical := " - "
if d.LogicalID != nil {
logical = string(*d.LogicalID)
}

printTags := []string{}
for k, v := range d.Tags {
printTags = append(printTags, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(printTags)

fmt.Printf("%-30s\t%-30s\t%-s\n", d.ID, logical, strings.Join(printTags, ","))
printTags := []string{}
for k, v := range d.Tags {
printTags = append(printTags, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(printTags)

fmt.Printf("%-30s\t%-30s\t%-s\n", d.ID, logical, strings.Join(printTags, ","))
}
return err
},
}
return err
}
describe.Flags().BoolVarP(&quiet, "quiet", "q", false, "Print rows without column headers")
cmd.AddCommand(describe)

cmd.AddCommand(&cobra.Command{
Expand Down Expand Up @@ -198,23 +195,23 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
describeGroups := &cobra.Command{
Use: "ls",
Short: "list groups",
RunE: func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", groupPlugin)
}
quietls := describeGroups.Flags().BoolP("quiet", "q", false, "Print rows without column headers")
describeGroups.RunE = func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", groupPlugin)

groups, err := groupPlugin.InspectGroups()
if err == nil {
if !quiet {
fmt.Printf("%s\n", "ID")
}
for _, g := range groups {
fmt.Printf("%s\n", g.ID)
}
groups, err := groupPlugin.InspectGroups()
if err == nil {
if !*quietls {
fmt.Printf("%s\n", "ID")
}
for _, g := range groups {
fmt.Printf("%s\n", g.ID)
}
}

return err
},
return err
}
describeGroups.Flags().BoolVarP(&quiet, "quiet", "q", false, "Print rows without column headers")
cmd.AddCommand(describeGroups)

return cmd
Expand Down
Loading