golang-exercises/chat-tests/cmd/server.go

56 lines
1.3 KiB
Go
Raw Permalink Normal View History

/*
Copyright © 2024 Raul <raul@bulgariu.xyz>
*/
package cmd
import (
"github.com/spf13/cobra"
2024-04-23 07:59:38 +02:00
"log"
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
2024-04-23 07:59:38 +02:00
Short: "Tiny chat server",
Long: `Refactored mini-chat server designed to be connected to
using a proper interface this time, not using netcat.
2024-04-23 07:59:38 +02:00
Example:
2024-04-25 10:30:10 +02:00
./mini-chat server --port 1337`,
Run: func(cmd *cobra.Command, args []string) {
2024-04-23 07:59:38 +02:00
if err := setServerParameters(cmd); err != nil {
log.Fatalf("Error happened trying to set parameters: %v\n", err)
}
Server()
},
}
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")
// 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")
}
2024-04-23 07:59:38 +02:00
func setServerParameters(cmd *cobra.Command) error {
parameterPort, err := cmd.Flags().GetString("port")
if err != nil {
return err
}
if parameterPort != "" {
2024-04-23 07:59:38 +02:00
listenPort = parameterPort
}
return nil
}