From 08fb32b4b221c00f6965e0b19d7c38cb7e566b16 Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 8 May 2024 09:08:36 +0200 Subject: [PATCH] Transition project to Cobra --- cmd/clientFunc.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 51 ++++++++++++++++++++++++++++++++++++ cmd/server.go | 42 +++++++++++++++++++++++++++++ cmd/serverFunc.go | 16 +++++++++++ main.go | 67 +++++------------------------------------------ 5 files changed, 181 insertions(+), 61 deletions(-) create mode 100644 cmd/clientFunc.go create mode 100644 cmd/root.go create mode 100644 cmd/server.go create mode 100644 cmd/serverFunc.go diff --git a/cmd/clientFunc.go b/cmd/clientFunc.go new file mode 100644 index 0000000..7754b78 --- /dev/null +++ b/cmd/clientFunc.go @@ -0,0 +1,66 @@ +package cmd + +import ( + "encoding/json" + "fmt" + xj "github.com/basgys/goxml2json" + "io" + "log" + "net/http" + "strings" +) + +type root struct { + Base struct { + Nombre string `json:"nombre"` + Prediccion struct { + Dia []struct { + Fecha string `json:"-fecha"` + Temperatura struct { + Maxima string `json:"maxima"` + Minima string `json:"minima"` + } + } + } + } `json:"root"` +} + +func main() { + jsonData := getJSON() + textBytes := []byte(jsonData) + aemetRequest := root{} + err := json.Unmarshal(textBytes, &aemetRequest) + if err != nil { + log.Fatalf("Error occurred unmarshalling data: %v\n", err) + } + + fmt.Println(aemetRequest.Base.Nombre) + fmt.Println(aemetRequest.Base.Prediccion.Dia[0].Fecha) + fmt.Printf("Temperatura máxima: %v°C\n", aemetRequest.Base.Prediccion.Dia[0].Temperatura.Maxima) + fmt.Printf("Temperatura mínima: %v°C\n", aemetRequest.Base.Prediccion.Dia[0].Temperatura.Minima) +} + +func getJSON() string { + resp, err := http.Get("https://www.aemet.es/xml/municipios/localidad_46250.xml") + if err != nil { + log.Fatalf("Error occurred pulling data: %v\n", err) + } + + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + log.Fatalf("Error occurred accessing website: %v\n", err) + } + + data, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatalf("Error occurred reading data: %v\n", err) + } + + xml := strings.NewReader(string(data)) + json, err := xj.Convert(xml) + if err != nil { + log.Fatalf("Error occurred converting XML to JSON: %v\n", err) + } + + return json.String() +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..05c5819 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,51 @@ +/* +Copyright © 2024 NAME HERE + +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + + + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "aemet", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +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) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.aemet.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + + diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..98d74cf --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,42 @@ +/* +Copyright © 2024 raul +*/ + +package cmd + +import ( + "github.com/spf13/cobra" + "log" +) + +var serverCmd = &cobra.Command{ + Use: "server", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + if err := setServerParameters(cmd); err != nil { + log.Fatalf("Error happened trying to set parameters: %v\n", err) + } + }, +} + +func init() { + rootCmd.AddCommand(serverCmd) + serverCmd.PersistentFlags().StringP("port", "p", "1302", "port to use for listening") +} + +func setServerParameters(cmd *cobra.Command) error { + parameterPort, err := cmd.Flags().GetString("port") + if err != nil { + return err + } + if parameterPort != "" { + listenPort = parameterPort + } + return nil +} diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go new file mode 100644 index 0000000..f2c3b3f --- /dev/null +++ b/cmd/serverFunc.go @@ -0,0 +1,16 @@ +package cmd + +import ( + "fmt" + + "github.com/gin-gonic/gin" +) + +var ( + listenPort string = "1302" +) + +func server() { + router := gin.Default + getJSON() +} diff --git a/main.go b/main.go index 135d317..991956d 100644 --- a/main.go +++ b/main.go @@ -1,66 +1,11 @@ +/* +Copyright © 2024 raul +*/ + package main -import ( - "encoding/json" - "fmt" - xj "github.com/basgys/goxml2json" - "io" - "log" - "net/http" - "strings" -) - -type root struct { - Base struct { - Nombre string `json:"nombre"` - Prediccion struct { - Dia []struct { - Fecha string `json:"-fecha"` - Temperatura struct { - Maxima string `json:"maxima"` - Minima string `json:"minima"` - } - } - } - } `json:"root"` -} +import "aemet/cmd" func main() { - jsonData := getJSON() - textBytes := []byte(jsonData) - aemetRequest := root{} - err := json.Unmarshal(textBytes, &aemetRequest) - if err != nil { - log.Fatalf("Error occurred unmarshalling data: %v\n", err) - } - - fmt.Println(aemetRequest.Base.Nombre) - fmt.Println(aemetRequest.Base.Prediccion.Dia[0].Fecha) - fmt.Printf("Temperatura máxima: %v°C\n", aemetRequest.Base.Prediccion.Dia[0].Temperatura.Maxima) - fmt.Printf("Temperatura mínima: %v°C\n", aemetRequest.Base.Prediccion.Dia[0].Temperatura.Minima) -} - -func getJSON() string { - resp, err := http.Get("https://www.aemet.es/xml/municipios/localidad_46250.xml") - if err != nil { - log.Fatalf("Error occurred pulling data: %v\n", err) - } - - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - log.Fatalf("Error occurred accessing website: %v\n", err) - } - - data, err := io.ReadAll(resp.Body) - if err != nil { - log.Fatalf("Error occurred reading data: %v\n", err) - } - - xml := strings.NewReader(string(data)) - json, err := xj.Convert(xml) - if err != nil { - log.Fatalf("Error occurred converting XML to JSON: %v\n", err) - } - - return json.String() + cmd.Execute() }