Skip to content

Commit

Permalink
add global includes and excludes options
Browse files Browse the repository at this point in the history
Adds `--include` and `--exclude` which can accept comma-separated
values or can be given multiple times to specify more
includes/excludes.

Naturally, dotted syntax (as supported by the API) can be used to
include/exclude deep values.

Default includes will not be used when their results are not rendered.

Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Aug 29, 2020
1 parent a99fc44 commit 642820b
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/retrieve_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ packet device get --id [device_UUID]
} else if deviceID == "" && projectID == "" {
return fmt.Errorf("Either id or project-id should be set.")
} else if projectID != "" {
devices, _, err := PacknGo.Devices.List(projectID, nil)
devices, _, err := PacknGo.Devices.List(projectID, listOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Devices")
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/retrieve_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@ packet event get -d [device_UUID]
Retrieve all events of a current user:
packet event get
When using "--json" or "--yaml", "--include=relationships" is implied.
`,
RunE: func(cmd *cobra.Command, args []string) error {
var events []packngo.Event
var err error
header := []string{"ID", "Body", "Type", "Created"}
listOpt := &packngo.ListOptions{Includes: []string{"relationships"}}

inc := []string{"relationships"}

// don't fetch extra details that won't be rendered
if !isYaml && !isJSON {
inc = nil
}
listOpt := listOptions(inc, nil)

if deviceID != "" && projectID != "" && organizationID != "" && eventID != "" {
return fmt.Errorf("The id, project-id, device-id, and organization-id parameters are mutually exclusive")
Expand Down
2 changes: 1 addition & 1 deletion cmd/retrieve_facilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ packet facilities get
`,
RunE: func(cmd *cobra.Command, args []string) error {
facilities, _, err := PacknGo.Facilities.List(nil)
facilities, _, err := PacknGo.Facilities.List(listOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Facilities")
}
Expand Down
15 changes: 12 additions & 3 deletions cmd/retrieve_hardware_reservations.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ var retrieveHardwareReservationsCmd = &cobra.Command{
Retrieve all hardware reservations of a project:
packet hardware_reservations get -p [project_id]
When using "--json" or "--yaml", "--include=project,facility,device" is implied.
`,
RunE: func(cmd *cobra.Command, args []string) error {
header := []string{"ID", "Facility", "Plan", "Created"}
listOpt := &packngo.ListOptions{Includes: []string{"project,facility,device"}}

inc := []string{"project", "facility", "device"}

// don't fetch extra details that won't be rendered
if !isYaml && !isJSON {
inc = nil
}
listOpt := listOptions(inc, nil)

if hardwareReservationID == "" && projectID == "" {
return fmt.Errorf("Either id or project-id should be set.")
Expand All @@ -59,8 +68,8 @@ packet hardware_reservations get -p [project_id]

return output(reservations, header, &data)
} else if hardwareReservationID != "" {
getOpt := &packngo.GetOptions{Includes: listOpt.Includes}
r, _, err := PacknGo.HardwareReservations.Get(hardwareReservationID, getOpt)
getOpts := &packngo.GetOptions{Includes: listOpt.Includes, Excludes: listOpt.Excludes}
r, _, err := PacknGo.HardwareReservations.Get(hardwareReservationID, getOpts)
if err != nil {
return errors.Wrap(err, "Could not get Hardware Reservation")
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/retrieve_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cmd

import (
"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -39,8 +40,9 @@ packet organization get -i [organization-id]
`,
RunE: func(cmd *cobra.Command, args []string) error {
listOpts := listOptions(nil, nil)
if organizationID == "" {
orgs, _, err := PacknGo.Organizations.List(nil)
orgs, _, err := PacknGo.Organizations.List(listOpts)
if err != nil {
return errors.Wrap(err, "Could not list Organizations")
}
Expand All @@ -54,7 +56,8 @@ packet organization get -i [organization-id]

return output(orgs, header, &data)
} else {
org, _, err := PacknGo.Organizations.Get(organizationID, nil)
getOpts := &packngo.GetOptions{Includes: listOpts.Includes, Excludes: listOpts.Excludes}
org, _, err := PacknGo.Organizations.Get(organizationID, getOpts)
if err != nil {
return errors.Wrap(err, "Could not get Organization")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/retrieve_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var retrievePlansCmd = &cobra.Command{
`,
RunE: func(cmd *cobra.Command, args []string) error {
plans, _, err := PacknGo.Plans.List(nil)
plans, _, err := PacknGo.Plans.List(listOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Plans")
}
Expand Down
21 changes: 15 additions & 6 deletions cmd/retrieve_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,25 @@ packet project get
Retrieve a specific project:
packet project get -i [project_UUID]
packet project get -n [project_name]
When using "--json" or "--yaml", "--include=members" is implied.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if projectID != "" && projectName != "" {
return fmt.Errorf("Must specify only one of project-id and project name")
}
if projectID == "" {
listOpt := &packngo.ListOptions{
Includes: []string{"members"},
}

projects, _, err := PacknGo.Projects.List(listOpt)
inc := []string{"members"}

// don't fetch extra details that won't be rendered
if !isYaml && !isJSON {
inc = nil
}

listOpts := listOptions(inc, nil)

if projectID == "" {
projects, _, err := PacknGo.Projects.List(listOpts)
if err != nil {
return errors.Wrap(err, "Could not list Projects")
}
Expand All @@ -77,7 +85,8 @@ packet project get -n [project_name]
header := []string{"ID", "Name", "Created"}
return output(projects, header, &data)
} else {
p, _, err := PacknGo.Projects.Get(projectID, &packngo.GetOptions{Includes: []string{"members"}})
getOpts := &packngo.GetOptions{Includes: listOpts.Includes, Excludes: listOpts.Excludes}
p, _, err := PacknGo.Projects.Get(projectID, getOpts)
if err != nil {
return errors.Wrap(err, "Could not get Project")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/retrieve_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ packet virtual-network get -p [project_UUID]
`,
RunE: func(cmd *cobra.Command, args []string) error {
vnets, _, err := PacknGo.ProjectVirtualNetworks.List(projectID, nil)
vnets, _, err := PacknGo.ProjectVirtualNetworks.List(projectID, listOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Project Virtual Networks")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/retrieve_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ packet volume get --id [volume_UUID]
} else if projectID == "" && volumeID == "" {
return fmt.Errorf("Either id or project-id should be set.")
} else if projectID != "" {
volumes, _, err := PacknGo.Volumes.List(projectID, nil)
volumes, _, err := PacknGo.Volumes.List(projectID, listOptions(nil, nil))
if err != nil {
return errors.Wrap(err, "Could not list Volumes")
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
cfgFile string
isJSON bool
isYaml bool
includes *[]string
excludes *[]string
packetToken string
)

Expand Down Expand Up @@ -78,9 +80,25 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&isJSON, "json", "j", false, "JSON output")
rootCmd.PersistentFlags().BoolVarP(&isYaml, "yaml", "y", false, "YAML output")

includes = rootCmd.PersistentFlags().StringSlice("include", nil, "Comma seperated Href references to expand in results, may be dotted three levels deep")
excludes = rootCmd.PersistentFlags().StringSlice("exclude", nil, "Comma seperated Href references to collapse in results, may be dotted three levels deep")

rootCmd.Version = Version
}

// listOptions creates a ListOptions using the includes and excludes persistent
// flags. When not defined, the defaults given will be supplied.
func listOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions {
listOptions := &packngo.ListOptions{}
if !rootCmd.Flags().Changed("include") {
listOptions.Includes = defaultIncludes
}
if !rootCmd.Flags().Changed("exclude") {
listOptions.Excludes = defaultExcludes
}
return listOptions
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
Expand Down

0 comments on commit 642820b

Please sign in to comment.