package cmd
import (
"fmt"
"log"
"strconv"
"time"
"github.com/liamg/tml"
"github.com/spf13/cobra"
"uberbringer/config"
"uberbringer/service"
)
var rootCommand = &cobra.Command{
Use: "uberbringer",
Short: "Runs the Überbringer daemon.",
PreRun: func(cmd *cobra.Command, args []string) {
config.GetConfig()
startSplash()
},
Run: func(cmd *cobra.Command, args []string) {
startServer()
},
}
var versionCommand = &cobra.Command{
Use: "version",
Short: "Prints the current executable version and exits.",
Run: func(cmd *cobra.Command, _ []string) {
fmt.Printf("%s | Version: %s Build: %s | Copyright © %d William Gill\n", config.Project, config.Version, config.Build, time.Now().Year())
},
}
func Execute() {
if err := rootCommand.Execute(); err != nil {
log.Fatalf("failed to execute command: %s", err)
}
}
func init() {
rootCommand.PersistentFlags().StringVarP(&config.TOML, "config", "c", "config.toml", "Specify daemon config file.")
rootCommand.AddCommand(versionCommand)
}
func startSplash() {
//// ***** Start Console Splash ***** ////
tml.Printf("-----------------------------------------------------------------------------------------------\n\n")
tml.Printf(" " + config.Project + " | Version: " + config.Version + " Build: " + config.Build + " | Copyright © " + strconv.Itoa(time.Now().Year()) + " William Gill \n\n")
tml.Printf(" Project: " + config.Project + "\n")
tml.Printf(" Description: " + config.Description + "\n")
tml.Printf(" Authors: " + config.Authors + "\n\n")
tml.Printf("-----------------------------------------------------------------------------------------------\n")
tml.Printf("[" + config.Project + "]: Initializing...\n")
//// ***** End Console Splash ***** ////
}
func startServer() {
//// ***** Start Echo ***** ////
tml.Printf("[" + config.Project + "]: Initializing Web Server...\n")
serverChannel := make(chan bool)
defer close(serverChannel)
// Start Web Server
go service.StartEcho(serverChannel, config.WebServerBindIP, config.WebServerPort)
// Stream Channel
serverData := <-serverChannel
fmt.Scanln(serverData)
//// ***** End Echo ***** ////
}