Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base URL implicitly #318

Merged
merged 2 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
// RawJSON controls if the client does json handling or outputs it raw
var RawJSON = false

func genericJSONMethod(get bool, base, section, command string, body map[string]interface{}, timeout time.Duration) (*resty.Response, error) {
url, err := URLHelper(base, section, command)
func genericJSONMethod(get bool, section, command string, body map[string]interface{}, timeout time.Duration) (*resty.Response, error) {
url, err := URLHelper(section, command)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -47,19 +47,19 @@ func genericJSONMethod(get bool, base, section, command string, body map[string]
}

// GenericJSONGet is a helper for generic empty post request
func GenericJSONGet(base, section, command string) (*resty.Response, error) {
return genericJSONMethod(true, base, section, command, nil, DefaultTimeout)
func GenericJSONGet(section, command string) (*resty.Response, error) {
return genericJSONMethod(true, section, command, nil, DefaultTimeout)
}

func GenericJSONGetTimeout(base, section, command string, timeout time.Duration) (*resty.Response, error) {
return genericJSONMethod(true, base, section, command, nil, timeout)
func GenericJSONGetTimeout(section, command string, timeout time.Duration) (*resty.Response, error) {
return genericJSONMethod(true, section, command, nil, timeout)
}

// GenericJSONPost is a helper for generic empty post request
func GenericJSONPost(base, section, command string, body map[string]interface{}) (*resty.Response, error) {
return genericJSONMethod(false, base, section, command, body, DefaultTimeout)
func GenericJSONPost(section, command string, body map[string]interface{}) (*resty.Response, error) {
return genericJSONMethod(false, section, command, body, DefaultTimeout)
}

func GenericJSONPostTimeout(base, section, command string, body map[string]interface{}, timeout time.Duration) (*resty.Response, error) {
return genericJSONMethod(false, base, section, command, body, timeout)
func GenericJSONPostTimeout(section, command string, body map[string]interface{}, timeout time.Duration) (*resty.Response, error) {
return genericJSONMethod(false, section, command, body, timeout)
}
3 changes: 2 additions & 1 deletion client/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type Response struct {
}

// URLHelper returns a URL built from the arguments
func URLHelper(base, section, command string) (string, error) {
func URLHelper(section, command string) (string, error) {
base := viper.GetString("endpoint")
log.WithFields(log.Fields{
"base": base,
"section": section,
Expand Down
5 changes: 4 additions & 1 deletion client/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package client
import (
"fmt"
"testing"

"github.com/spf13/viper"
)

var urlTests = []struct {
Expand All @@ -27,7 +29,8 @@ var urlTests = []struct {
func TestURLHelper(t *testing.T) {
for _, tt := range urlTests {
t.Run(fmt.Sprintf("[%s][%s][%s]", tt.base, tt.section, tt.command), func(t *testing.T) {
s, _ := URLHelper(tt.base, tt.section, tt.command)
viper.SetDefault("endpoint", tt.base)
s, _ := URLHelper(tt.section, tt.command)
if s != tt.out {
t.Errorf("got %q, want %q", s, tt.out)
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsCmd = &cobra.Command{
Expand All @@ -26,9 +25,8 @@ information commands for add-ons.`,

section := "addons"
command := ""
base := viper.GetString("endpoint")

resp, err := helper.GenericJSONGet(base, section, command)
resp, err := helper.GenericJSONGet(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsChangelogCmd = &cobra.Command{
Expand All @@ -26,9 +25,8 @@ ha addons changelog core_mosquitto`,

section := "addons"
command := "{slug}/changelog"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)

if err != nil {
fmt.Println(err)
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsInfoCmd = &cobra.Command{
Expand All @@ -28,9 +27,8 @@ is provided, information about a specific add-on.

section := "addons"
command := "{slug}/info"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)

if err != nil {
fmt.Println(err)
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsInstalCmd = &cobra.Command{
Expand All @@ -27,9 +26,8 @@ This command allows you to install a Home Assistant add-on from the commandline.

section := "addons"
command := "{slug}/install"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsLogsCmd = &cobra.Command{
Expand All @@ -26,9 +25,8 @@ Allowing you to look at the log output generated by a Home Assistant add-on.

section := "addons"
command := "{slug}/logs"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)

if err != nil {
fmt.Println(err)
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsRebuildCmd = &cobra.Command{
Expand All @@ -30,9 +29,8 @@ add-on.

section := "addons"
command := "{slug}/rebuild"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsReloadCmd = &cobra.Command{
Expand All @@ -27,10 +26,9 @@ an add-on is released, but not yet available as an upgrade in Home Assistant.

section := "addons"
command := "reload"
base := viper.GetString("endpoint")

ProgressSpinner.Start()
resp, err := helper.GenericJSONPost(base, section, command, nil)
resp, err := helper.GenericJSONPost(section, command, nil)
ProgressSpinner.Stop()

if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsRestartCmd = &cobra.Command{
Expand All @@ -27,9 +26,8 @@ Restart a Home Assistant add-on

section := "addons"
command := "{slug}/restart"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsStartCmd = &cobra.Command{
Expand All @@ -27,9 +26,8 @@ This command allows you to manually start a stopped Home Assistant add-on

section := "addons"
command := "{slug}/start"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsStatsCmd = &cobra.Command{
Expand All @@ -28,9 +27,8 @@ how much CPU, memory, disk & network resources it uses.

section := "addons"
command := "{slug}/stats"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)

if err != nil {
fmt.Println(err)
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsStopCmd = &cobra.Command{
Expand All @@ -27,9 +26,8 @@ This command allows you to manually start a stopped Home Assistant add-on

section := "addons"
command := "{slug}/stop"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsUninstallCmd = &cobra.Command{
Expand All @@ -27,9 +26,8 @@ This command allows you to uninstall a Home Assistant add-on.

section := "addons"
command := "{slug}/uninstall"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/addons_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var addonsUpdateCmd = &cobra.Command{
Expand All @@ -28,9 +27,8 @@ It is currently not possible to upgrade/downgrade to a specific version.

section := "addons"
command := "{slug}/update"
base := viper.GetString("endpoint")

url, err := helper.URLHelper(base, section, command)
url, err := helper.URLHelper(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/audio_default_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var audioDefaultInputCmd = &cobra.Command{
Expand All @@ -25,7 +24,6 @@ Home Assistant Audio on your Home Assistant system.`,

section := "audio"
command := "default/input"
base := viper.GetString("endpoint")

options := make(map[string]interface{})

Expand All @@ -34,7 +32,7 @@ Home Assistant Audio on your Home Assistant system.`,
options["name"] = name
}

resp, err := helper.GenericJSONPost(base, section, command, options)
resp, err := helper.GenericJSONPost(section, command, options)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/audio_default_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var audioDefaultOutputCmd = &cobra.Command{
Expand All @@ -25,7 +24,6 @@ Home Assistant Audio on your Home Assistant system.`,

section := "audio"
command := "default/output"
base := viper.GetString("endpoint")

options := make(map[string]interface{})

Expand All @@ -34,7 +32,7 @@ Home Assistant Audio on your Home Assistant system.`,
options["name"] = name
}

resp, err := helper.GenericJSONPost(base, section, command, options)
resp, err := helper.GenericJSONPost(section, command, options)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
4 changes: 1 addition & 3 deletions cmd/audio_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var audioInfoCmd = &cobra.Command{
Expand All @@ -23,9 +22,8 @@ running on your Home Assistant system, including its devices.`,

section := "audio"
command := "info"
base := viper.GetString("endpoint")

resp, err := helper.GenericJSONGet(base, section, command)
resp, err := helper.GenericJSONGet(section, command)
if err != nil {
fmt.Println(err)
ExitWithError = true
Expand Down
Loading