Skip to content

Commit

Permalink
Looking for config file under XDG dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolev Hadar committed Nov 21, 2021
1 parent ed5b5ae commit 8955c14
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
# gh-prs

`gh` cli extension to display a "dashboard" or pull requests by filters you care about.
`gh` cli extension to display a dashboard or pull requests by filters you care about.

![demo](https://github.com/dlvhdr/gh-prs/blob/8621183574c573e4077360b5027ffea70999b921/demo.gif)

Comes with 3 default sections:
* My Pull Requests
* Needs My Review
* Subscribed

## Installation

1. Install gh cli:

```sh
brew install gh
```
1. Install gh cli - see the [installation instructions](https://github.com/cli/cli#installation)

2. Install this extension:

Expand All @@ -26,9 +17,12 @@ gh extension install dlvhdr/gh-prs
## Configuring

Configuration is done in the `sections.yml` file under the extension's directory.
Each section is defined by a top level array item and has the following properies:
- title - shown in the TUI
- repos - a list of repos to enumerate
- filters - how the repo's PRs should be filtered - these are plain [github filters](https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests)

Example `sections.yml` file:

```yml
- title: My Pull Requests
repos:
Expand All @@ -48,8 +42,12 @@ Example `sections.yml` file:
## Usage
Run
Run:
```sh
gh prs
```

### Author
Then press <kbd>?</kbd> for help.

# Author
Dolev Hadar dolevc2@gmail.com
61 changes: 51 additions & 10 deletions config/parser.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"log"
"fmt"
"os"
"path"
"path/filepath"

"gopkg.in/yaml.v2"
Expand All @@ -14,23 +15,63 @@ type SectionConfig struct {
Repos []string
}

const PrsDir = "prs"
const SectionsFileName = "sections.yml"

type configError struct {
configDir string
err error
}

func (e configError) Error() string {
return fmt.Sprintf(
`Couldn't find a sections.yml configuration file.
Create one under: %s
Example of a sections.yml file:
- title: My Pull Requests
repos:
- dlvhdr/gh-prs
filters: author:@me
- title: Needs My Review
repos:
- dlvhdr/gh-prs
filters: assignee:@me
- title: Subscribed
repos:
- cli/cli
- charmbracelet/glamour
- charmbracelet/lipgloss
filters: -author:@me
For more info, go to https://github.com/dlvhdr/gh-prs
press q to exit.
Original error: %v`,
path.Join(e.configDir, PrsDir, SectionsFileName),
e.err,
)
}

func ParseSectionsConfig() ([]SectionConfig, error) {
ex, err := os.Executable()
if err != nil {
panic(err)
var sections []SectionConfig
configDir := os.Getenv("XDG_CONFIG_HOME")
var err error
if configDir == "" {
configDir, err = os.UserConfigDir()
if err != nil {
return sections, configError{configDir: configDir, err: err}
}
}
exPath := filepath.Dir(ex)
data, err := os.ReadFile(filepath.Join(exPath, SectionsFileName))

data, err := os.ReadFile(filepath.Join(configDir, PrsDir, SectionsFileName))
if err != nil {
panic(err)
return sections, configError{configDir: configDir, err: err}
}
var sections []SectionConfig

err = yaml.Unmarshal([]byte(data), &sections)
if err != nil {
log.Fatalf("error: %v", err)
return nil, err
return sections, fmt.Errorf("Failed parsing sections.yml: %w", err)
}

return sections, nil
Expand Down
14 changes: 0 additions & 14 deletions sections.yml

This file was deleted.

12 changes: 5 additions & 7 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ui
import (
"dlvhdr/gh-prs/config"
"dlvhdr/gh-prs/utils"
"log"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -63,8 +62,7 @@ func NewModel(logFile *os.File) Model {
func initScreen() tea.Msg {
sections, err := config.ParseSectionsConfig()
if err != nil {
log.Fatal(err)
panic(err)
return errMsg{err}
}

return initMsg{Config: sections}
Expand Down Expand Up @@ -200,12 +198,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m Model) View() string {
if m.configs == nil {
return "Reading config...\n"
if m.err != nil {
return m.err.Error()
}

if m.err != nil {
return "Error!\n"
if m.configs == nil {
return "Reading config...\n"
}

paddedContentStyle := lipgloss.NewStyle().
Expand Down

0 comments on commit 8955c14

Please sign in to comment.