2024-03-29 09:04:20 +01:00
|
|
|
/*
|
2024-04-26 08:48:48 +02:00
|
|
|
Copyright © 2024 Raul <raul@bulgariu.xyz>
|
2024-03-29 09:04:20 +01:00
|
|
|
*/
|
2024-04-01 12:28:22 +02:00
|
|
|
|
2024-03-29 09:04:20 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-04-01 12:28:22 +02:00
|
|
|
"github.com/spf13/cobra"
|
2024-04-26 08:48:48 +02:00
|
|
|
"log"
|
2024-03-29 09:04:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
Use: "server",
|
2024-04-26 08:48:48 +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-01 12:28:22 +02:00
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
Example:
|
|
|
|
./mini-chat server --port 1337`,
|
2024-03-29 09:04:20 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-04-26 08:48:48 +02:00
|
|
|
|
|
|
|
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)
|
2024-05-08 08:34:07 +02:00
|
|
|
serverCmd.PersistentFlags().StringP("port", "p", "1302", "port to use for listening")
|
|
|
|
serverCmd.PersistentFlags().StringP("history", "r", "", "File to store and recover chat history from")
|
2024-05-13 12:02:03 +02:00
|
|
|
serverCmd.PersistentFlags().String("password", "", "Password for accessing the chat server")
|
2024-03-29 09:04:20 +01:00
|
|
|
}
|
|
|
|
|
2024-04-26 08:48:48 +02:00
|
|
|
func setServerParameters(cmd *cobra.Command) error {
|
|
|
|
parameterPort, err := cmd.Flags().GetString("port")
|
2024-03-29 09:36:03 +01:00
|
|
|
if err != nil {
|
2024-04-26 08:48:48 +02:00
|
|
|
return err
|
2024-03-29 09:04:20 +01:00
|
|
|
}
|
2024-04-26 08:48:48 +02:00
|
|
|
if parameterPort != "" {
|
|
|
|
listenPort = parameterPort
|
2024-03-29 09:04:20 +01:00
|
|
|
}
|
2024-05-07 10:48:16 +02:00
|
|
|
parameterHistory, err := cmd.Flags().GetString("history")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parameterHistory != "" {
|
|
|
|
logLocation = parameterHistory
|
|
|
|
isLogging = true
|
|
|
|
}
|
2024-05-13 12:02:03 +02:00
|
|
|
parPassword, err := cmd.Flags().GetString("password")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parPassword != "" {
|
|
|
|
password = parPassword
|
|
|
|
}
|
2024-04-26 08:48:48 +02:00
|
|
|
return nil
|
2024-03-29 09:04:20 +01:00
|
|
|
}
|