diff --git a/cmd/info.go b/cmd/info.go index dc12b2101..62cdef156 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -7,7 +7,6 @@ package cmd import ( "github.com/joshuar/go-hass-agent/internal/agent" - "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -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}) }, } diff --git a/cmd/version.go b/cmd/version.go index a0ab0cfcb..32cff6266 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -7,7 +7,6 @@ package cmd import ( "github.com/joshuar/go-hass-agent/internal/agent" - "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -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}) }, } diff --git a/internal/agent/agent.go b/internal/agent/agent.go index d33985576..4cde8cf75 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -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, @@ -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{}) @@ -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 @@ -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)