Compare commits

..
4 Commits
Author SHA1 Message Date
lukaszraczylo 9598444d56 Add automatic wrapping of the content based on terminal width 2025-11-29 01:59:23 +00:00
lukaszraczylo 1b832e2fcf Fix taps confusion. 2025-11-29 01:34:17 +00:00
lukaszraczylo 005ff27392 Add more tests. 2025-11-28 15:29:40 +00:00
lukaszraczylo 929fa454e7 Add MIT license file. 2025-11-28 14:35:34 +00:00
13 changed files with 1524 additions and 26 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ release:
brews:
- repository:
owner: lukaszraczylo
name: brew-taps
name: homebrew-taps
token: "{{ .Env.HOMEBREW_TAP_TOKEN }}"
directory: Formula
homepage: https://github.com/lukaszraczylo/lolcathost
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Lukasz Raczylo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+1 -1
View File
@@ -53,7 +53,7 @@ lolcathost manages your `/etc/hosts` file with an interactive terminal interface
### Homebrew (macOS/Linux)
```bash
brew install lukaszraczylo/brew-taps/lolcathost
brew install lukaszraczylo/taps/lolcathost
```
After Homebrew installation, run:
+1 -1
View File
@@ -278,7 +278,7 @@
<i class="fas fa-beer mr-2 text-amber-500"></i>
Homebrew (macOS/Linux)
</h3>
<pre class="bg-gray-900 text-gray-100 p-4 rounded-lg overflow-x-auto"><code>brew install lukaszraczylo/brew-taps/lolcathost
<pre class="bg-gray-900 text-gray-100 p-4 rounded-lg overflow-x-auto"><code>brew install lukaszraczylo/taps/lolcathost
sudo lolcathost --install</code></pre>
</div>
<div class="glass p-6 rounded-xl">
+270
View File
@@ -474,6 +474,276 @@ func TestClient_RequestTypes_Matrix(t *testing.T) {
}
}
func TestClient_Add(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestAdd {
var payload protocol.AddPayload
req.ParsePayload(&payload)
assert.Equal(t, "test.local", payload.Domain)
assert.Equal(t, "127.0.0.1", payload.IP)
assert.Equal(t, "test-local", payload.Alias)
assert.Equal(t, "dev", payload.Group)
assert.True(t, payload.Enabled)
resp, _ := protocol.NewOKResponse(protocol.SetData{Domain: payload.Domain, Applied: true})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
data, err := client.Add("test.local", "127.0.0.1", "test-local", "dev", true)
assert.NoError(t, err)
assert.Equal(t, "test.local", data.Domain)
assert.True(t, data.Applied)
}
func TestClient_Delete(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestDelete {
var payload protocol.DeletePayload
req.ParsePayload(&payload)
assert.Equal(t, "test-alias", payload.Alias)
resp, _ := protocol.NewOKResponse(map[string]string{"deleted": payload.Alias})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
err = client.Delete("test-alias")
assert.NoError(t, err)
}
func TestClient_AddGroup(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestAddGroup {
var payload protocol.GroupPayload
req.ParsePayload(&payload)
assert.Equal(t, "newgroup", payload.Name)
resp, _ := protocol.NewOKResponse(map[string]string{"added": payload.Name})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
err = client.AddGroup("newgroup")
assert.NoError(t, err)
}
func TestClient_DeleteGroup(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestDeleteGroup {
var payload protocol.GroupPayload
req.ParsePayload(&payload)
assert.Equal(t, "todelete", payload.Name)
resp, _ := protocol.NewOKResponse(map[string]string{"deleted": payload.Name})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
err = client.DeleteGroup("todelete")
assert.NoError(t, err)
}
func TestClient_RenameGroup(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestRenameGroup {
var payload protocol.RenameGroupPayload
req.ParsePayload(&payload)
assert.Equal(t, "oldname", payload.OldName)
assert.Equal(t, "newname", payload.NewName)
resp, _ := protocol.NewOKResponse(map[string]string{"renamed": payload.NewName})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
err = client.RenameGroup("oldname", "newname")
assert.NoError(t, err)
}
func TestClient_ListGroups(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestListGroups {
resp, _ := protocol.NewOKResponse(protocol.GroupsData{
Groups: []string{"dev", "staging", "prod"},
})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
groups, err := client.ListGroups()
require.NoError(t, err)
assert.Equal(t, []string{"dev", "staging", "prod"}, groups)
}
func TestClient_GetBackupContent(t *testing.T) {
server := newMockServer(t)
defer server.close()
expectedContent := "127.0.0.1\tlocalhost\n"
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestBackupContent {
var payload protocol.BackupContentPayload
req.ParsePayload(&payload)
assert.Equal(t, "hosts.backup.bak", payload.BackupName)
resp, _ := protocol.NewOKResponse(protocol.BackupContentData{
Content: expectedContent,
})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
content, err := client.GetBackupContent("hosts.backup.bak")
require.NoError(t, err)
assert.Equal(t, expectedContent, content)
}
func TestClient_AddPreset(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestAddPreset {
var payload protocol.AddPresetPayload
req.ParsePayload(&payload)
assert.Equal(t, "newpreset", payload.Name)
assert.Equal(t, []string{"a", "b"}, payload.Enable)
assert.Equal(t, []string{"c"}, payload.Disable)
resp, _ := protocol.NewOKResponse(map[string]string{"added": payload.Name})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
err = client.AddPreset("newpreset", []string{"a", "b"}, []string{"c"})
assert.NoError(t, err)
}
func TestClient_DeletePreset(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestDeletePreset {
var payload protocol.PresetPayload
req.ParsePayload(&payload)
assert.Equal(t, "todelete", payload.Name)
resp, _ := protocol.NewOKResponse(map[string]string{"deleted": payload.Name})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
err = client.DeletePreset("todelete")
assert.NoError(t, err)
}
func TestClient_ListPresets(t *testing.T) {
server := newMockServer(t)
defer server.close()
server.handler = func(req *protocol.Request) *protocol.Response {
if req.Type == protocol.RequestListPresets {
resp, _ := protocol.NewOKResponse(protocol.PresetsData{
Presets: []protocol.PresetInfo{
{Name: "local", Enable: []string{"a"}, Disable: []string{"b"}},
{Name: "staging", Enable: []string{"b"}, Disable: []string{"a"}},
},
})
return resp
}
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "unexpected")
}
client := New(server.path)
err := client.Connect()
require.NoError(t, err)
defer client.Close()
presets, err := client.ListPresets()
require.NoError(t, err)
assert.Len(t, presets, 2)
assert.Equal(t, "local", presets[0].Name)
assert.Equal(t, "staging", presets[1].Name)
}
func BenchmarkClient_Ping(b *testing.B) {
tmpDir := b.TempDir()
socketPath := filepath.Join(tmpDir, "bench.sock")
+325
View File
@@ -265,3 +265,328 @@ func TestFlushMethod(t *testing.T) {
})
}
}
func TestDefaultConfigDir(t *testing.T) {
dir := DefaultConfigDir()
assert.NotEmpty(t, dir)
assert.Contains(t, dir, ".config/lolcathost")
}
func TestDefaultConfigPath(t *testing.T) {
path := DefaultConfigPath()
assert.NotEmpty(t, path)
assert.Contains(t, path, "config.yaml")
}
func TestConfig_GenerateAlias(t *testing.T) {
cfg := &Config{
Groups: []Group{
{
Name: "dev",
Hosts: []Host{
{Domain: "existing.com", IP: "127.0.0.1", Alias: "existing-com", Enabled: true},
},
},
},
}
t.Run("simple domain", func(t *testing.T) {
alias := cfg.GenerateAlias("newdomain.com")
assert.Equal(t, "newdomain-com", alias)
})
t.Run("domain with underscore", func(t *testing.T) {
alias := cfg.GenerateAlias("my_app.test")
assert.Equal(t, "my-app-test", alias)
})
t.Run("duplicate generates numbered alias", func(t *testing.T) {
alias := cfg.GenerateAlias("existing.com")
assert.Equal(t, "existing-com-2", alias)
})
}
func TestConfig_AddHost(t *testing.T) {
t.Run("add to existing group", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{}},
},
}
err := cfg.AddHost("test.local", "127.0.0.1", "test-local", "dev", true)
require.NoError(t, err)
assert.Len(t, cfg.Groups[0].Hosts, 1)
assert.Equal(t, "test.local", cfg.Groups[0].Hosts[0].Domain)
})
t.Run("add to new group", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
err := cfg.AddHost("test.local", "127.0.0.1", "test-local", "newgroup", true)
require.NoError(t, err)
assert.Len(t, cfg.Groups, 1)
assert.Equal(t, "newgroup", cfg.Groups[0].Name)
})
t.Run("auto-generate alias", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
err := cfg.AddHost("auto.test", "127.0.0.1", "", "dev", true)
require.NoError(t, err)
assert.Equal(t, "auto-test", cfg.Groups[0].Hosts[0].Alias)
})
t.Run("duplicate alias error", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{{Domain: "a.com", IP: "127.0.0.1", Alias: "existing"}}},
},
}
err := cfg.AddHost("b.com", "127.0.0.1", "existing", "dev", true)
assert.Error(t, err)
assert.Contains(t, err.Error(), "alias already exists")
})
}
func TestConfig_AddGroup(t *testing.T) {
t.Run("add new group", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
err := cfg.AddGroup("newgroup")
require.NoError(t, err)
assert.Len(t, cfg.Groups, 1)
assert.Equal(t, "newgroup", cfg.Groups[0].Name)
})
t.Run("duplicate group error", func(t *testing.T) {
cfg := &Config{Groups: []Group{{Name: "existing"}}}
err := cfg.AddGroup("existing")
assert.Error(t, err)
assert.Contains(t, err.Error(), "group already exists")
})
}
func TestConfig_DeleteGroup(t *testing.T) {
t.Run("delete existing group", func(t *testing.T) {
cfg := &Config{Groups: []Group{{Name: "todelete"}, {Name: "keep"}}}
err := cfg.DeleteGroup("todelete")
require.NoError(t, err)
assert.Len(t, cfg.Groups, 1)
assert.Equal(t, "keep", cfg.Groups[0].Name)
})
t.Run("delete nonexistent group", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
err := cfg.DeleteGroup("nonexistent")
assert.Error(t, err)
assert.Contains(t, err.Error(), "group not found")
})
}
func TestConfig_RenameGroup(t *testing.T) {
t.Run("rename existing group", func(t *testing.T) {
cfg := &Config{Groups: []Group{{Name: "oldname"}}}
err := cfg.RenameGroup("oldname", "newname")
require.NoError(t, err)
assert.Equal(t, "newname", cfg.Groups[0].Name)
})
t.Run("rename to existing name error", func(t *testing.T) {
cfg := &Config{Groups: []Group{{Name: "a"}, {Name: "b"}}}
err := cfg.RenameGroup("a", "b")
assert.Error(t, err)
assert.Contains(t, err.Error(), "group already exists")
})
t.Run("rename nonexistent group", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
err := cfg.RenameGroup("nonexistent", "newname")
assert.Error(t, err)
assert.Contains(t, err.Error(), "group not found")
})
}
func TestConfig_GetGroups(t *testing.T) {
cfg := &Config{Groups: []Group{{Name: "a"}, {Name: "b"}, {Name: "c"}}}
groups := cfg.GetGroups()
assert.Equal(t, []string{"a", "b", "c"}, groups)
}
func TestConfig_DeleteHost(t *testing.T) {
t.Run("delete existing host", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{
{Domain: "a.com", Alias: "a"},
{Domain: "b.com", Alias: "b"},
}},
},
}
result := cfg.DeleteHost("a")
assert.True(t, result)
assert.Len(t, cfg.Groups[0].Hosts, 1)
assert.Equal(t, "b", cfg.Groups[0].Hosts[0].Alias)
})
t.Run("delete nonexistent host", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
result := cfg.DeleteHost("nonexistent")
assert.False(t, result)
})
}
func TestConfig_UpdateHost(t *testing.T) {
t.Run("update in same group", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{{Domain: "old.com", IP: "127.0.0.1", Alias: "test"}}},
},
}
err := cfg.UpdateHost("test", "new.com", "192.168.1.1", "test", "dev")
require.NoError(t, err)
assert.Equal(t, "new.com", cfg.Groups[0].Hosts[0].Domain)
assert.Equal(t, "192.168.1.1", cfg.Groups[0].Hosts[0].IP)
})
t.Run("move to different group", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "source", Hosts: []Host{{Domain: "a.com", IP: "127.0.0.1", Alias: "test"}}},
{Name: "target", Hosts: []Host{}},
},
}
err := cfg.UpdateHost("test", "a.com", "127.0.0.1", "test", "target")
require.NoError(t, err)
assert.Len(t, cfg.Groups[0].Hosts, 0)
assert.Len(t, cfg.Groups[1].Hosts, 1)
})
t.Run("move to new group", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "source", Hosts: []Host{{Domain: "a.com", IP: "127.0.0.1", Alias: "test"}}},
},
}
err := cfg.UpdateHost("test", "a.com", "127.0.0.1", "test", "newgroup")
require.NoError(t, err)
assert.Len(t, cfg.Groups, 2)
assert.Equal(t, "newgroup", cfg.Groups[1].Name)
})
t.Run("change alias", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{{Domain: "a.com", IP: "127.0.0.1", Alias: "old"}}},
},
}
err := cfg.UpdateHost("old", "a.com", "127.0.0.1", "new", "dev")
require.NoError(t, err)
assert.Equal(t, "new", cfg.Groups[0].Hosts[0].Alias)
})
t.Run("alias conflict error", func(t *testing.T) {
cfg := &Config{
Groups: []Group{
{Name: "dev", Hosts: []Host{
{Domain: "a.com", Alias: "a"},
{Domain: "b.com", Alias: "b"},
}},
},
}
err := cfg.UpdateHost("a", "a.com", "127.0.0.1", "b", "dev")
assert.Error(t, err)
assert.Contains(t, err.Error(), "alias already exists")
})
t.Run("host not found error", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
err := cfg.UpdateHost("nonexistent", "a.com", "127.0.0.1", "new", "dev")
assert.Error(t, err)
assert.Contains(t, err.Error(), "alias not found")
})
}
func TestConfig_AddPreset(t *testing.T) {
t.Run("add new preset", func(t *testing.T) {
cfg := &Config{Presets: []Preset{}}
err := cfg.AddPreset("newpreset", []string{"a"}, []string{"b"})
require.NoError(t, err)
assert.Len(t, cfg.Presets, 1)
assert.Equal(t, "newpreset", cfg.Presets[0].Name)
})
t.Run("duplicate preset error", func(t *testing.T) {
cfg := &Config{Presets: []Preset{{Name: "existing"}}}
err := cfg.AddPreset("existing", nil, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "preset already exists")
})
}
func TestConfig_DeletePreset(t *testing.T) {
t.Run("delete existing preset", func(t *testing.T) {
cfg := &Config{Presets: []Preset{{Name: "todelete"}, {Name: "keep"}}}
err := cfg.DeletePreset("todelete")
require.NoError(t, err)
assert.Len(t, cfg.Presets, 1)
assert.Equal(t, "keep", cfg.Presets[0].Name)
})
t.Run("delete nonexistent preset", func(t *testing.T) {
cfg := &Config{Presets: []Preset{}}
err := cfg.DeletePreset("nonexistent")
assert.Error(t, err)
assert.Contains(t, err.Error(), "preset not found")
})
}
func TestConfig_GetPresets(t *testing.T) {
cfg := &Config{Presets: []Preset{{Name: "a"}, {Name: "b"}}}
presets := cfg.GetPresets()
assert.Len(t, presets, 2)
}
func TestConfig_EnsureDefaultGroup(t *testing.T) {
t.Run("creates default when empty", func(t *testing.T) {
cfg := &Config{Groups: []Group{}}
cfg.EnsureDefaultGroup()
assert.Len(t, cfg.Groups, 1)
assert.Equal(t, "default", cfg.Groups[0].Name)
})
t.Run("does nothing when groups exist", func(t *testing.T) {
cfg := &Config{Groups: []Group{{Name: "existing"}}}
cfg.EnsureDefaultGroup()
assert.Len(t, cfg.Groups, 1)
assert.Equal(t, "existing", cfg.Groups[0].Name)
})
}
func TestManager_Watch(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
err := CreateDefault(configPath)
require.NoError(t, err)
manager := NewManager(configPath)
err = manager.Load()
require.NoError(t, err)
changeCh := make(chan *Config, 1)
err = manager.Watch(func(cfg *Config) {
changeCh <- cfg
})
require.NoError(t, err)
// Stop the watcher
manager.Stop()
}
func TestManager_Save_NoConfig(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
manager := NewManager(configPath)
// Don't load, so config is nil
err := manager.Save()
assert.Error(t, err)
assert.Contains(t, err.Error(), "no config loaded")
}
+756
View File
@@ -0,0 +1,756 @@
package daemon
import (
"encoding/json"
"net"
"os"
"path/filepath"
"testing"
"time"
"github.com/lukaszraczylo/lolcathost/internal/config"
"github.com/lukaszraczylo/lolcathost/internal/protocol"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupTestServer(t *testing.T) (*Server, string, func()) {
tmpDir := t.TempDir()
socketPath := filepath.Join(tmpDir, "test.sock")
configPath := filepath.Join(tmpDir, "config.yaml")
hostsPath := filepath.Join(tmpDir, "hosts")
backupDir := filepath.Join(tmpDir, "backups")
// Create initial hosts file
err := os.WriteFile(hostsPath, []byte("127.0.0.1\tlocalhost\n"), 0644)
require.NoError(t, err)
// Create config
err = config.CreateDefault(configPath)
require.NoError(t, err)
cfgManager := config.NewManager(configPath)
err = cfgManager.Load()
require.NoError(t, err)
server := &Server{
socketPath: socketPath,
config: cfgManager,
hosts: NewHostsManagerWithPaths(hostsPath, backupDir),
flusher: NewDNSFlusher(FlushMethodAuto),
rateLimiter: NewRateLimiter(100, time.Minute),
stopCh: make(chan struct{}),
}
cleanup := func() {
server.Stop()
}
return server, tmpDir, cleanup
}
func TestServer_HandlePing(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
resp := server.handlePing()
assert.Equal(t, "ok", resp.Status)
}
func TestServer_HandleStatus(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
resp := server.handleStatus()
assert.Equal(t, "ok", resp.Status)
var data protocol.StatusData
err := resp.ParseData(&data)
require.NoError(t, err)
assert.True(t, data.Running)
}
func TestServer_HandleList(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
resp := server.handleList()
assert.Equal(t, "ok", resp.Status)
var data protocol.ListData
err := resp.ParseData(&data)
require.NoError(t, err)
assert.NotNil(t, data.Entries)
}
func TestServer_HandleSet(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// First add a host to set
cfg := server.config.Get()
cfg.AddHost("test.local", "127.0.0.1", "test-local", "default", false)
server.config.Save()
t.Run("enable host", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestSet, protocol.SetPayload{
Alias: "test-local",
Enabled: true,
})
resp := server.handleSet(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("disable host", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestSet, protocol.SetPayload{
Alias: "test-local",
Enabled: false,
})
resp := server.handleSet(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("nonexistent host", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestSet, protocol.SetPayload{
Alias: "nonexistent",
Enabled: true,
})
resp := server.handleSet(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeNotFound, resp.Code)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestSet,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleSet(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleAdd(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
t.Run("valid host", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAdd, protocol.AddPayload{
Domain: "newhost.local",
IP: "127.0.0.1",
Group: "default",
})
resp := server.handleAdd(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("duplicate alias", func(t *testing.T) {
// When alias is explicitly provided, duplicates are rejected
req, _ := protocol.NewRequest(protocol.RequestAdd, protocol.AddPayload{
Domain: "another.local",
IP: "127.0.0.1",
Alias: "newhost-local", // Same alias as auto-generated for newhost.local
Group: "default",
})
resp := server.handleAdd(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeConflict, resp.Code)
})
t.Run("blocked domain", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAdd, protocol.AddPayload{
Domain: "apple.com",
IP: "127.0.0.1",
Group: "default",
})
resp := server.handleAdd(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeBlockedDomain, resp.Code)
})
t.Run("invalid domain", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAdd, protocol.AddPayload{
Domain: "",
IP: "127.0.0.1",
Group: "default",
})
resp := server.handleAdd(req)
assert.Equal(t, "error", resp.Status)
})
t.Run("empty IP", func(t *testing.T) {
// Only empty IP is rejected, format is not validated
req, _ := protocol.NewRequest(protocol.RequestAdd, protocol.AddPayload{
Domain: "valid.local",
IP: "",
Group: "default",
})
resp := server.handleAdd(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeInvalidIP, resp.Code)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestAdd,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleAdd(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleDelete(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Add a host first
cfg := server.config.Get()
cfg.AddHost("todelete.local", "127.0.0.1", "todelete", "default", false)
server.config.Save()
t.Run("delete existing", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestDelete, protocol.DeletePayload{
Alias: "todelete",
})
resp := server.handleDelete(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("delete nonexistent", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestDelete, protocol.DeletePayload{
Alias: "nonexistent",
})
resp := server.handleDelete(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeNotFound, resp.Code)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestDelete,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleDelete(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleSync(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
resp := server.handleSync()
assert.Equal(t, "ok", resp.Status)
}
func TestServer_HandleBackups(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Create a backup first
server.hosts.CreateBackup()
resp := server.handleBackups()
assert.Equal(t, "ok", resp.Status)
var data protocol.BackupsData
err := resp.ParseData(&data)
require.NoError(t, err)
assert.NotNil(t, data.Backups)
}
func TestServer_HandleAddGroup(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
t.Run("add new group", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAddGroup, protocol.GroupPayload{
Name: "newgroup",
})
resp := server.handleAddGroup(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("add duplicate group", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAddGroup, protocol.GroupPayload{
Name: "newgroup",
})
resp := server.handleAddGroup(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeConflict, resp.Code)
})
t.Run("empty name", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAddGroup, protocol.GroupPayload{
Name: "",
})
resp := server.handleAddGroup(req)
assert.Equal(t, "error", resp.Status)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestAddGroup,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleAddGroup(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleDeleteGroup(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Add a group first
cfg := server.config.Get()
cfg.AddGroup("todeletegroup")
server.config.Save()
t.Run("delete existing group", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestDeleteGroup, protocol.GroupPayload{
Name: "todeletegroup",
})
resp := server.handleDeleteGroup(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("delete nonexistent group", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestDeleteGroup, protocol.GroupPayload{
Name: "nonexistent",
})
resp := server.handleDeleteGroup(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeNotFound, resp.Code)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestDeleteGroup,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleDeleteGroup(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleListGroups(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
resp := server.handleListGroups()
assert.Equal(t, "ok", resp.Status)
var data protocol.GroupsData
err := resp.ParseData(&data)
require.NoError(t, err)
assert.NotNil(t, data.Groups)
}
func TestServer_HandleRenameGroup(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Add a group to rename
cfg := server.config.Get()
cfg.AddGroup("oldname")
server.config.Save()
t.Run("rename existing group", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestRenameGroup, protocol.RenameGroupPayload{
OldName: "oldname",
NewName: "newname",
})
resp := server.handleRenameGroup(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("rename nonexistent group", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestRenameGroup, protocol.RenameGroupPayload{
OldName: "nonexistent",
NewName: "newname2",
})
resp := server.handleRenameGroup(req)
assert.Equal(t, "error", resp.Status)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestRenameGroup,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleRenameGroup(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleAddPreset(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
t.Run("add new preset", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAddPreset, protocol.AddPresetPayload{
Name: "newpreset",
Enable: []string{"alias1"},
Disable: []string{"alias2"},
})
resp := server.handleAddPreset(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("add duplicate preset", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAddPreset, protocol.AddPresetPayload{
Name: "newpreset",
Enable: []string{"alias1"},
Disable: []string{"alias2"},
})
resp := server.handleAddPreset(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeConflict, resp.Code)
})
t.Run("empty name", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestAddPreset, protocol.AddPresetPayload{
Name: "",
})
resp := server.handleAddPreset(req)
assert.Equal(t, "error", resp.Status)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestAddPreset,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleAddPreset(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleDeletePreset(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Add a preset first
cfg := server.config.Get()
cfg.AddPreset("todeletepreset", []string{"a"}, []string{"b"})
server.config.Save()
t.Run("delete existing preset", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestDeletePreset, protocol.PresetPayload{
Name: "todeletepreset",
})
resp := server.handleDeletePreset(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("delete nonexistent preset", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestDeletePreset, protocol.PresetPayload{
Name: "nonexistent",
})
resp := server.handleDeletePreset(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeNotFound, resp.Code)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestDeletePreset,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleDeletePreset(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleListPresets(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
resp := server.handleListPresets()
assert.Equal(t, "ok", resp.Status)
var data protocol.PresetsData
err := resp.ParseData(&data)
require.NoError(t, err)
assert.NotNil(t, data.Presets)
}
func TestServer_HandlePreset(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Add hosts and preset
cfg := server.config.Get()
cfg.AddHost("host1.local", "127.0.0.1", "host1", "default", false)
cfg.AddHost("host2.local", "127.0.0.1", "host2", "default", false)
cfg.AddPreset("testpreset", []string{"host1"}, []string{"host2"})
server.config.Save()
t.Run("apply existing preset", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestPreset, protocol.PresetPayload{
Name: "testpreset",
})
resp := server.handlePreset(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("apply nonexistent preset", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestPreset, protocol.PresetPayload{
Name: "nonexistent",
})
resp := server.handlePreset(req)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeNotFound, resp.Code)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestPreset,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handlePreset(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleRollback(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Create a backup first
server.hosts.CreateBackup()
backups, _ := server.hosts.ListBackups()
require.NotEmpty(t, backups)
t.Run("rollback to existing backup", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestRollback, protocol.RollbackPayload{
BackupName: backups[0].Name,
})
resp := server.handleRollback(req)
assert.Equal(t, "ok", resp.Status)
})
t.Run("rollback to nonexistent backup", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestRollback, protocol.RollbackPayload{
BackupName: "nonexistent.bak",
})
resp := server.handleRollback(req)
assert.Equal(t, "error", resp.Status)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestRollback,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleRollback(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleBackupContent(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
// Create a backup first
server.hosts.CreateBackup()
backups, _ := server.hosts.ListBackups()
require.NotEmpty(t, backups)
t.Run("get existing backup content", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestBackupContent, protocol.BackupContentPayload{
BackupName: backups[0].Name,
})
resp := server.handleBackupContent(req)
assert.Equal(t, "ok", resp.Status)
var data protocol.BackupContentData
err := resp.ParseData(&data)
require.NoError(t, err)
assert.NotEmpty(t, data.Content)
})
t.Run("get nonexistent backup content", func(t *testing.T) {
req, _ := protocol.NewRequest(protocol.RequestBackupContent, protocol.BackupContentPayload{
BackupName: "nonexistent.bak",
})
resp := server.handleBackupContent(req)
assert.Equal(t, "error", resp.Status)
})
t.Run("invalid payload", func(t *testing.T) {
req := &protocol.Request{
Type: protocol.RequestBackupContent,
Payload: json.RawMessage(`{invalid`),
}
resp := server.handleBackupContent(req)
assert.Equal(t, "error", resp.Status)
})
}
func TestServer_HandleRequest_UnknownType(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
req := &protocol.Request{
Type: "unknown_type",
}
creds := &PeerCredentials{UID: 0, GID: 0, PID: 1}
resp := server.handleRequest(req, creds)
assert.Equal(t, "error", resp.Status)
assert.Equal(t, protocol.ErrCodeInvalidRequest, resp.Code)
}
func TestServer_IsAuthorized(t *testing.T) {
server, _, cleanup := setupTestServer(t)
defer cleanup()
t.Run("root user", func(t *testing.T) {
creds := &PeerCredentials{UID: 0, GID: 0, PID: 1}
assert.True(t, server.isAuthorized(creds))
})
t.Run("nil credentials", func(t *testing.T) {
assert.False(t, server.isAuthorized(nil))
})
}
func TestServer_StartStop(t *testing.T) {
// Skip test if not running as root (server.Start requires root to chown socket)
if os.Getuid() != 0 {
t.Skip("Test requires root privileges to create socket with proper ownership")
}
server, _, _ := setupTestServer(t)
// Don't use cleanup since we manually call Stop
// Start server in goroutine
errCh := make(chan error, 1)
go func() {
errCh <- server.Start()
}()
// Give it time to start
time.Sleep(100 * time.Millisecond)
// Verify socket exists
_, err := os.Stat(server.socketPath)
assert.NoError(t, err)
// Stop server
err = server.Stop()
assert.NoError(t, err)
// Verify socket is removed
_, err = os.Stat(server.socketPath)
assert.True(t, os.IsNotExist(err))
}
func TestServer_AcceptConnection(t *testing.T) {
// Skip test if not running as root (server.Start requires root to chown socket)
if os.Getuid() != 0 {
t.Skip("Test requires root privileges to create socket with proper ownership")
}
server, _, _ := setupTestServer(t)
// Don't use cleanup - manually stop
// Start server
go server.Start()
time.Sleep(100 * time.Millisecond)
defer server.Stop()
// Connect to server
conn, err := net.Dial("unix", server.socketPath)
require.NoError(t, err)
defer conn.Close()
// Send ping request
req, _ := protocol.NewRequest(protocol.RequestPing, nil)
encoder := json.NewEncoder(conn)
err = encoder.Encode(req)
require.NoError(t, err)
// Read response
decoder := json.NewDecoder(conn)
var resp protocol.Response
err = decoder.Decode(&resp)
require.NoError(t, err)
assert.Equal(t, "ok", resp.Status)
}
// Benchmarks
func BenchmarkServer_HandlePing(b *testing.B) {
tmpDir := b.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
config.CreateDefault(configPath)
cfgManager := config.NewManager(configPath)
cfgManager.Load()
server := &Server{
config: cfgManager,
rateLimiter: NewRateLimiter(100000, time.Minute),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
server.handlePing()
}
}
func BenchmarkServer_HandleList(b *testing.B) {
tmpDir := b.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
config.CreateDefault(configPath)
cfgManager := config.NewManager(configPath)
cfgManager.Load()
server := &Server{
config: cfgManager,
rateLimiter: NewRateLimiter(100000, time.Minute),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
server.handleList()
}
}
func BenchmarkServer_HandleSet(b *testing.B) {
tmpDir := b.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
hostsPath := filepath.Join(tmpDir, "hosts")
backupDir := filepath.Join(tmpDir, "backups")
os.WriteFile(hostsPath, []byte("127.0.0.1\tlocalhost\n"), 0644)
config.CreateDefault(configPath)
cfgManager := config.NewManager(configPath)
cfgManager.Load()
// Add a test host
cfg := cfgManager.Get()
cfg.AddHost("bench.local", "127.0.0.1", "bench-local", "default", false)
server := &Server{
config: cfgManager,
hosts: NewHostsManagerWithPaths(hostsPath, backupDir),
flusher: NewDNSFlusher(FlushMethodAuto),
rateLimiter: NewRateLimiter(100000, time.Minute),
}
req, _ := protocol.NewRequest(protocol.RequestSet, protocol.SetPayload{
Alias: "bench-local",
Enabled: true,
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
server.handleSet(req)
}
}
+71 -16
View File
@@ -920,8 +920,15 @@ func (m *Model) View() string {
currentContent := sb.String()
currentLines := strings.Count(currentContent, "\n") + 1
// Fill space to push footer to bottom (reserve 3 lines for footer)
footerHeight := 3
// Calculate footer height dynamically (help bar lines + status bar + spacing)
footerHeight := 2 // status bar + newline before it
var helpBarContent string
if m.mode == ViewList {
helpBarContent = m.helpBar()
helpBarLines := strings.Count(helpBarContent, "\n") + 1
footerHeight += helpBarLines + 1 // +1 for newline before help bar
}
remainingLines := m.height - currentLines - footerHeight
if remainingLines > 0 {
sb.WriteString(strings.Repeat("\n", remainingLines))
@@ -930,7 +937,7 @@ func (m *Model) View() string {
// Footer (help bar + status bar)
if m.mode == ViewList {
sb.WriteString("\n")
sb.WriteString(m.helpBar())
sb.WriteString(helpBarContent)
}
sb.WriteString("\n")
sb.WriteString(m.statusBar())
@@ -939,19 +946,67 @@ func (m *Model) View() string {
}
func (m *Model) helpBar() string {
return helpBarStyle.Render(fmt.Sprintf("%s/%s: Navigate %s: Toggle %s: New %s: Edit %s: Delete %s: Presets %s: Groups %s: Backups %s: Search %s: Help %s: Quit",
helpKeyStyle.Render("↑↓"),
helpKeyStyle.Render("jk"),
helpKeyStyle.Render("Space"),
helpKeyStyle.Render("n"),
helpKeyStyle.Render("e"),
helpKeyStyle.Render("d"),
helpKeyStyle.Render("p"),
helpKeyStyle.Render("g"),
helpKeyStyle.Render("b"),
helpKeyStyle.Render("/"),
helpKeyStyle.Render("?"),
helpKeyStyle.Render("q")))
// Define help items with their display widths (without ANSI codes)
type helpItem struct {
key string
desc string
rawWidth int // width without ANSI escape codes
}
items := []helpItem{
{"↑↓/jk", "Navigate", 13},
{"Space", "Toggle", 13},
{"n", "New", 6},
{"e", "Edit", 7},
{"d", "Delete", 9},
{"p", "Presets", 10},
{"g", "Groups", 9},
{"b", "Backups", 10},
{"/", "Search", 9},
{"?", "Help", 7},
{"q", "Quit", 7},
}
separator := " "
sepWidth := 2
var lines []string
var currentLine string
var currentWidth int
for i, item := range items {
rendered := helpKeyStyle.Render(item.key) + ": " + item.desc
// Check if adding this item would exceed width
newWidth := currentWidth + item.rawWidth
if currentWidth > 0 {
newWidth += sepWidth
}
if m.width > 0 && newWidth > m.width && currentWidth > 0 {
// Start a new line
lines = append(lines, currentLine)
currentLine = rendered
currentWidth = item.rawWidth
} else {
// Add to current line
if currentWidth > 0 {
currentLine += separator
}
currentLine += rendered
currentWidth = newWidth
if i == 0 {
currentWidth = item.rawWidth
}
}
}
// Add the last line
if currentLine != "" {
lines = append(lines, currentLine)
}
return helpBarStyle.Render(strings.Join(lines, "\n"))
}
func (m *Model) statusBar() string {
+3 -2
View File
@@ -174,7 +174,7 @@ func (b *BackupPicker) selectView() string {
}
leftSb.WriteString("\n")
leftSb.WriteString(helpDescStyle.Render("↑↓ navigate • Enter restore • Esc cancel"))
leftSb.WriteString(WrapHelpText("↑↓ navigate • Enter restore • Esc cancel", 40))
// Build right panel (preview)
var rightSb strings.Builder
@@ -219,7 +219,8 @@ func (b *BackupPicker) selectView() string {
// Show scroll indicator
if len(lines) > previewHeight {
rightSb.WriteString("\n")
rightSb.WriteString(helpDescStyle.Render(fmt.Sprintf("Lines %d-%d of %d (Shift+↑↓ scroll)", b.previewScroll+1, endLine, len(lines))))
scrollText := fmt.Sprintf("%d-%d of %d (↑↓ scroll)", b.previewScroll+1, endLine, len(lines))
rightSb.WriteString(helpDescStyle.Render(scrollText))
}
}
+1 -1
View File
@@ -291,7 +291,7 @@ func (f *Form) View() string {
sb.WriteString("\n\n")
sb.WriteString("\n")
sb.WriteString(helpDescStyle.Render("Tab/↓ next • Shift+Tab/↑ prev • ←→ select group • Enter save • Esc cancel"))
sb.WriteString(WrapHelpText("Tab/↓ next • Shift+Tab/↑ prev • ←→ select group • Enter save • Esc cancel", f.width-6))
return dialogStyle.Render(sb.String())
}
+1 -1
View File
@@ -190,7 +190,7 @@ func (g *GroupPicker) selectView() string {
}
sb.WriteString("\n\n")
sb.WriteString(helpDescStyle.Render("↑↓ navigate • n new • r rename • d delete • Esc back"))
sb.WriteString(WrapHelpText("↑↓ navigate • n new • r rename • d delete • Esc back", g.width-6))
return dialogStyle.Render(sb.String())
}
+3 -3
View File
@@ -411,7 +411,7 @@ func (p *PresetPicker) selectView() string {
}
sb.WriteString("\n")
sb.WriteString(helpDescStyle.Render("↑↓ navigate • Enter apply • n new • e edit • d delete • Esc cancel"))
sb.WriteString(WrapHelpText("↑↓ navigate • Enter apply • n new • e edit • d delete • Esc cancel", p.width-6))
return dialogStyle.Render(sb.String())
}
@@ -483,7 +483,7 @@ func (p *PresetPicker) formView() string {
}
sb.WriteString("\n\n")
sb.WriteString(helpDescStyle.Render("Tab/↓ next • Enter select/save • Esc cancel"))
sb.WriteString(WrapHelpText("Tab/↓ next • Enter select/save • Esc cancel", p.width-6))
return dialogStyle.Render(sb.String())
}
@@ -536,7 +536,7 @@ func (p *PresetPicker) pickerView() string {
sb.WriteString(titleStyle.Render(title))
sb.WriteString("\n")
sb.WriteString(helpDescStyle.Render("Space to toggle • Enter to confirm • Esc to cancel"))
sb.WriteString(WrapHelpText("Space to toggle • Enter to confirm • Esc to cancel", p.width-6))
sb.WriteString("\n\n")
filtered := p.getFilteredAliases()
+70
View File
@@ -2,6 +2,8 @@
package tui
import (
"strings"
"github.com/charmbracelet/lipgloss"
)
@@ -148,3 +150,71 @@ func StatusText(enabled bool, pending bool, hasError bool) string {
func HelpItem(key, desc string) string {
return helpKeyStyle.Render(key) + " " + helpDescStyle.Render(desc)
}
// WrapHelpText wraps help text to fit within maxWidth, splitting on bullet separators.
// If maxWidth is 0 or negative, returns the original text.
func WrapHelpText(text string, maxWidth int) string {
if maxWidth <= 0 {
return helpDescStyle.Render(text)
}
separator := " • "
parts := splitOnSeparator(text, separator)
var lines []string
var currentLine string
var currentWidth int
for i, part := range parts {
partWidth := len(part)
sepWidth := 3 // len(" • ")
newWidth := currentWidth + partWidth
if currentWidth > 0 {
newWidth += sepWidth
}
if newWidth > maxWidth && currentWidth > 0 {
lines = append(lines, currentLine)
currentLine = part
currentWidth = partWidth
} else {
if currentWidth > 0 {
currentLine += separator
}
currentLine += part
if i == 0 {
currentWidth = partWidth
} else {
currentWidth = newWidth
}
}
}
if currentLine != "" {
lines = append(lines, currentLine)
}
// Apply style to each line and join
var result []string
for _, line := range lines {
result = append(result, helpDescStyle.Render(line))
}
return strings.Join(result, "\n")
}
// splitOnSeparator splits a string on the given separator.
func splitOnSeparator(s, sep string) []string {
var parts []string
for {
idx := strings.Index(s, sep)
if idx == -1 {
parts = append(parts, s)
break
}
parts = append(parts, s[:idx])
s = s[idx+len(sep):]
}
return parts
}