70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
/*
|
|
Copyright © 2024 Raul <raul@bulgariu.xyz>
|
|
*/
|
|
|
|
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 --history chat.log --password coolh4x0r1337`,
|
|
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")
|
|
serverCmd.PersistentFlags().String("password", "", "Password for accessing the chat server")
|
|
serverCmd.Flags().Bool("insecure", false, "[UNSAFE] Do not use TLS encryption")
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
parPassword, err := cmd.Flags().GetString("password")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if parPassword != "" {
|
|
password = parPassword
|
|
}
|
|
|
|
insecure, err := cmd.Flags().GetBool("insecure")
|
|
if insecure == true {
|
|
servInsecure = true
|
|
}
|
|
return nil
|
|
}
|