Skip to content

Commit

Permalink
feat: add list command
Browse files Browse the repository at this point in the history
Also:
- refactor the root command usage
- create global variables for config, cache and client
  This doesn't feel correct, but I'll deal with changing that later
  • Loading branch information
nobe4 committed May 20, 2024
1 parent de8f14f commit 883e7a8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 71 deletions.
8 changes: 7 additions & 1 deletion cmd/gh-not/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
25 changes: 25 additions & 0 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
@@ -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
},
}
)
58 changes: 0 additions & 58 deletions internal/cmd/manage.go

This file was deleted.

51 changes: 39 additions & 12 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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{}

Expand Down
47 changes: 47 additions & 0 deletions internal/cmd/sync.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 883e7a8

Please sign in to comment.