first working request
This commit is contained in:
parent
ff03883985
commit
8c268d9342
13
cmd/root.go
13
cmd/root.go
|
@ -1,17 +1,16 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/internal/mood"
|
||||||
|
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/pkg/env"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "home-assistant",
|
Use: "home-assistant",
|
||||||
|
@ -24,7 +23,11 @@ This application is a tool to generate the needed files
|
||||||
to quickly create a Cobra application.`,
|
to quickly create a Cobra application.`,
|
||||||
// Uncomment the following line if your bare application
|
// Uncomment the following line if your bare application
|
||||||
// has an action associated with it:
|
// has an action associated with it:
|
||||||
// Run: func(cmd *cobra.Command, args []string) { },
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
env.EnvLoadFile()
|
||||||
|
|
||||||
|
mood.SetMood(mood.GetMoods()[0])
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
|
@ -47,5 +50,3 @@ func init() {
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
7
go.mod
7
go.mod
|
@ -3,8 +3,11 @@ module git.lorenzzz.dev/lorenzhohermuth/home-assistant
|
||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gofor-little/env v1.0.18 // indirect
|
github.com/gofor-little/env v1.0.18
|
||||||
|
github.com/spf13/cobra v1.8.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/spf13/cobra v1.8.1 // indirect
|
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
@ -1 +1,23 @@
|
||||||
package mood
|
package mood
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lorenzzz.dev/lorenzhohermuth/home-assistant/pkg/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var key = "mood"
|
||||||
|
var moods = []string{"relax"}
|
||||||
|
var eventName = "SET_MOOD_EVENT"
|
||||||
|
|
||||||
|
func createBody(mood string) string {
|
||||||
|
return fmt.Sprintf("{\"%s\": \"%s\"}", key, mood)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMoods() []string {
|
||||||
|
return moods
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetMood(mood string) {
|
||||||
|
http.FireEvent(eventName, createBody(mood))
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,14 @@ import (
|
||||||
"github.com/gofor-little/env"
|
"github.com/gofor-little/env"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func EnvLoadFile() {
|
||||||
|
err := env.Load(".env")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetUrl() string {
|
func GetUrl() string {
|
||||||
url, err := env.MustGet("URL")
|
url, err := env.MustGet("URL")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -15,7 +15,11 @@ func FireEvent(event string, eventBody string) {
|
||||||
apiSpecs := api.GetApiSpecifications()
|
apiSpecs := api.GetApiSpecifications()
|
||||||
url := env.GetUrl() + apiSpecs.FireEvent + event
|
url := env.GetUrl() + apiSpecs.FireEvent + event
|
||||||
|
|
||||||
response, err := http.Post(url, apiSpecs.JsonContentType, reader)
|
req, err := http.NewRequest("POST", url, reader)
|
||||||
|
req.Header.Add("Authorization", "Bearer "+env.GetToken())
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
response, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue