Skip to content

Commit

Permalink
The version command now gives a direct link to the commit in GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
thibauult committed Nov 28, 2023
1 parent f9f8535 commit 5d19db1
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 83 deletions.
17 changes: 9 additions & 8 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/openhue/gen"
"openhue-cli/util"
)

Expand All @@ -14,7 +15,7 @@ type GetFlags struct {
var GetConfig GetFlags

// NewCmdGet returns an initialized Command instance for 'get' sub command
func NewCmdGet(api *openhue.ClientWithResponses) *cobra.Command {
func NewCmdGet(ctx *openhue.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Display one or many resources",
Expand All @@ -23,7 +24,7 @@ func NewCmdGet(api *openhue.ClientWithResponses) *cobra.Command {
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 := api.GetResourcesWithResponse(context.Background())
resp, err := ctx.Api.GetResourcesWithResponse(context.Background())
cobra.CheckErr(err)
resources := *(*resp.JSON200).Data

Expand All @@ -33,7 +34,7 @@ Retrieve information for any kind of resources exposed by your Hue Bridge: light
// filter resources by type
n := 0
for _, r := range resources {
if *r.Type == openhue.ResourceGetType(typeFlag) {
if *r.Type == gen.ResourceGetType(typeFlag) {
resources[n] = r
n++
}
Expand All @@ -42,9 +43,9 @@ Retrieve information for any kind of resources exposed by your Hue Bridge: light
}

if GetConfig.Json {
util.PrintJson(resources)
util.PrintJson(ctx.Io, resources)
} else {
util.PrintTable(resources, PrintResource, "Resource ID", "Resource Type")
util.PrintTable(ctx.Io, resources, PrintResource, "Resource ID", "Resource Type")
}
},
}
Expand All @@ -56,12 +57,12 @@ Retrieve information for any kind of resources exposed by your Hue Bridge: light
cmd.PersistentFlags().BoolVar(&GetConfig.Json, "json", false, "Format output as JSON")

// sub commands
cmd.AddCommand(NewCmdGetLight(api))
cmd.AddCommand(NewCmdGetRoom(api))
cmd.AddCommand(NewCmdGetLight(ctx))
cmd.AddCommand(NewCmdGetRoom(ctx))

return cmd
}

func PrintResource(resource openhue.ResourceGet) string {
func PrintResource(resource gen.ResourceGet) string {
return *resource.Id + "\t" + string(*resource.Type)
}
19 changes: 10 additions & 9 deletions cmd/get/get_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/openhue/gen"
"openhue-cli/util"
"os"
)
Expand Down Expand Up @@ -34,7 +35,7 @@ func NewGetLightOptions() *LightOptions {
}

// NewCmdGetLight returns initialized Command instance for the 'get light' sub command
func NewCmdGetLight(api *openhue.ClientWithResponses) *cobra.Command {
func NewCmdGetLight(ctx *openhue.Context) *cobra.Command {

o := NewGetLightOptions()

Expand All @@ -46,7 +47,7 @@ func NewCmdGetLight(api *openhue.ClientWithResponses) *cobra.Command {
Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
o.PrepareGetRoomCmd(args)
o.RunGetLightCmd(api)
o.RunGetLightCmd(ctx)
},
}

Expand All @@ -59,11 +60,11 @@ func (o *LightOptions) PrepareGetRoomCmd(args []string) {
}
}

func (o *LightOptions) RunGetLightCmd(api *openhue.ClientWithResponses) {
var lights *[]openhue.LightGet
func (o *LightOptions) RunGetLightCmd(ctx *openhue.Context) {
var lights *[]gen.LightGet

if len(o.LightId) > 0 {
resp, err := api.GetLightWithResponse(context.Background(), o.LightId)
resp, err := ctx.Api.GetLightWithResponse(context.Background(), o.LightId)
cobra.CheckErr(err)

if resp.JSON200 == nil {
Expand All @@ -73,19 +74,19 @@ func (o *LightOptions) RunGetLightCmd(api *openhue.ClientWithResponses) {

lights = (*resp.JSON200).Data
} else {
resp, err := api.GetLightsWithResponse(context.Background())
resp, err := ctx.Api.GetLightsWithResponse(context.Background())
cobra.CheckErr(err)
lights = (*resp.JSON200).Data
}

if !GetConfig.Json {
util.PrintTable(*lights, PrintLight, "ID", "Name", "Type", "Status", "Brightness")
util.PrintTable(ctx.Io, *lights, PrintLight, "ID", "Name", "Type", "Status", "Brightness")
} else {
util.PrintJsonArray(*lights)
util.PrintJsonArray(ctx.Io, *lights)
}
}

func PrintLight(light openhue.LightGet) string {
func PrintLight(light gen.LightGet) string {

status := "[ ]"
brightness := "N/A"
Expand Down
19 changes: 10 additions & 9 deletions cmd/get/get_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/openhue/gen"
"openhue-cli/util"
"os"
)
Expand Down Expand Up @@ -34,7 +35,7 @@ func NewGetRoomOptions() *RoomOptions {
}

// NewCmdGetRoom returns initialized Command instance for the 'get light' sub command
func NewCmdGetRoom(api *openhue.ClientWithResponses) *cobra.Command {
func NewCmdGetRoom(ctx *openhue.Context) *cobra.Command {

o := NewGetRoomOptions()

Expand All @@ -46,7 +47,7 @@ func NewCmdGetRoom(api *openhue.ClientWithResponses) *cobra.Command {
Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
o.PrepareGetRoomCmd(args)
o.RunGetRoomCmd(api)
o.RunGetRoomCmd(ctx)
},
}

Expand All @@ -59,11 +60,11 @@ func (o *RoomOptions) PrepareGetRoomCmd(args []string) {
}
}

func (o *RoomOptions) RunGetRoomCmd(api *openhue.ClientWithResponses) {
var rooms *[]openhue.RoomGet
func (o *RoomOptions) RunGetRoomCmd(ctx *openhue.Context) {
var rooms *[]gen.RoomGet

if len(o.RoomId) > 0 {
resp, err := api.GetRoomWithResponse(context.Background(), o.RoomId)
resp, err := ctx.Api.GetRoomWithResponse(context.Background(), o.RoomId)
cobra.CheckErr(err)

if resp.JSON200 == nil {
Expand All @@ -73,18 +74,18 @@ func (o *RoomOptions) RunGetRoomCmd(api *openhue.ClientWithResponses) {

rooms = (*resp.JSON200).Data
} else {
resp, err := api.GetRoomsWithResponse(context.Background())
resp, err := ctx.Api.GetRoomsWithResponse(context.Background())
cobra.CheckErr(err)
rooms = (*resp.JSON200).Data
}

if !GetConfig.Json {
util.PrintTable(*rooms, PrintRoom, "ID", "Name", "Type")
util.PrintTable(ctx.Io, *rooms, PrintRoom, "ID", "Name", "Type")
} else {
util.PrintJsonArray(*rooms)
util.PrintJsonArray(ctx.Io, *rooms)
}
}

func PrintRoom(room openhue.RoomGet) string {
func PrintRoom(room gen.RoomGet) string {
return *room.Id + "\t" + *room.Metadata.Name + "\t" + string(*room.Metadata.Archetype)
}
28 changes: 14 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"openhue-cli/cmd/set"
"openhue-cli/cmd/setup"
"openhue-cli/cmd/version"
"openhue-cli/config"
"openhue-cli/util"
"openhue-cli/openhue"
)

// NewCmdOpenHue represents the `openhue` base command, AKA entry point of the CLI
Expand All @@ -27,31 +26,32 @@ openhue controls your Philips Hue lighting system

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(buildInfo *util.BuildInfo) {
func Execute(buildInfo *openhue.BuildInfo) {

// load the configuration
c := config.Config{}
c.Load()
c := openhue.Config{}
c.LoadConfig()

// get the API Client
api := c.NewOpenHueClient()
ctx := openhue.NewContext(openhue.NewIOSteams(), buildInfo, api)

// create the root command
cmd := NewCmdOpenHue()
root := NewCmdOpenHue()

// init groups
initGroups(cmd)
initGroups(root)

// add sub commands
cmd.AddCommand(version.NewCmdVersion(buildInfo))
cmd.AddCommand(setup.NewCmdAuth())
cmd.AddCommand(setup.NewCmdDiscover())
cmd.AddCommand(setup.NewCmdConfigure())
cmd.AddCommand(set.NewCmdSet(api))
cmd.AddCommand(get.NewCmdGet(api))
root.AddCommand(version.NewCmdVersion(ctx))
root.AddCommand(setup.NewCmdAuth())
root.AddCommand(setup.NewCmdDiscover())
root.AddCommand(setup.NewCmdConfigure())
root.AddCommand(set.NewCmdSet(ctx))
root.AddCommand(get.NewCmdGet(ctx))

// execute the root command
err := cmd.Execute()
err := root.Execute()
cobra.CheckErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// NewCmdSet returns an initialized Command instance for 'set' sub command
func NewCmdSet(api *openhue.ClientWithResponses) *cobra.Command {
func NewCmdSet(ctx *openhue.Context) *cobra.Command {

cmd := &cobra.Command{
Use: "set",
Expand All @@ -17,7 +17,7 @@ Set the values for a specific resource
`,
}

cmd.AddCommand(NewCmdSetLight(api))
cmd.AddCommand(NewCmdSetLight(ctx))

return cmd
}
17 changes: 9 additions & 8 deletions cmd/set/set_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/openhue/gen"
"openhue-cli/util/color"
)

Expand Down Expand Up @@ -66,7 +67,7 @@ func NewSetLightOptions() *LightOptions {
}

// NewCmdSetLight returns initialized Command instance for the 'set light' sub command
func NewCmdSetLight(api *openhue.ClientWithResponses) *cobra.Command {
func NewCmdSetLight(ctx *openhue.Context) *cobra.Command {

o := NewSetLightOptions()
f := LightFlags{}
Expand All @@ -79,7 +80,7 @@ func NewCmdSetLight(api *openhue.ClientWithResponses) *cobra.Command {
Args: cobra.MatchAll(cobra.RangeArgs(1, 10), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(o.PrepareCmdSetLight(&f))
cobra.CheckErr(o.RunCmdSetLight(api, args))
cobra.CheckErr(o.RunCmdSetLight(ctx.Api, args))
},
}

Expand Down Expand Up @@ -168,25 +169,25 @@ func (o *LightOptions) PrepareCmdSetLight(flags *LightFlags) error {
}

// RunCmdSetLight executes the light update command logic
func (o *LightOptions) RunCmdSetLight(api *openhue.ClientWithResponses, args []string) error {
func (o *LightOptions) RunCmdSetLight(api *gen.ClientWithResponses, args []string) error {

request := &openhue.UpdateLightJSONRequestBody{}
request := &gen.UpdateLightJSONRequestBody{}

if o.Status != Undefined {
request.On = &openhue.On{
request.On = &gen.On{
On: ToBool(o.Status),
}
}

if o.Brightness >= 0 && o.Brightness <= 100.0 {
request.Dimming = &openhue.Dimming{
request.Dimming = &gen.Dimming{
Brightness: &o.Brightness,
}
}

if o.Color != color.UndefinedColor {
request.Color = &openhue.Color{
Xy: &openhue.GamutPosition{
request.Color = &gen.Color{
Xy: &gen.GamutPosition{
X: &o.Color.X,
Y: &o.Color.Y,
},
Expand Down
6 changes: 3 additions & 3 deletions cmd/setup/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"
"github.com/spf13/cobra"
"openhue-cli/config"
"openhue-cli/openhue"
"openhue-cli/openhue/gen"
"os"
)

Expand All @@ -25,9 +25,9 @@ func NewCmdAuth() *cobra.Command {
Long: `Authenticate to retrieve the Hue Application Key. Requires to go and press the button on the bridge`,
Run: func(cmd *cobra.Command, args []string) {

client := config.NewOpenHueClientNoAuth(bridge)
client := openhue.NewOpenHueClientNoAuth(bridge)

body := new(openhue.AuthenticateJSONRequestBody)
body := new(gen.AuthenticateJSONRequestBody)
body.Devicetype = &deviceType
body.Generateclientkey = &generateClientKey
resp, err := client.AuthenticateWithResponse(context.Background(), *body)
Expand Down
15 changes: 9 additions & 6 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package version
import (
"fmt"
"github.com/spf13/cobra"
"openhue-cli/util"
"openhue-cli/openhue"
)

func NewCmdVersion(buildInfo *util.BuildInfo) *cobra.Command {
const (
BaseCommitUrl = "https://github.com/openhue/openhue-cli/commit/"
)

func NewCmdVersion(ctx *openhue.Context) *cobra.Command {

cmd := &cobra.Command{
Use: "version",
Expand All @@ -18,12 +22,11 @@ openhue version
`,
Run: func(cmd *cobra.Command, args []string) {

fmt.Println("\n# Version\t", buildInfo.Version)
fmt.Println("# Commit\t", buildInfo.Commit)
fmt.Println("# Time\t", buildInfo.Date)
fmt.Fprintln(ctx.Io.Out, "\n# Version\t", ctx.BuildInfo.Version)
fmt.Fprintln(ctx.Io.Out, "# Commit\t", BaseCommitUrl+ctx.BuildInfo.Commit)
fmt.Fprintln(ctx.Io.Out, "# Built at\t", ctx.BuildInfo.Date)
},
}

return cmd

}
Loading

0 comments on commit 5d19db1

Please sign in to comment.