diff --git a/cmd/default.go b/cmd/default.go new file mode 100644 index 0000000..de466b3 --- /dev/null +++ b/cmd/default.go @@ -0,0 +1,40 @@ +/* +Copyright © 2024 NAME HERE + +*/ +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") +} diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..3c5eedd --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,40 @@ +/* +Copyright © 2024 NAME HERE + +*/ +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") +} diff --git a/cmd/set.go b/cmd/set.go new file mode 100644 index 0000000..82ac591 --- /dev/null +++ b/cmd/set.go @@ -0,0 +1,90 @@ +/* +Copyright © 2024 NAME HERE +*/ +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") +} diff --git a/internal/getCity.go b/internal/getCity.go index 4390c94..6830e02 100644 --- a/internal/getCity.go +++ b/internal/getCity.go @@ -4,10 +4,13 @@ import ( "encoding/json" "io" "net/http" + + "github.com/spf13/viper" ) 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 { return "", err } diff --git a/internal/getMensa.go b/internal/getMensa.go index 9b79fa3..3598055 100644 --- a/internal/getMensa.go +++ b/internal/getMensa.go @@ -4,10 +4,13 @@ import ( "encoding/json" "io" "net/http" + + "github.com/spf13/viper" ) 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 { return "", err } diff --git a/internal/getMenu.go b/internal/getMenu.go index 5d5fdad..5024486 100644 --- a/internal/getMenu.go +++ b/internal/getMenu.go @@ -7,10 +7,12 @@ import ( "net/http" "github.com/LeRoid-hub/Mensa-CLI/models" + "github.com/spf13/viper" ) 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 { return models.Mensa{}, err }