mini-chat/cmd/server.go

56 lines
1.3 KiB
Go
Raw Normal View History

2024-03-29 09:04:20 +01:00
/*
Copyright © 2024 Raul <raul@bulgariu.xyz>
2024-03-29 09:04:20 +01:00
*/
2024-03-29 09:04:20 +01:00
package cmd
import (
"github.com/spf13/cobra"
"log"
2024-03-29 09:04:20 +01:00
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "Tiny chat server",
Long: `Refactored mini-chat server designed to be connected to
using a proper interface this time, not using netcat.
Example:
./mini-chat server --port 1337`,
2024-03-29 09:04:20 +01:00
Run: func(cmd *cobra.Command, args []string) {
if err := setServerParameters(cmd); err != nil {
log.Fatalf("Error happened trying to set parameters: %v\n", err)
}
Server()
2024-03-29 09:04:20 +01:00
},
}
func init() {
rootCmd.AddCommand(serverCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
serverCmd.PersistentFlags().String("port", "1302", "port to use for listening")
2024-03-29 09:04:20 +01:00
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func setServerParameters(cmd *cobra.Command) error {
parameterPort, err := cmd.Flags().GetString("port")
if err != nil {
return err
2024-03-29 09:04:20 +01:00
}
if parameterPort != "" {
listenPort = parameterPort
2024-03-29 09:04:20 +01:00
}
return nil
2024-03-29 09:04:20 +01:00
}