Skip to content

Commit

Permalink
chore: add more linters
Browse files Browse the repository at this point in the history
+dependency upgrades
  • Loading branch information
dhth committed Aug 24, 2024
1 parent 663b6fe commit 83888e7
Show file tree
Hide file tree
Showing 16 changed files with 349 additions and 341 deletions.
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
linters:
enable:
- errcheck
- errname
- errorlint
- goconst
- gofumpt
- gosimple
- govet
- ineffassign
- nilerr
- prealloc
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- unconvert
- unused
- usestdlibvars
- wastedassign
79 changes: 41 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,53 @@ const (
envPrefix = "PRS"
author = "@dhth"
projectHomePage = "https://github.com/dhth/prs"
issuesUrl = "https://github.com/dhth/prs/issues"
issuesURL = "https://github.com/dhth/prs/issues"
configFileName = "prs/prs.yml"
defaultSearchQuery = "type:pr author:@me sort:updated-desc state:open"
defaultPRNum = 20
maxPRNum = 50
)

var (
errModeIncorrect = errors.New("incorrect mode provided")
errConfigFileDoesntExist = errors.New("config file does not exist")
errNoReposProvided = errors.New("no repos were provided")
errCouldntGetHomeDir = errors.New("couldn't get home directory")
errCouldntGetConfigDir = errors.New("couldn't get config directory")
errModeIncorrect = errors.New("incorrect mode provided")
errConfigFileDoesntExist = errors.New("config file does not exist")
errNoReposProvided = errors.New("no repos were provided")
errIncorrectRepoProvided = errors.New("incorrect repo provided")
errCouldntSetupGithubClient = errors.New("couldn't set up a Github Client")
)

func Execute(version string) {
rootCmd, err := NewRootCommand(version)
var reportIssueMsg = fmt.Sprintf("Let %s know about this error via %s.", author, issuesURL)

func Execute(version string) error {
rootCmd, err := NewRootCommand(version)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
fmt.Printf("Error: %s\n", err.Error())
switch {
case errors.Is(err, errCouldntGetHomeDir), errors.Is(err, errCouldntGetConfigDir):
fmt.Printf(`
This is a fatal error; use --config-path to specify config file path manually.
%s
`, reportIssueMsg)
}
return err
}

_ = rootCmd.Execute()
err = rootCmd.Execute()

switch {
case errors.Is(err, errCouldntSetupGithubClient):
fmt.Printf(`
If the error is due to misconfigured authentication, you can fix that by either of the following:
- Provide a valid Github token via $GH_TOKEN
- Have an authenticated instance of gh (https://github.com/cli/cli) available
`)
}
return err
}

func NewRootCommand(version string) (*cobra.Command, error) {

var (
configFilePath string
configPathFull string
Expand Down Expand Up @@ -84,7 +105,7 @@ Project home page: %s
Args: cobra.MaximumNArgs(0),
SilenceUsage: true,
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
configPathFull = expandTilde(configFilePath)

if filepath.Ext(configPathFull) != ".yml" {
Expand Down Expand Up @@ -122,7 +143,7 @@ Project home page: %s
if mode == ui.RepoMode {
var reposToUse []string
// pretty ugly hack to get around the fact that
// v.GetStringSlice("repos") always seems to prioritise the config file
// v.GetStringSlice("repos") always seems to prioritize the config file
if len(repoStrs) > 0 && len(repoStrs[0]) > 0 && !strings.HasPrefix(repoStrs[0], "[") {
reposToUse = repoStrs
} else {
Expand All @@ -137,7 +158,7 @@ Project home page: %s
repoEls := strings.Split(r, "/")
// TODO: there can be more validations done here, maybe regex based
if len(repoEls) != 2 {
return fmt.Errorf("Incorrect repo provided: %s", r)
return fmt.Errorf("%w: %s", errIncorrectRepoProvided, r)
}

repos = append(repos, ui.Repo{
Expand All @@ -155,18 +176,11 @@ Project home page: %s

ghClient, err = ghapi.NewGraphQLClient(opts)
if err != nil {
return fmt.Errorf(`Couldn't set up a Github client.
If the error is due to misconfigured authentication, you can fix that by either of the following:
- Provide a valid Github token via $GH_TOKEN
- Have an authenticated instance of gh (https://github.com/cli/cli) available
Underlying error: %s`, err.Error())
return fmt.Errorf("%w: %s", errCouldntSetupGithubClient, err.Error())
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {

RunE: func(_ *cobra.Command, _ []string) error {
config := ui.Config{
PRCount: prNum,
Repos: repos,
Expand All @@ -181,11 +195,7 @@ Underlying error: %s`, err.Error())
ros := runtime.GOOS
userCfgDir, err := os.UserConfigDir()
if err != nil {
return nil, fmt.Errorf(`Couldn't get your default config directory. This is a fatal error;
use --config-path to specify config file path manually.
Let %s know about this via %s.
Error: %s`, author, issuesUrl, err)
return nil, fmt.Errorf("%w: %s", errCouldntGetConfigDir, err.Error())
}

var defaultConfigFilePath string
Expand All @@ -196,11 +206,7 @@ Error: %s`, author, issuesUrl, err)
// to use ~/.config instead of $HOME/Library/Application Support
hd, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf(`Couldn't get your home directory. This is a fatal error;
use --config-path to specify config file path manually
Let %s know about this via %s.
Error: %s`, author, issuesUrl, err)
return nil, fmt.Errorf("%w: %s", errCouldntGetHomeDir, err.Error())
}
defaultConfigFilePath = filepath.Join(hd, ".config", configFileName)
}
Expand All @@ -223,11 +229,9 @@ func initializeConfig(cmd *cobra.Command, configFile string) (*viper.Viper, erro
v.SetConfigType("yaml")
v.AddConfigPath(filepath.Dir(configFile))

var err error
if err = v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return v, err
}
err := v.ReadInConfig()
if err != nil && !errors.As(err, &viper.ConfigFileNotFoundError{}) {
return v, err
}

v.SetEnvPrefix(envPrefix)
Expand All @@ -245,7 +249,6 @@ func initializeConfig(cmd *cobra.Command, configFile string) (*viper.Viper, erro
func bindFlags(cmd *cobra.Command, v *viper.Viper) error {
var err error
cmd.Flags().VisitAll(func(f *pflag.Flag) {

if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
fErr := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
Expand Down
41 changes: 18 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/dhth/prs
go 1.22.5

require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.6
github.com/charmbracelet/glamour v0.7.0
github.com/charmbracelet/lipgloss v0.12.1
github.com/charmbracelet/bubbles v0.19.0
github.com/charmbracelet/bubbletea v0.27.1
github.com/charmbracelet/glamour v0.8.0
github.com/charmbracelet/lipgloss v0.13.0
github.com/cli/go-gh/v2 v2.9.0
github.com/cli/shurcooL-graphql v0.0.4
github.com/dustin/go-humanize v1.0.1
github.com/muesli/termenv v0.15.2
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
Expand All @@ -22,13 +22,11 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/charmbracelet/x/input v0.1.3 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.2 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.11.2 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
Expand All @@ -45,29 +43,26 @@ require (
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/thlib/go-timezone-local v0.0.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yuin/goldmark v1.7.4 // indirect
github.com/yuin/goldmark-emoji v1.0.3 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 83888e7

Please sign in to comment.