Transition project to Cobra
This commit is contained in:
parent
acd0b74fca
commit
08fb32b4b2
|
@ -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()
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
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")
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
listenPort string = "1302"
|
||||
)
|
||||
|
||||
func server() {
|
||||
router := gin.Default
|
||||
getJSON()
|
||||
}
|
67
main.go
67
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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue