Add client to project
This commit is contained in:
parent
09a9f9efb7
commit
ed54642110
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Copyright © 2024 Raul <raul@bulgariu.xyz>
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
// clientCmd represents the client command
|
||||
var clientCmd = &cobra.Command{
|
||||
Use: "client",
|
||||
Short: "Client interface for tiny-chat",
|
||||
Long: `Refactored mini-chat client that properly interfaces
|
||||
with its server.
|
||||
|
||||
Example:
|
||||
./tiny-chat client --ip 192.168.0.100 --port 1337`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
cmd.Help()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if err := setClientParameters(cmd); err != nil {
|
||||
log.Fatalf("Error happened trying to set parameters: %v\n", err)
|
||||
}
|
||||
|
||||
Client()
|
||||
},
|
||||
}
|
||||
|
||||
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")
|
||||
clientCmd.PersistentFlags().String("ip", "", "Server IP to connect to")
|
||||
clientCmd.PersistentFlags().String("port", "1302", "Server port to connect to")
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
// "log"
|
||||
// "net"
|
||||
)
|
||||
|
||||
var (
|
||||
serverPort string = "1302"
|
||||
serverIP string
|
||||
)
|
||||
|
||||
func Client() {
|
||||
|
||||
}
|
Loading…
Reference in New Issue