mini-chat/cmd/server.go

70 lines
1.5 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
)
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)
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")
serverCmd.Flags().Bool("insecure", false, "[UNSAFE] Do not use TLS encryption")
2024-03-29 09:04:20 +01:00
}
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
}
2024-05-14 09:27:23 +02:00
parameterHistory, err := cmd.Flags().GetString("history")
if err != nil {
return err
}
if parameterHistory != "" {
logLocation = parameterHistory
isLogging = true
}
2024-05-14 09:27:23 +02:00
2024-05-13 12:02:03 +02:00
parPassword, err := cmd.Flags().GetString("password")
if err != nil {
return err
}
if parPassword != "" {
password = parPassword
}
2024-05-14 09:27:23 +02:00
insecure, err := cmd.Flags().GetBool("insecure")
if insecure == true {
servInsecure = true
}
return nil
2024-03-29 09:04:20 +01:00
}