diff --git a/cmd/cli/cmd/check.go b/cmd/cli/cmd/check.go index 8917ea3..e9ceb1f 100644 --- a/cmd/cli/cmd/check.go +++ b/cmd/cli/cmd/check.go @@ -65,19 +65,16 @@ var checkCmd = &cobra.Command{ emails sent by players to indicate if they will attend the next session.`, RunE: func(cmd *cobra.Command, args []string) error { - log.Default().Println("Checking for emails") emails, err := email.FetchEmails() if err != nil { + log.Default().Printf("ERROR fetching emails: %s", err) return err } - if len(emails) == 0 { - log.Default().Println("No new emails") - } else { - log.Default().Println("Reading new emails") + if len(emails) > 0 { session, err := data.ReadSessionData() if err != nil { - log.Default().Println(err) + log.Default().Printf("ERROR reading session data: %s", err) return err } diff --git a/cmd/cli/cmd/root.go b/cmd/cli/cmd/root.go index 39b1afa..0d6e1fb 100644 --- a/cmd/cli/cmd/root.go +++ b/cmd/cli/cmd/root.go @@ -1,26 +1,32 @@ -/* -Copyright © 2024 NAME HERE -*/ package cmd import ( + "fmt" + "log" "os" + "github.com/joho/godotenv" "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "awp-cli", Short: "CLI utility for Are We Playing? web app", Long: `CLI utility for Are We Playing? web app`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + godotenv.Load() + logFile := os.Getenv("AWP_LOG_FILE") + if len(logFile) > 0 { + f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("unable to open log file %s: %w", logFile, err) + } + log.SetOutput(f) + } + 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 { @@ -30,13 +36,5 @@ func Execute() { func init() { rootCmd.CompletionOptions.DisableDefaultCmd = true - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cli.yaml)") - - // Cobra also supports local flags, which will only run - // when this action is called directly. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/pkg/email/fetch.go b/pkg/email/fetch.go index e6fe9f3..57bbb34 100644 --- a/pkg/email/fetch.go +++ b/pkg/email/fetch.go @@ -19,7 +19,7 @@ func FetchEmails() ([]Email, error) { imapClient, err := config.ConnectToServer(IMAP_SERVICE) if err != nil { - log.Fatal(err) + return nil, err } defer imapClient.Logout()