Organize files and implement clean port selection

This commit is contained in:
raul 2024-04-22 08:21:59 +02:00
parent 3bfe744b8d
commit c4302aea1a
2 changed files with 67 additions and 50 deletions

View File

@ -5,10 +5,9 @@ Copyright © 2024 Raul <raul@bulgariu.xyz>
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
}

View File

@ -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)
}
}