helperbot/cmd/root.go

61 lines
1.3 KiB
Go

/*
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
*/
package cmd
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"git.bullercodeworks.com/brian/helperbot/app"
"github.com/spf13/cobra"
)
var debug = false
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "helperbot",
Short: "Helperbot Slack Bot",
// Uncomment the following line if your bare application
// has an action associated with it:
RunE: func(cmd *cobra.Command, args []string) error {
a, err := app.NewApp(debug)
if err != nil {
return err
}
// Set up a channel to intercept Ctrl+C for graceful shutdowns
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
// Save the changes when the app quits
fmt.Println("\nFinishing up...")
a.SendQuitMessage()
a.Stop()
}()
for a.IsRunning() {
time.Sleep(time.Second * 1)
}
fmt.Println("Done")
return nil
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Debug mode")
}