Skip to content

Commit

Permalink
fix(cmd): update info and version commands
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Jul 3, 2023
1 parent 2008937 commit 903561f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
10 changes: 6 additions & 4 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd

import (
"github.com/joshuar/go-hass-agent/internal/agent"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -19,9 +18,12 @@ var infoCmd = &cobra.Command{
Use: "info",
Short: "Print details of this device",
Long: "This will show the information that was used to register this device with Home Assistant",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setLogging()
setDebugging()
setProfiling()
},
Run: func(cmd *cobra.Command, args []string) {
_, _, agent := agent.NewAgent("")
deviceName, deviceID := agent.DeviceDetails()
log.Info().Msgf("Device Name %s. Device ID %s.", deviceName, deviceID)
agent.ShowInfo(agent.AgentOptions{ID: appID})
},
}
9 changes: 6 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd

import (
"github.com/joshuar/go-hass-agent/internal/agent"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -18,8 +17,12 @@ func init() {
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setLogging()
setDebugging()
setProfiling()
},
Run: func(cmd *cobra.Command, args []string) {
_, _, agent := agent.NewAgent("")
log.Info().Msgf("%s: %s", agent.Name, agent.Version)
agent.ShowVersion(agent.AgentOptions{ID: appID})
},
}
18 changes: 15 additions & 3 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type AgentOptions struct {
Headless, Register bool
}

func NewAgent(appID string) (context.Context, context.CancelFunc, *Agent) {
func newAgent(appID string) (context.Context, context.CancelFunc, *Agent) {
a := &Agent{
app: newUI(appID),
Name: Name,
Expand All @@ -69,7 +69,7 @@ func NewAgent(appID string) (context.Context, context.CancelFunc, *Agent) {
// publish it to Home Assistant
func Run(options AgentOptions) {
translator = translations.NewTranslator()
agentCtx, cancelFunc, agent := NewAgent(options.ID)
agentCtx, cancelFunc, agent := newAgent(options.ID)
defer close(agent.done)

registrationDone := make(chan struct{})
Expand Down Expand Up @@ -120,7 +120,7 @@ func Run(options AgentOptions) {
// UI or non-UI registration flow.
func Register(options AgentOptions, server, token string) {
translator = translations.NewTranslator()
agentCtx, cancelFunc, agent := NewAgent(options.ID)
agentCtx, cancelFunc, agent := newAgent(options.ID)
defer close(agent.done)

// Don't proceed unless the agent is registered and forced is not set
Expand All @@ -142,6 +142,18 @@ func Register(options AgentOptions, server, token string) {
log.Info().Msg("Device registered with Home Assistant.")
}

func ShowVersion(options AgentOptions) {
_, _, agent := newAgent(options.ID)
log.Info().Msgf("%s: %s", agent.Name, agent.Version)
}

func ShowInfo(options AgentOptions) {
_, _, agent := newAgent(options.ID)
deviceName, deviceID := agent.DeviceDetails()
log.Info().Msgf("Device Name %s. Device ID %s.", deviceName, deviceID)

}

func (agent *Agent) extraStoragePath(id string) (fyne.URI, error) {
rootPath := agent.app.Storage().RootURI()
extraPath, err := storage.Child(rootPath, id)
Expand Down

0 comments on commit 903561f

Please sign in to comment.