2024-04-02 11:27:48 +02:00
|
|
|
/*
|
2024-04-26 08:48:48 +02:00
|
|
|
Copyright © 2024 Raul <raul@bulgariu.xyz>
|
2024-04-02 11:27:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-04-13 13:11:36 +02:00
|
|
|
"github.com/spf13/cobra"
|
2024-04-02 11:27:48 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// clientCmd represents the client command
|
|
|
|
var clientCmd = &cobra.Command{
|
|
|
|
Use: "client",
|
2024-04-26 08:48:48 +02:00
|
|
|
Short: "Client interface for mini-chat",
|
|
|
|
Long: `Refactored mini-chat client that properly interfaces
|
|
|
|
with its server.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
./mini-chat client --ip 192.168.0.100 --port 1337`,
|
2024-04-02 11:27:48 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-04-26 08:48:48 +02:00
|
|
|
|
|
|
|
if err := setClientParameters(cmd); err != nil {
|
|
|
|
cmd.Help()
|
|
|
|
fmt.Printf("\n-----------------------\n")
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Printf("-----------------------\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
Client()
|
2024-04-02 11:27:48 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(clientCmd)
|
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// clientCmd.PersistentFlags().String("foo", "", "A help for foo")
|
2024-04-26 08:48:48 +02:00
|
|
|
clientCmd.PersistentFlags().String("ip", "", "Server IP to connect to")
|
|
|
|
clientCmd.PersistentFlags().String("port", "1302", "Server port to connect to")
|
2024-04-02 11:27:48 +02:00
|
|
|
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
|
|
// is called directly, e.g.:
|
|
|
|
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
func setClientParameters(cmd *cobra.Command) error {
|
|
|
|
parameterPort, err := cmd.Flags().GetString("port")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-12 12:00:08 +02:00
|
|
|
}
|
2024-04-26 08:48:48 +02:00
|
|
|
if parameterPort != "" {
|
|
|
|
serverPort = parameterPort
|
2024-04-02 11:27:48 +02:00
|
|
|
}
|
2024-04-13 13:11:36 +02:00
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
parameterIP, err := cmd.Flags().GetString("ip")
|
2024-04-13 13:11:36 +02:00
|
|
|
if err != nil {
|
2024-04-26 08:48:48 +02:00
|
|
|
return err
|
2024-04-13 13:11:36 +02:00
|
|
|
}
|
2024-04-26 08:48:48 +02:00
|
|
|
if parameterIP == "" {
|
|
|
|
err := fmt.Errorf("IP cannot be empty")
|
|
|
|
return err
|
2024-04-13 13:11:36 +02:00
|
|
|
}
|
2024-04-26 08:48:48 +02:00
|
|
|
serverIP = parameterIP
|
2024-04-12 12:00:08 +02:00
|
|
|
|
2024-04-13 13:11:36 +02:00
|
|
|
return nil
|
2024-04-02 11:27:48 +02:00
|
|
|
}
|