- **Shortcuts**: Add hostname normalization with domain stripping and hyphen folding. Include alias generation for cached hosts. - **Shell Hook**: Automate 24h cache refresh trigger with shell startup hook. Add install/uninstall commands for bash, zsh, and fish. - **Wizard**: Extend setup wizard to configure shortcuts (domains, hyphen stripping) and default SSH port. - **Cache**: Add `GetByShortcut` for resolving hosts via normalized shortcuts. Implement `NeedsRefresh` / `SetRefreshed` logic for refresh timestamps. - **Tests**: Comprehensive unit tests for shortcuts, hook installation, cache refresh, and alias generation. - **Docs**: Update README with shortcuts, shell hook, and default SSH port configuration.
This commit is contained in:
Vendored
+61
-8
@@ -20,10 +20,11 @@ type Entry struct {
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
mu sync.RWMutex
|
||||
entries map[string]Entry
|
||||
path string
|
||||
ttl time.Duration
|
||||
mu sync.RWMutex
|
||||
entries map[string]Entry
|
||||
path string
|
||||
ttl time.Duration
|
||||
refreshStamp string // path to last_refresh timestamp file
|
||||
}
|
||||
|
||||
type diskFormat struct {
|
||||
@@ -31,11 +32,48 @@ type diskFormat struct {
|
||||
}
|
||||
|
||||
func New(path string, ttlSeconds int) *Cache {
|
||||
return &Cache{
|
||||
entries: make(map[string]Entry),
|
||||
path: path,
|
||||
ttl: time.Duration(ttlSeconds) * time.Second,
|
||||
stamp := ""
|
||||
if path != "" {
|
||||
stamp = filepath.Join(filepath.Dir(path), "last_refresh")
|
||||
}
|
||||
return &Cache{
|
||||
entries: make(map[string]Entry),
|
||||
path: path,
|
||||
ttl: time.Duration(ttlSeconds) * time.Second,
|
||||
refreshStamp: stamp,
|
||||
}
|
||||
}
|
||||
|
||||
// NeedsRefresh reports whether the last full cache refresh (via `cache refresh`)
|
||||
// is older than d, or has never happened. Always returns false when no path is set.
|
||||
func (c *Cache) NeedsRefresh(d time.Duration) bool {
|
||||
if c.refreshStamp == "" {
|
||||
return false
|
||||
}
|
||||
data, err := os.ReadFile(c.refreshStamp)
|
||||
if err != nil {
|
||||
return true // file missing → never refreshed
|
||||
}
|
||||
var t time.Time
|
||||
if err := t.UnmarshalText(data); err != nil {
|
||||
return true
|
||||
}
|
||||
return time.Since(t) >= d
|
||||
}
|
||||
|
||||
// SetRefreshed records the current time as the last successful full refresh.
|
||||
func (c *Cache) SetRefreshed() error {
|
||||
if c.refreshStamp == "" {
|
||||
return nil
|
||||
}
|
||||
data, err := time.Now().MarshalText()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(c.refreshStamp), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(c.refreshStamp, data, 0o644)
|
||||
}
|
||||
|
||||
func (c *Cache) Load() error {
|
||||
@@ -137,6 +175,21 @@ func (c *Cache) Search(prefix string) []Entry {
|
||||
return out
|
||||
}
|
||||
|
||||
// GetByShortcut scans all entries and returns the first whose normalized name matches
|
||||
// the normalized shortcut. Returns (entry, found, fresh).
|
||||
func (c *Cache) GetByShortcut(shortcut string, normalize func(string) string) (entry Entry, found bool, fresh bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
norm := normalize(shortcut)
|
||||
for _, e := range c.entries {
|
||||
if normalize(e.Name) == norm {
|
||||
isFresh := c.ttl > 0 && time.Since(e.CachedAt) < c.ttl
|
||||
return e, true, isFresh
|
||||
}
|
||||
}
|
||||
return Entry{}, false, false
|
||||
}
|
||||
|
||||
// Get returns an entry and reports whether it is still within the TTL.
|
||||
func (c *Cache) Get(name string) (entry Entry, fresh bool) {
|
||||
c.mu.RLock()
|
||||
|
||||
Reference in New Issue
Block a user