changeable server

This commit is contained in:
Jan 2024-09-21 16:58:56 +02:00
parent 09e550d5bb
commit 1c31bfd746
6 changed files with 181 additions and 3 deletions

40
cmd/default.go Normal file
View File

@ -0,0 +1,40 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// defaultCmd represents the default command
var defaultCmd = &cobra.Command{
Use: "default",
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) {
fmt.Println("default called")
},
}
func init() {
serverCmd.AddCommand(defaultCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// defaultCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// defaultCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

40
cmd/server.go Normal file
View File

@ -0,0 +1,40 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// serverCmd represents the server command
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) {
fmt.Println("server called")
},
}
func init() {
rootCmd.AddCommand(serverCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

90
cmd/set.go Normal file
View File

@ -0,0 +1,90 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"io"
"net/http"
"net/url"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// setCmd represents the set command
var setCmd = &cobra.Command{
Use: "set",
Short: "Set the server to get the data from",
Long: `This command will set the server to get the data from.
You can set the server to your own server or the default server.
To set the server to the default server, use the argument "default"
To set the server to your own server pass your server as the argument.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("No server provided")
return
}
if len(args) > 1 {
fmt.Println("Too many arguments")
return
}
server := args[0]
if server == "default" {
fmt.Println("Setting server to default")
viper.Set("Server", "https://mensa.barfuss.email")
viper.WriteConfig()
return
}
_, err := url.ParseRequestURI(server)
if err != nil {
fmt.Println("Invalid server")
return
}
h, err := http.Get(server)
if err != nil {
fmt.Println("Server not reachable")
return
}
if h.StatusCode != 200 {
fmt.Println("Server not reachable")
return
}
body, err := io.ReadAll(h.Body)
if err != nil {
fmt.Println("Error reading body")
return
}
if string(body) != "{\"message\":\"Mensen API\"}" {
fmt.Println("Invalid server")
return
}
defer h.Body.Close()
viper.Set("Server", server)
fmt.Println("Server set to", server)
viper.WriteConfig()
},
}
func init() {
serverCmd.AddCommand(setCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// setCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// setCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -4,10 +4,13 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"github.com/spf13/viper"
) )
func GetState(state string) (string, error) { func GetState(state string) (string, error) {
http, err := http.Get("https://mensa.barfuss.email/state/" + state) server := viper.Get("Server").(string)
http, err := http.Get(server + "/state/" + state)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -4,10 +4,13 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"github.com/spf13/viper"
) )
func GetMensen(city string) (string, error) { func GetMensen(city string) (string, error) {
http, err := http.Get("https://mensa.barfuss.email/city/" + city) server := viper.Get("Server").(string)
http, err := http.Get(server + "/city/" + city)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -7,10 +7,12 @@ import (
"net/http" "net/http"
"github.com/LeRoid-hub/Mensa-CLI/models" "github.com/LeRoid-hub/Mensa-CLI/models"
"github.com/spf13/viper"
) )
func GetMenu(mensa string) (models.Mensa, error) { func GetMenu(mensa string) (models.Mensa, error) {
http, err := http.Get("https://mensa.barfuss.email/mensa/" + mensa) server := viper.Get("Server").(string)
http, err := http.Get(server + "/mensa/" + mensa)
if err != nil { if err != nil {
return models.Mensa{}, err return models.Mensa{}, err
} }