Skip to content

Commit

Permalink
Optimized Home Context loading for support commands
Browse files Browse the repository at this point in the history
  • Loading branch information
thibauult committed Jan 12, 2024
1 parent 0583d85 commit 545ecff
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
1 change: 1 addition & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewCmdGet(ctx *openhue.Context) *cobra.Command {

cmd := &cobra.Command{
Use: "get",
Aliases: []string{"g"},
Short: "Display one or many resources",
GroupID: "hue",
Long: `
Expand Down
58 changes: 43 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
"openhue-cli/cmd/setup"
"openhue-cli/cmd/version"
"openhue-cli/openhue"
"os"
"time"
"openhue-cli/util"
)

type OpenHueCmdGroup string

const (
// OpenHueCmdGroupHue contains the actual commands to control the home
OpenHueCmdGroupHue = OpenHueCmdGroup("hue")
// OpenHueCmdGroupConfig contains the commands to configure the CLI
OpenHueCmdGroupConfig = OpenHueCmdGroup("config")
)

// NewCmdOpenHue represents the `openhue` base command, AKA entry point of the CLI
func NewCmdOpenHue() *cobra.Command {
func NewCmdOpenHue(ctx *openhue.Context) *cobra.Command {

cmd := &cobra.Command{
Use: "openhue",
Expand All @@ -22,11 +30,27 @@ func NewCmdOpenHue() *cobra.Command {
openhue controls your Philips Hue lighting system
Find more information at: https://www.openhue.io/cli`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.Infof("Running the '%s' command", cmd.Name())
LoadHomeIfNeeded(ctx, cmd)
},
}

return cmd
}

// LoadHomeIfNeeded checks if the command requires loading the openhue.Home context
func LoadHomeIfNeeded(ctx *openhue.Context, cmd *cobra.Command) {
if isCmdInGroup(cmd, OpenHueCmdGroupHue) {
log.Infof("The '%s' command is in the '%s' group so we are loading the Home Context", cmd.Name(), OpenHueCmdGroupHue)
timer := util.NewTimer()
home, err := openhue.LoadHome(ctx.Api)
cobra.CheckErr(err)
ctx.Home = home
log.Infof("It took %dms to load the Home Context", timer.SinceInMillis())
}
}

// 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 *openhue.BuildInfo) {
Expand All @@ -39,16 +63,8 @@ func Execute(buildInfo *openhue.BuildInfo) {
api := c.NewOpenHueClient()
ctx := openhue.NewContext(openhue.NewIOStreams(), buildInfo, api)

// load the home context
t0 := time.Now()
home, err := openhue.LoadHome(api)
cobra.CheckErr(err)
ctx.Home = home
log.Infof("It took %dms to load the Home Context", time.Since(t0).Milliseconds())

// create the root command
root := NewCmdOpenHue()
log.Infof("Running the '%s' command", os.Args)
root := NewCmdOpenHue(ctx)

// init groups
initGroups(root)
Expand All @@ -63,18 +79,30 @@ func Execute(buildInfo *openhue.BuildInfo) {
root.AddCommand(get.NewCmdGet(ctx))

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

func initGroups(rootCmd *cobra.Command) {
rootCmd.AddGroup(&cobra.Group{
ID: "config",
ID: string(OpenHueCmdGroupConfig),
Title: "Configuration",
})

rootCmd.AddGroup(&cobra.Group{
ID: "hue",
ID: string(OpenHueCmdGroupHue),
Title: "Philips Hue",
})
}

// isCmdInGroup verifies if a given cobra.Command belongs to a certain OpenHueCmdGroup
func isCmdInGroup(cmd *cobra.Command, id OpenHueCmdGroup) bool {

if cmd.GroupID == string(id) {
return true
} else if cmd.Parent() != nil {
return isCmdInGroup(cmd.Parent(), id)
}

return false
}
1 change: 1 addition & 0 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func NewCmdSet(ctx *openhue.Context) *cobra.Command {

cmd := &cobra.Command{
Use: "set",
Aliases: []string{"s"},
Short: "Set specific features on resources",
GroupID: "hue",
Long: `
Expand Down
Binary file added docs/images/social.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions util/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

import "time"

func NewTimer() *Timer {
return new(Timer).start()
}

type Timer struct {
t0 time.Time
}

func (t *Timer) start() *Timer {
t.t0 = time.Now()
return t
}

func (t *Timer) SinceInMillis() int64 {
return time.Since(t.t0).Milliseconds()
}

0 comments on commit 545ecff

Please sign in to comment.