diff --git a/chat-tests/cmd/server.go b/chat-tests/cmd/server.go index 1682ea1..55a57de 100644 --- a/chat-tests/cmd/server.go +++ b/chat-tests/cmd/server.go @@ -5,10 +5,9 @@ Copyright © 2024 Raul package cmd import ( - "fmt" - "github.com/spf13/cobra" "log" - "net" + + "github.com/spf13/cobra" ) // serverCmd represents the server command @@ -22,7 +21,11 @@ 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) { - setParameters(cmd) + + if err := setParameters(cmd); err != nil { + log.Fatalln("Error: Port could not be read!") + } + Server() }, } @@ -42,52 +45,13 @@ func init() { // serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } -var ( - port string = "1302" -) - -type Updater interface { - UpdateUser() -} - -type User struct { - Username string - IP string -} - -func setParameters(cmd *cobra.Command) { - -} - -func (u User) UpdateUser(usr string, ip string) User { - newU := new(User) - newU.Username = usr - newU.IP = ip - return *newU -} - -func Server() { - ln, err := net.Listen("tcp", ":"+port) - checkErr(err) - fmt.Printf("Listening on port %v...\n", port) - for { - conn, err := ln.Accept() - checkErr(err) - go handleConn(conn) - } -} - -func getUserInput() { - -} - -func handleConn(conn net.Conn) { - fmt.Println("Received connection") - fmt.Fprintln(conn, "Hello buddy!") -} - -func checkErr(err error) { +func setParameters(cmd *cobra.Command) error { + parameterPort, err := cmd.Flags().GetString("port") if err != nil { - log.Fatalf("Error: %v\n", err) + return err } + if parameterPort != "" { + port = parameterPort + } + return nil } diff --git a/chat-tests/cmd/serverFunc.go b/chat-tests/cmd/serverFunc.go new file mode 100644 index 0000000..433d589 --- /dev/null +++ b/chat-tests/cmd/serverFunc.go @@ -0,0 +1,53 @@ +package cmd + +import ( + "fmt" + "log" + "net" +) + +var ( + port string = "1302" +) + +type Updater interface { + UpdateUser() +} + +type User struct { + Username string + IP string +} + +func (u User) UpdateUser(usr string, ip string) User { + newU := new(User) + newU.Username = usr + newU.IP = ip + return *newU +} + +func Server() { + ln, err := net.Listen("tcp", ":"+port) + checkErr(err) + fmt.Printf("Listening on port %v...\n", port) + for { + conn, err := ln.Accept() + checkErr(err) + go handleConn(conn) + } +} + +func getUserInput() { + +} + +func handleConn(conn net.Conn) { + fmt.Println("Received connection") + fmt.Fprintln(conn, "Hello buddy!") +} + +func checkErr(err error) { + if err != nil { + log.Fatalf("Error: %v\n", err) + } +}