mini-chat/cmd/client.go

73 lines
1.6 KiB
Go

/*
Copyright © 2024 Raul <raul@bulgariu.xyz>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
)
var clientCmd = &cobra.Command{
Use: "client",
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`,
Run: func(cmd *cobra.Command, args []string) {
if err := setClientParameters(cmd); err != nil {
cmd.Help()
fmt.Printf("\n-----------------------\n")
fmt.Println(err)
fmt.Printf("-----------------------\n")
os.Exit(0)
}
Client()
},
}
func init() {
rootCmd.AddCommand(clientCmd)
clientCmd.PersistentFlags().StringP("ip", "i", "", "Server IP to connect to")
clientCmd.PersistentFlags().StringP("port", "p", "1302", "Server port to connect to")
clientCmd.Flags().Bool("insecure", false, "[UNSAFE] Do not use TLS encryption")
clientCmd.Flags().BoolP("ascii", "a", false, "Render UI in pure ASCII, might help with rendering issues")
}
func setClientParameters(cmd *cobra.Command) error {
parameterPort, err := cmd.Flags().GetString("port")
if err != nil {
return err
}
if parameterPort != "" {
serverPort = parameterPort
}
parameterIP, err := cmd.Flags().GetString("ip")
if err != nil {
return err
}
if parameterIP == "" {
err := fmt.Errorf("IP cannot be empty")
return err
}
serverIP = parameterIP
insecure, err := cmd.Flags().GetBool("insecure")
if insecure == true {
clientInsecure = true
}
ascii, err := cmd.Flags().GetBool("ascii")
if ascii == true {
useASCII = true
}
return nil
}