[go-fan] Go Module Review: github.com/cli/go-gh/v2 #5826
Closed
Replies: 2 comments 1 reply
-
|
/plan |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
⚓ Avast! This discussion be marked as outdated by Go Fan. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
🐹 Go Fan Report: github.com/cli/go-gh/v2
Module Overview
github.com/cli/go-gh/v2is the official Go library for GitHub CLI, providing seamless GitHub API integration for CLI extensions and Go applications. It handles authentication automatically, respects GitHub CLI conventions, and provides utilities for terminal capabilities, repository context detection, and both REST and GraphQL API interactions.Key Highlight: The project has been upgraded to v2.13.0 (from v2.12.2), which includes HTTP status 205 handling fixes, the new
replacesprig function, and Go 1.25.0 support.Current Usage in gh-aw
The project uses
go-ghin 4 files with a total of approximately 850 lines of code leveraging this module:gh.Exec()for shell-out commandsUsage Breakdown
pkg/workflow/repository_features_validation.go (182 lines)
pkg/workflow/github_cli.go (58 lines)
ExecGH()andExecGHWithOutput()pkg/cli/exec.go (140 lines)
gh repo clonepkg/parser/remote_fetch.go (410 lines)
Current Usage Pattern
Shell-Out Approach (100% of usage):
No native REST/GraphQL client usage detected - all GitHub API interactions go through CLI shell-outs with manual JSON parsing.
Research Findings
What's New in v2.13.0
✨ Features:
replacesprig function for Go template string manipulation🐛 Fixes:
HandleHTTPErrordoes not close response body🔒 Security Note:
Best Practices from Upstream
The
go-ghdocumentation and examples strongly recommend:Use Native API Clients instead of shell-outs:
Use
repository.Current()for automatic repo detection:Enable API Caching for better performance:
Use GraphQL Client for complex queries:
Improvement Opportunities
Full Improvement Analysis
🏃 Quick Wins (High Impact, Low Effort)
1. Replace
gh repo viewwithrepository.Current()Location:
pkg/workflow/repository_features_validation.go:182Current:
Better:
Benefits:
Estimated Effort: 5 minutes
2. Use
api.DefaultRESTClient()for Repository QueriesLocation:
pkg/workflow/repository_features_validation.go:317Current:
Better:
Benefits:
Estimated Effort: 10 minutes
3. Use
api.DefaultGraphQLClient()for Discussions CheckLocation:
pkg/workflow/repository_features_validation.go:286Current:
Better:
Benefits:
Estimated Effort: 15 minutes
✨ Feature Opportunities
1. Enable API Caching for Repository Features
The code already implements application-level caching with
sync.Map. Adding go-gh's built-in HTTP caching layer would provide additional benefits:Benefits:
Estimated Effort: 30 minutes
2. Leverage New
replaceSprig Function (v2.13.0)If the project uses Go templates for output formatting, the new
replacefunction provides powerful string manipulation:Action: Audit template usage and consider leveraging new sprig functions.
Estimated Effort: Variable (depending on template usage)
3. Consider Accessible Colors for Markdown Rendering
If rendering markdown to terminal, consider supporting
GH_ACCESSIBLE_COLORS:Benefits:
Estimated Effort: 10 minutes
📐 Best Practice Alignment
1. Consolidate GitHub API Logic into Dedicated Package
Current State: GitHub API calls are scattered across:
pkg/workflow/repository_features_validation.gopkg/workflow/github_cli.gopkg/cli/exec.gopkg/parser/remote_fetch.goRecommended Structure:
Benefits:
Estimated Effort: 2-3 hours
2. Eliminate Manual Base64 Decoding
Location:
pkg/parser/remote_fetch.go:401Current:
Better:
Benefits:
Estimated Effort: 20 minutes
3. Replace Error String Matching with Typed Errors
Current Pattern (multiple files):
Better:
Benefits:
Estimated Effort: 1 hour
4. Adopt
pkg/authfor Token ManagementLocation:
pkg/workflow/github_cli.go:24-38Current:
Better:
Benefits:
Estimated Effort: 30 minutes
🔧 General Improvements
1. Migration Path from Shell-Outs to Native Clients
Recommended Approach:
Example Interface:
Benefits:
Estimated Effort: 4-6 hours for full migration
2. Add HTTP Response Body Cleanup
Important Note: v2.13.0 documentation clarifies that
HandleHTTPErrordoes NOT close response bodies.Action: Ensure response bodies are properly closed when using REST client:
Estimated Effort: 15 minutes (code review)
3. Consider REST Client for File Downloads
Location:
pkg/parser/remote_fetch.go:375-409The current multi-step process with base64 decoding and git fallbacks could be simplified with native REST client usage.
Benefits:
Estimated Effort: 1 hour
Recommendations
Priority 1: High-Impact, Low-Effort (Start Here! 🎯)
gh repo viewwithrepository.Current()→ 5 minutesClientOptions→ 30 minutesTotal Time: ~1.5 hours
Impact: Immediate performance improvement and code simplification
Priority 2: Medium-Impact, Medium-Effort
pkg/githubpackage with REST/GraphQL clients → 2-3 hoursTotal Time: ~4 hours
Impact: Better architecture, maintainability, and consistency
Priority 3: Long-Term Architecture
pkg/authintegration for token management → 30 minutesTotal Time: ~6 hours
Impact: Modern, idiomatic Go code aligned with go-gh best practices
Performance Considerations
Shell-Out vs Native Client Benchmarks (Estimated)
Note: Actual performance depends on network latency, but native clients eliminate process spawning overhead.
Next Steps
pkg/github/client.goandrepository.goConclusion
The upgrade to v2.13.0 is excellent and includes important fixes. However, the project is missing out on significant performance and maintainability benefits by exclusively using shell-out commands instead of the native REST and GraphQL clients provided by go-gh.
Key Insight: The current implementation treats go-gh as merely a wrapper around the
ghCLI, when it actually provides powerful native API client capabilities that could eliminate process spawning overhead and simplify the codebase significantly.Recommended Action: Start with the Priority 1 quick wins (1.5 hours total investment) to see immediate benefits, then plan the architectural improvements for long-term maintainability.
References:
Module summary saved to:
specs/mods/cli-go-gh.mdBeta Was this translation helpful? Give feedback.
All reactions