refactor: modernize config, improve Go idioms, and clean up state handling

This commit is contained in:
krishnassh committed 2025-07-15 20:24:14 +05:30
1 parent f34184420a
commit 5dded68307
5 files changed
+109 -173

No files matched your search

-3
View File
@@ -8,7 +8,6 @@ require (
github.com/charmbracelet/lipgloss v0.9.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
github.com/tidwall/gjson v1.18.0
)
require (
@@ -37,8 +36,6 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // 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
+4 -10
View File
@@ -120,7 +120,7 @@ github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIG
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200212025743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
@@ -217,12 +217,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -311,7 +305,7 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202538be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -319,7 +313,7 @@ golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4Iltr
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210218202505-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -426,7 +420,7 @@ golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202527-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE=
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
+2 -2
View File
@@ -238,7 +238,7 @@ func (s *SearchComponent) GetCurrentQuery() string {
}
func (s *SearchComponent) SearchWithQuery(query string) ([]types.Video, error) {
return services.SearchYouTube(query, s.config.GetSearchLimit())
return services.SearchYouTube(query, s.config.SearchLimit)
}
func (s *SearchComponent) searchVideos(query string) tea.Cmd {
@@ -298,7 +298,7 @@ func (s *SearchComponent) addRecentSearch(query string) {
func (s *SearchComponent) searchWithYtDlp(query string) ([]types.Video, error) {
// Use the new YouTube scraper instead of yt-dlp
return services.SearchYouTube(query, s.config.GetSearchLimit())
return services.SearchYouTube(query, s.config.SearchLimit)
}
// ClearCache clears the search cache
+40 -56
View File
@@ -7,69 +7,53 @@ import (
"github.com/spf13/viper"
)
const (
DefaultMPVPath = "mpv"
DefaultYTDlpPath = "yt-dlp"
DefaultVideoQuality = "best[height<=1080]/best"
DefaultSearchLimit = 20
)
type Config struct {
MPVPath string
YTDlpPath string
VideoQuality string
DownloadPath string
SearchLimit int
MPVPath string `mapstructure:"mpv_path"`
YTDlpPath string `mapstructure:"ytdlp_path"`
VideoQuality string `mapstructure:"video_quality"`
DownloadPath string `mapstructure:"download_path"`
SearchLimit int `mapstructure:"search_limit"`
}
func NewConfig() *Config {
config := &Config{
MPVPath: "mpv",
YTDlpPath: "yt-dlp",
VideoQuality: "best[height<=1080]/best",
DownloadPath: getDefaultDownloadPath(),
SearchLimit: 20, // changed from 8 to 20
func NewConfig() (*Config, error) {
v := viper.New()
v.SetConfigName("gophertube")
v.SetConfigType("yaml")
v.AddConfigPath("$HOME/.config/gophertube")
v.AddConfigPath(".")
// Set defaults
v.SetDefault("mpv_path", DefaultMPVPath)
v.SetDefault("ytdlp_path", DefaultYTDlpPath)
v.SetDefault("video_quality", DefaultVideoQuality)
v.SetDefault("download_path", getDefaultDownloadPath())
v.SetDefault("search_limit", DefaultSearchLimit)
_ = v.ReadInConfig() // Ignore error: config file is optional
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return nil, err
}
// Load config from file
viper.SetConfigName("gophertube")
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/.config/gophertube")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err == nil {
if mpvPath := viper.GetString("mpv_path"); mpvPath != "" {
config.MPVPath = mpvPath
}
if ytdlpPath := viper.GetString("ytdlp_path"); ytdlpPath != "" {
config.YTDlpPath = ytdlpPath
}
if quality := viper.GetString("video_quality"); quality != "" {
config.VideoQuality = quality
}
if downloadPath := viper.GetString("download_path"); downloadPath != "" {
config.DownloadPath = downloadPath
}
if searchLimit := viper.GetInt("search_limit"); searchLimit > 0 {
config.SearchLimit = searchLimit
}
// Defensive: ensure DownloadPath is set
if cfg.DownloadPath == "" {
cfg.DownloadPath = getDefaultDownloadPath()
}
if cfg.SearchLimit <= 0 {
cfg.SearchLimit = DefaultSearchLimit
}
return config
}
// ConfigService interface implementation
func (c *Config) GetMPVPath() string {
return c.MPVPath
}
func (c *Config) GetYTDlpPath() string {
return c.YTDlpPath
}
func (c *Config) GetVideoQuality() string {
return c.VideoQuality
}
func (c *Config) GetDownloadPath() string {
return c.DownloadPath
}
func (c *Config) GetSearchLimit() int {
return c.SearchLimit
return &cfg, nil
}
func getDefaultDownloadPath() string {
+63 -102
View File
@@ -1,135 +1,105 @@
.TH GOPHERTUBE 1 "2024" "GopherTube" "User Commands"
.TH GOPHERTUBE 1 "2025" "GopherTube" "User Commands"
.SH NAME
gophertube \- Terminal user interface for YouTube video search and playback
gophertube \- Terminal YouTube client for searching and watching videos
.SH SYNOPSIS
.B gophertube
[\fB\-\-help\fR]
[
.B \-\-help
]
.SH DESCRIPTION
.B GopherTube
is a modern terminal user interface for searching and watching YouTube videos
using yt-dlp and mpv. It provides a clean, keyboard-driven interface for
browsing and playing YouTube content directly in the terminal.
is a simple, modern terminal YouTube client for searching and watching videos using yt-dlp and mpv. It features a fast, keyboard-driven interface built with Go and Bubble Tea, requiring no API key. GopherTube scrapes YouTube directly and plays videos with mpv, all from your terminal.
.SH FEATURES
.TP
.B Search Interface
Real-time YouTube video search with instant results
.B Fast YouTube search
Scrapes YouTube directly, no API key needed
.TP
.B Video Playback
Direct video playback using mpv media player
.B Play videos with mpv
Seamless playback using the mpv media player
.TP
.B Navigation
Full keyboard navigation with arrow keys and shortcuts
.B Minimal terminal UI
Built with Bubble Tea, fully keyboard-driven
.TP
.B Design
Swiss design-inspired clean interface
.B Keyboard navigation
Navigate with arrows, Enter, Tab, Esc, g/G
.TP
.B Configuration
YAML-based configuration system
.B YAML config
Configure paths and settings easily
.TP
.B Responsive
Adapts to terminal window size
.B Rotating tips
Helpful shortcuts displayed at the bottom
.SH NAVIGATION
.SH KEYBOARD SHORTCUTS
.TS
allbox;
l l.
Key Action
_
Enter Search / Play video
↑/↓ Navigate video list
Tab Load more videos
g Go to first video
G Go to last video
Esc Go back / Quit
.TE
.SS Search View
.TP
.B Enter
Execute search with current query
.TP
.B Ctrl+C
Quit the application
.TP
.B Esc
Quit the application
.SS Video List View
.TP
.B ↑/↓
Navigate through video list
.TP
.B Enter
Play selected video
.TP
.B Tab
Load more videos (append to current list)
.TP
.B g
Go to first video
.TP
.B G
Go to last video
.TP
.B Esc
Return to search view
.SH USAGE
.PP
Start the app:
.B ./gophertube
.PP
Type a search and press Enter. Use ↑/↓ to move, Enter to play, Tab to load more, Esc to go back. A spinner shows while the video loads, then mpv opens. Watch for rotating tips at the bottom for helpful shortcuts!
.SH CONFIGURATION
The application can be configured by creating a YAML configuration file at
.BR ~/.config/gophertube/gophertube.yaml .
Example configuration:
.RS
.PP
Create ~/.config/gophertube/gophertube.yaml:
.PP
.nf
# Path to mpv executable
mpv_path: "mpv"
# Path to yt-dlp executable
ytdlp_path: "yt-dlp"
# Default video quality
video_quality: "best"
# Download directory
video_quality: "best[height<=1080]/best"
download_path: "~/Videos/gophertube"
search_limit: 8
.fi
.RE
.PP
See config/gophertube.yaml.example for the full template.
.SH REQUIREMENTS
.TP
.B yt-dlp
YouTube video downloader (required for search functionality)
.B Go 1.21+
Required for building from source
.TP
.B mpv
Media player for video playback
.TP
.B Go 1.21+
Required for building from source
.B yt-dlp
YouTube downloader for search and playback
.SH INSTALLATION
.SS From Source
.SS Quick Install (One-liner)
.PP
.nf
git clone https://github.com/yourusername/gophertube.git
cd gophertube
go build -o gophertube
sudo cp gophertube /usr/local/bin/
curl -sSL https://raw.githubusercontent.com/KrishnaSSH/GopherTube/main/install.sh | bash
.fi
.SS Dependencies
.PP
Install required dependencies:
.SS Manual Installation
.PP
.nf
# Ubuntu/Debian
sudo apt install mpv
pip install yt-dlp
# macOS
brew install mpv yt-dlp
# Arch Linux
sudo pacman -S mpv yt-dlp
git clone https://github.com/KrishnaSSH/GopherTube.git
cd GopherTube
go build -o gophertube
./gophertube
.fi
.SS Makefile
.PP
.nf
make install # Install binary and man page
.fi
.SH EXAMPLES
.TP
.B gophertube
Start the application
@@ -138,7 +108,6 @@ Start the application
Show help information
.SH ENVIRONMENT
.TP
.B GOPHERTUBE_CONFIG
Override configuration file path
@@ -147,7 +116,6 @@ Override configuration file path
Override cache directory path
.SH FILES
.TP
.B ~/.config/gophertube/gophertube.yaml
Configuration file
@@ -156,22 +124,15 @@ Configuration file
Default download directory
.SH BUGS
Report bugs to the project issue tracker at:
.BR https://github.com/yourusername/gophertube/issues
Report bugs and suggestions at:
.BR https://github.com/KrishnaSSH/GopherTube/issues
.SH AUTHOR
Written by the GopherTube development team.
.SH COPYRIGHT
Copyright © 2024 GopherTube contributors. This is free software; see the source
for copying conditions. There is NO warranty; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
Copyright © 2025 GopherTube contributors. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
.SH SEE ALSO
.BR mpv (1),
.BR yt-dlp (1),
.BR youtube-dl (1)
.BR yt-dlp (1)