Add error logging to awp-cli with configurable log file
- Route all log output to AWP_LOG_FILE (from .env) if set, else stderr - Log errors explicitly before returning so they appear in the log file - Replace log.Fatal in fetch.go with proper error return so callers control logging - Remove chatty success/no-op log lines that would bloat cron log files Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
04f687a278
commit
713a96501d
3 changed files with 19 additions and 24 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +1,32 @@
|
|||
/*
|
||||
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue