-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The get command allows retrieving all resources as well as the lights…
… and the rooms with details
- Loading branch information
Showing
10 changed files
with
233 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"golang.org/x/net/context" | ||
"openhue-cli/openhue" | ||
) | ||
|
||
type GetFlags struct { | ||
Json bool | ||
} | ||
|
||
var GetConfig GetFlags | ||
|
||
// getCmd represents the get command | ||
var getCmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "Get resources", | ||
Long: ` | ||
Retrieve information for any kind of resources exposed by your Hue Bridge: lights, rooms, scenes, etc. | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
resp, err := openhue.Api.GetResourcesWithResponse(context.Background()) | ||
cobra.CheckErr(err) | ||
resources := *(*resp.JSON200).Data | ||
|
||
typeFlag := cmd.Flag("type").Value.String() | ||
|
||
if len(typeFlag) > 0 { | ||
// filter resources by type | ||
n := 0 | ||
for _, r := range resources { | ||
if *r.Type == openhue.ResourceGetType(typeFlag) { | ||
resources[n] = r | ||
n++ | ||
} | ||
} | ||
resources = resources[:n] | ||
} | ||
|
||
if GetConfig.Json { | ||
openhue.PrintJson(resources) | ||
} else { | ||
openhue.PrintTable(resources, PrintResource, "Resource ID", "Resource Type") | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(getCmd) | ||
|
||
// local flags | ||
getCmd.Flags().StringP("type", "t", "", "Filter by resource type (light, scene, room...)") | ||
|
||
// persistence flags | ||
getCmd.PersistentFlags().BoolVar(&GetConfig.Json, "json", false, "Format output as JSON") | ||
} | ||
|
||
func PrintResource(resource openhue.ResourceGet) string { | ||
return *resource.Id + "\t" + string(*resource.Type) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/net/context" | ||
"openhue-cli/openhue" | ||
"os" | ||
) | ||
|
||
// lightCmd represents the light command | ||
var lightCmd = &cobra.Command{ | ||
Use: "light", | ||
Short: "Get light", | ||
Long: ` | ||
Fetches and displays all available lights | ||
`, | ||
Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
var lights *[]openhue.LightGet | ||
|
||
if len(args) > 0 { | ||
resp, err := openhue.Api.GetLightWithResponse(context.Background(), args[0]) | ||
cobra.CheckErr(err) | ||
|
||
if resp.JSON200 == nil { | ||
fmt.Println("\nNot light found with ID", args[0]) | ||
os.Exit(0) | ||
} | ||
|
||
lights = (*resp.JSON200).Data | ||
} else { | ||
resp, err := openhue.Api.GetLightsWithResponse(context.Background()) | ||
cobra.CheckErr(err) | ||
lights = (*resp.JSON200).Data | ||
} | ||
|
||
if !GetConfig.Json { | ||
openhue.PrintTable(*lights, PrintLight, "Light ID", "Light Name", "Light Type") | ||
} else { | ||
openhue.PrintJsonArray(*lights) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
getCmd.AddCommand(lightCmd) | ||
} | ||
|
||
func PrintLight(light openhue.LightGet) string { | ||
return *light.Id + "\t" + *light.Metadata.Name + "\t" + string(*light.Metadata.Archetype) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/net/context" | ||
"openhue-cli/openhue" | ||
"os" | ||
) | ||
|
||
// roomCmd represents the room command | ||
var roomCmd = &cobra.Command{ | ||
Use: "room", | ||
Short: "Get room", | ||
Long: ` | ||
Fetches and displays all available rooms from the Philips Hue bridge | ||
`, | ||
Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
var rooms *[]openhue.RoomGet | ||
|
||
if len(args) > 0 { | ||
resp, err := openhue.Api.GetRoomWithResponse(context.Background(), args[0]) | ||
cobra.CheckErr(err) | ||
|
||
if resp.JSON200 == nil { | ||
fmt.Println("\nNot room found with ID", args[0]) | ||
os.Exit(0) | ||
} | ||
|
||
rooms = (*resp.JSON200).Data | ||
} else { | ||
resp, err := openhue.Api.GetRoomsWithResponse(context.Background()) | ||
cobra.CheckErr(err) | ||
rooms = (*resp.JSON200).Data | ||
} | ||
|
||
if !GetConfig.Json { | ||
openhue.PrintTable(*rooms, PrintRoom, "Room ID", "Room Name", "Room Type") | ||
} else { | ||
openhue.PrintJsonArray(*rooms) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
getCmd.AddCommand(roomCmd) | ||
} | ||
|
||
func PrintRoom(room openhue.RoomGet) string { | ||
return *room.Id + "\t" + *room.Metadata.Name + "\t" + string(*room.Metadata.Archetype) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package openhue | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"text/tabwriter" | ||
) | ||
|
||
// PrintJsonArray formats the input array as JSON and prints it. If the length of the array is equal to 1, | ||
// it will print it as a single object | ||
func PrintJsonArray[T any](array []T) { | ||
if len(array) == 1 { | ||
PrintJson(array[0]) | ||
} else { | ||
PrintJson(array) | ||
} | ||
} | ||
|
||
func PrintJson[T any](array T) { | ||
var out []byte | ||
out, _ = json.MarshalIndent(array, "", " ") | ||
fmt.Println(string(out)) | ||
} | ||
|
||
// PrintTable prints each line of the objects contained in the table value | ||
func PrintTable[T any](table []T, lineFn func(T) string, header ...string) { | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) | ||
|
||
for _, h := range header { | ||
_, _ = fmt.Fprint(w, h+"\t") | ||
} | ||
_, _ = fmt.Fprintln(w) | ||
|
||
for range header { | ||
_, _ = fmt.Fprint(w, "----\t") | ||
} | ||
_, _ = fmt.Fprintln(w) | ||
|
||
for _, l := range table { | ||
_, _ = fmt.Fprintln(w, lineFn(l)) | ||
} | ||
|
||
_ = w.Flush() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package openhue | ||
|
||
import "testing" | ||
|
||
type Light struct { | ||
id string | ||
name string | ||
} | ||
|
||
func PrintLightLineProcessor(light Light) string { | ||
return light.id + "\t" + light.name | ||
} | ||
|
||
func TestPrintTable(t *testing.T) { | ||
|
||
lights := []Light{{id: "1234", name: "Light 1"}, {id: "4321", name: "Light 2"}} | ||
|
||
PrintTable(lights, PrintLightLineProcessor, "id", "name") | ||
} |