diff --git a/cmd/gh-not/main.go b/cmd/gh-not/main.go index ec27e44..c6a202b 100644 --- a/cmd/gh-not/main.go +++ b/cmd/gh-not/main.go @@ -1,9 +1,15 @@ package main import ( + "fmt" + "os" + "github.com/nobe4/gh-not/internal/cmd" ) func main() { - cmd.Execute() + if err := cmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } } diff --git a/internal/cmd/list.go b/internal/cmd/list.go new file mode 100644 index 0000000..29a12bf --- /dev/null +++ b/internal/cmd/list.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "fmt" + "log/slog" + + "github.com/spf13/cobra" +) + +var ( + listCmd = &cobra.Command{ + Use: "list", + Short: "List notifications", + RunE: func(cmd *cobra.Command, args []string) error { + notifications, err := client.Notifications() + if err != nil { + slog.Error("Failed to list notifications", "err", err) + return err + } + + fmt.Println(notifications.ToString()) + return nil + }, + } +) diff --git a/internal/cmd/manage.go b/internal/cmd/manage.go deleted file mode 100644 index 5482d51..0000000 --- a/internal/cmd/manage.go +++ /dev/null @@ -1,58 +0,0 @@ -package cmd - -import ( - "log/slog" - - "github.com/nobe4/gh-not/internal/actors" - "github.com/nobe4/gh-not/internal/cache" - "github.com/nobe4/gh-not/internal/config" - "github.com/nobe4/gh-not/internal/gh" - "github.com/spf13/cobra" -) - -var ( - noop bool - - manageCmd = &cobra.Command{ - Use: "manage", - Short: "Manage your notification based on your config", - RunE: func(cmd *cobra.Command, args []string) error { - config, err := config.New(configPath) - if err != nil { - slog.Error("Failed to load the cache", "path", configPath, "err", err) - return err - } - - cache := cache.NewFileCache(config.Cache.TTLInHours, config.Cache.Path) - - client, err := gh.NewClient(cache) - if err != nil { - slog.Error("Failed to create a gh client", "err", err) - return err - } - - notifications, err := client.Notifications() - if err != nil { - slog.Error("Failed to list notifications", "err", err) - return err - } - - notifications, err = config.Apply(notifications, actors.Map(client), noop) - if err != nil { - slog.Error("Failed to applying rules", "err", err) - return err - } - - if err := cache.Write(notifications); err != nil { - slog.Error("Failed to write the cache", "err", err) - return err - } - - return nil - }, - } -) - -func init() { - manageCmd.Flags().BoolVarP(&noop, "no-op", "n", false, "Doesn't execute any action") -} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index dff8df6..196942f 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -1,10 +1,12 @@ package cmd import ( - "fmt" "log/slog" "os" + cachePkg "github.com/nobe4/gh-not/internal/cache" + configPkg "github.com/nobe4/gh-not/internal/config" + "github.com/nobe4/gh-not/internal/gh" "github.com/spf13/cobra" ) @@ -14,31 +16,56 @@ var ( refresh bool noRefresh bool + config *configPkg.Config + cache *cachePkg.FileCache + client *gh.Client + rootCmd = &cobra.Command{ - Use: "gh-not", - Short: "Manage your GitHub notifications", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return initLogger() - }, + Use: "gh-not", + Short: "Manage your GitHub notifications", + PersistentPreRunE: setupGlobals, } ) -func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } +func Execute() error { + return rootCmd.Execute() } func init() { rootCmd.Root().CompletionOptions.DisableDefaultCmd = true - rootCmd.AddCommand(manageCmd) + rootCmd.AddCommand(syncCmd) + rootCmd.AddCommand(listCmd) rootCmd.PersistentFlags().IntVarP(&verbosity, "verbosity", "v", 2, "Change logger verbosity") rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yaml", "Path to the YAML config file") } +func setupGlobals(cmd *cobra.Command, args []string) error { + var err error + + config, err = configPkg.New(configPath) + if err != nil { + slog.Error("Failed to load the cache", "path", configPath, "err", err) + return err + } + + cache = cachePkg.NewFileCache(config.Cache.TTLInHours, config.Cache.Path) + + client, err = gh.NewClient(cache) + if err != nil { + slog.Error("Failed to create a gh client", "err", err) + return err + } + + if err := initLogger(); err != nil { + slog.Error("Failed to init the logger", "err", err) + return err + } + + return nil +} + func initLogger() error { opts := &slog.HandlerOptions{} diff --git a/internal/cmd/sync.go b/internal/cmd/sync.go new file mode 100644 index 0000000..cafa153 --- /dev/null +++ b/internal/cmd/sync.go @@ -0,0 +1,47 @@ +package cmd + +import ( + "log/slog" + + "github.com/nobe4/gh-not/internal/actors" + "github.com/spf13/cobra" +) + +var ( + noop bool + + syncCmd = &cobra.Command{ + Use: "sync", + Short: "Sync notifications based on your config", + Long: ` +'gh-not sync' applies the ruleset on all the notifications in the cache. + +Use this command when you want to make sure that your notification list is up to +date with your ruleset. +`, + RunE: func(cmd *cobra.Command, args []string) error { + notifications, err := client.Notifications() + if err != nil { + slog.Error("Failed to list notifications", "err", err) + return err + } + + notifications, err = config.Apply(notifications, actors.Map(client), noop) + if err != nil { + slog.Error("Failed to applying rules", "err", err) + return err + } + + if err := cache.Write(notifications); err != nil { + slog.Error("Failed to write the cache", "err", err) + return err + } + + return nil + }, + } +) + +func init() { + syncCmd.Flags().BoolVarP(&noop, "no-op", "n", false, "Doesn't execute any action") +}