feat: add persistent proxy profile management
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"proxyctl/internal/app"
|
||||
"proxyctl/internal/profile"
|
||||
"proxyctl/internal/tui"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(os.Args[1:], os.Stdin, os.Stdout); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "proxyctl:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(args []string, input io.Reader, output io.Writer) error {
|
||||
if len(args) != 0 {
|
||||
return fmt.Errorf("this build supports no command-line arguments")
|
||||
}
|
||||
path, err := profile.ConfigPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
application, err := app.New(profile.NewFileStore(path))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
program := tea.NewProgram(tui.NewModel(application), tea.WithInput(input), tea.WithOutput(output))
|
||||
_, err = program.Run()
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefaultInvocationStartsTUIWithNoProfileState(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
var output bytes.Buffer
|
||||
if err := run(nil, strings.NewReader("q"), &output); err != nil {
|
||||
t.Fatalf("run proxyctl: %v", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "No Active Profile") {
|
||||
t.Fatalf("default TUI output missing no-Profile state: %q", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultInvocationLoadsPersistedActiveProfile(t *testing.T) {
|
||||
xdg := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", xdg)
|
||||
writeConfig(t, filepath.Join(xdg, "proxyctl", "profiles.json"), `{
|
||||
"profiles": [{"name":"office","mode":"single","singleUrl":"http://proxy.example:8080","httpUrl":"http://proxy.example:8080","httpsUrl":"http://proxy.example:8080","allUrl":"http://proxy.example:8080"}],
|
||||
"activeProfile": "office"
|
||||
}`)
|
||||
var output bytes.Buffer
|
||||
if err := run(nil, strings.NewReader("q"), &output); err != nil {
|
||||
t.Fatalf("run proxyctl: %v", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "Active Profile: office") {
|
||||
t.Fatalf("default TUI output missing Active Profile: %q", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func writeConfig(t *testing.T, path, contents string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user