From 8c268d9342a84585289b631d29484d99487665d3 Mon Sep 17 00:00:00 2001 From: lorenzhohermuth Date: Sun, 24 Nov 2024 19:04:48 +0100 Subject: [PATCH] first working request --- cmd/root.go | 13 +++++++------ go.mod | 7 +++++-- internal/mood/mood.go | 22 ++++++++++++++++++++++ pkg/env/env.go | 8 ++++++++ pkg/http/post.go | 6 +++++- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index bf6f0d1..ee53e34 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,17 +1,16 @@ /* Copyright © 2024 NAME HERE - */ package cmd import ( "os" + "git.lorenzzz.dev/lorenzhohermuth/home-assistant/internal/mood" + "git.lorenzzz.dev/lorenzhohermuth/home-assistant/pkg/env" "github.com/spf13/cobra" ) - - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "home-assistant", @@ -24,7 +23,11 @@ This application is a tool to generate the needed files to quickly create a Cobra application.`, // Uncomment the following line if your bare application // 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. @@ -47,5 +50,3 @@ func init() { // when this action is called directly. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } - - diff --git a/go.mod b/go.mod index 4c0e0b5..252fd0c 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,11 @@ module git.lorenzzz.dev/lorenzhohermuth/home-assistant go 1.22.5 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/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/internal/mood/mood.go b/internal/mood/mood.go index 453914a..ee95211 100644 --- a/internal/mood/mood.go +++ b/internal/mood/mood.go @@ -1 +1,23 @@ 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)) +} diff --git a/pkg/env/env.go b/pkg/env/env.go index bae96a2..8954e81 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -4,6 +4,14 @@ import ( "github.com/gofor-little/env" ) +func EnvLoadFile() { + err := env.Load(".env") + + if err != nil { + panic(err) + } +} + func GetUrl() string { url, err := env.MustGet("URL") if err != nil { diff --git a/pkg/http/post.go b/pkg/http/post.go index d1242a2..dd4c070 100644 --- a/pkg/http/post.go +++ b/pkg/http/post.go @@ -15,7 +15,11 @@ func FireEvent(event string, eventBody string) { apiSpecs := api.GetApiSpecifications() 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 { panic(err) }