Add configuration
This commit is contained in:
92
main.go
92
main.go
@@ -5,45 +5,99 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defaults := defaultConfig()
|
||||
defaultConfigPath, err := configPath()
|
||||
if err != nil {
|
||||
exitf("configuration: %v", err)
|
||||
}
|
||||
var (
|
||||
repo = flag.String("repo", os.Getenv("GH_REPO"), "optional GitHub repository filter as owner/name (or GH_REPO)")
|
||||
poll = flag.Duration("poll", 10*time.Second, "refresh interval")
|
||||
showAll = flag.Bool("all", false, "show all open PRs in --repo, not only PRs assigned to you")
|
||||
limit = flag.Int("limit", 50, "maximum open PRs to load (1-100)")
|
||||
endpoint = flag.String("endpoint", "https://api.github.com/graphql", "GitHub GraphQL endpoint")
|
||||
configFile = flag.String("config", defaultConfigPath, "TOML configuration file")
|
||||
repo = flag.String("repo", "", "optional GitHub repository filter as owner/name (or GH_REPO)")
|
||||
poll = flag.Duration("poll", defaults.RefreshInterval.Duration, "refresh interval")
|
||||
showAll = flag.Bool("all", defaults.ShowAll, "show all open PRs in --repo, not only PRs assigned to you")
|
||||
limit = flag.Int("limit", defaults.Limit, "maximum open PRs to load (1-100)")
|
||||
endpoint = flag.String("endpoint", defaults.Endpoint, "GitHub GraphQL endpoint")
|
||||
theme = flag.String("theme", defaults.Theme, "color theme: dark or light")
|
||||
foldResolved = flag.Bool("fold-resolved", defaults.Display.FoldResolved, "start resolved threads folded")
|
||||
listWidth = flag.Int("thread-list-width", defaults.Display.ThreadListWidthPercent, "thread list width as terminal percentage (20-60)")
|
||||
pathScroll = flag.Bool("path-scroll", defaults.Paths.Scroll, "scroll truncated paths")
|
||||
pathScrollRate = flag.Duration("path-scroll-interval", defaults.Paths.ScrollInterval.Duration, "path scrolling interval")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
visited := map[string]bool{}
|
||||
flag.Visit(func(item *flag.Flag) { visited[item.Name] = true })
|
||||
config, err := loadConfig(*configFile, visited["config"] || os.Getenv("GH_THREADS_CONFIG") != "")
|
||||
if err != nil {
|
||||
exitf("configuration: %v", err)
|
||||
}
|
||||
if visited["repo"] {
|
||||
config.Repository = *repo
|
||||
} else if envRepo := os.Getenv("GH_REPO"); envRepo != "" {
|
||||
config.Repository = envRepo
|
||||
}
|
||||
if visited["poll"] {
|
||||
config.RefreshInterval.Duration = *poll
|
||||
}
|
||||
if visited["all"] {
|
||||
config.ShowAll = *showAll
|
||||
}
|
||||
if visited["limit"] {
|
||||
config.Limit = *limit
|
||||
}
|
||||
if visited["endpoint"] {
|
||||
config.Endpoint = *endpoint
|
||||
}
|
||||
if visited["theme"] {
|
||||
config.Theme = *theme
|
||||
}
|
||||
if visited["fold-resolved"] {
|
||||
config.Display.FoldResolved = *foldResolved
|
||||
}
|
||||
if visited["thread-list-width"] {
|
||||
config.Display.ThreadListWidthPercent = *listWidth
|
||||
}
|
||||
if visited["path-scroll"] {
|
||||
config.Paths.Scroll = *pathScroll
|
||||
}
|
||||
if visited["path-scroll-interval"] {
|
||||
config.Paths.ScrollInterval.Duration = *pathScrollRate
|
||||
}
|
||||
if err := validateConfig(config); err != nil {
|
||||
exitf("configuration: %v", err)
|
||||
}
|
||||
|
||||
var owner, name string
|
||||
if *repo != "" {
|
||||
parts := strings.Split(*repo, "/")
|
||||
if config.Repository != "" {
|
||||
parts := strings.Split(config.Repository, "/")
|
||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||
exitf("--repo must be owner/name")
|
||||
}
|
||||
owner, name = parts[0], parts[1]
|
||||
}
|
||||
if *showAll && owner == "" {
|
||||
exitf("--all requires --repo")
|
||||
if err := applyTheme(config.Theme); err != nil {
|
||||
exitf("configuration: %v", err)
|
||||
}
|
||||
if *limit < 1 || *limit > 100 {
|
||||
exitf("--limit must be between 1 and 100")
|
||||
}
|
||||
if *poll < 2*time.Second {
|
||||
exitf("--poll must be at least 2s")
|
||||
}
|
||||
token, err := resolveToken(*endpoint)
|
||||
token, err := resolveToken(config.Endpoint)
|
||||
if err != nil {
|
||||
exitf("authenticate: %v", err)
|
||||
}
|
||||
|
||||
client := NewGitHubClient(*endpoint, token)
|
||||
app := NewApp(client, owner, name, *showAll, *limit, *poll)
|
||||
client := NewGitHubClient(config.Endpoint, token)
|
||||
app := NewAppWithSettings(
|
||||
client, owner, name, config.ShowAll, config.Limit, config.RefreshInterval.Duration,
|
||||
AppSettings{
|
||||
FoldResolved: config.Display.FoldResolved,
|
||||
ThreadListWidthPercent: config.Display.ThreadListWidthPercent,
|
||||
PathScroll: config.Paths.Scroll,
|
||||
PathScrollInterval: config.Paths.ScrollInterval.Duration,
|
||||
},
|
||||
)
|
||||
if _, err := tea.NewProgram(app, tea.WithAltScreen()).Run(); err != nil {
|
||||
exitf("run TUI: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user