/* Copyright © 2024 Raul */ package cmd import ( "github.com/spf13/cobra" "log" ) 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`, 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() }, } func init() { rootCmd.AddCommand(serverCmd) serverCmd.PersistentFlags().StringP("port", "p", "1302", "port to use for listening") serverCmd.PersistentFlags().StringP("history", "r", "", "File to store and recover chat history from") } func setServerParameters(cmd *cobra.Command) error { parameterPort, err := cmd.Flags().GetString("port") if err != nil { return err } if parameterPort != "" { listenPort = parameterPort } parameterHistory, err := cmd.Flags().GetString("history") if err != nil { return err } if parameterHistory != "" { logLocation = parameterHistory isLogging = true } return nil }