High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
implement config file support (P6 feature 4/4)
bota616967da99c
diff --git a/README.md b/README.mdindex 606aa476..0b9d4d6b 100644--- a/README.md+++ b/README.md@@ -1,5 +1,5 @@<p align="center">- <img src="assets/pinchtab-mascot-transparent.png" alt="pinchtab" width="200"/>+ <img src="assets/pinchtab-mascot.png" alt="pinchtab" width="200"/></p><p align="center">diff --git a/assets/pinchtab-mascot-transparent.png b/assets/pinchtab-mascot-transparent.pngdeleted file mode 100644index f8b43d78..00000000Binary files a/assets/pinchtab-mascot-transparent.png and /dev/null differdiff --git a/assets/pinchtab-mascot.png b/assets/pinchtab-mascot.pngnew file mode 100644index 00000000..1bcae91aBinary files /dev/null and b/assets/pinchtab-mascot.png differdiff --git a/config.go b/config.goindex 28f005a1..d4521fa8 100644--- a/config.go+++ b/config.go@@ -2,6 +2,8 @@ package mainimport (_ "embed"+ "encoding/json"+ "fmt""os""path/filepath""time"@@ -65,3 +67,146 @@ func homeDir() string {h, _ := os.UserHomeDir()return h}++// Config represents the configuration file structure+type Config struct {+ Port string `json:"port"`+ CdpURL string `json:"cdpUrl,omitempty"`+ Token string `json:"token,omitempty"`+ StateDir string `json:"stateDir"`+ ProfileDir string `json:"profileDir"`+ Headless bool `json:"headless"`+ NoRestore bool `json:"noRestore"`+ TimeoutSec int `json:"timeoutSec,omitempty"`+ NavigateSec int `json:"navigateSec,omitempty"`+}++// loadConfig loads configuration from file or environment variables+func loadConfig() {+ // Default config file location+ configPath := envOr("BRIDGE_CONFIG", filepath.Join(homeDir(), ".pinchtab", "config.json"))++ // Try to load config file+ if data, err := os.ReadFile(configPath); err == nil {+ var cfg Config+ if err := json.Unmarshal(data, &cfg); err == nil {+ // Apply config file values (env vars take precedence)+ if cfg.Port != "" && os.Getenv("BRIDGE_PORT") == "" {+ port = cfg.Port+ }+ if cfg.CdpURL != "" && os.Getenv("CDP_URL") == "" {+ cdpURL = cfg.CdpURL+ }+ if cfg.Token != "" && os.Getenv("BRIDGE_TOKEN") == "" {+ token = cfg.Token+ }+ if cfg.StateDir != "" && os.Getenv("BRIDGE_STATE_DIR") == "" {+ stateDir = cfg.StateDir+ }+ if cfg.ProfileDir != "" && os.Getenv("BRIDGE_PROFILE") == "" {+ profileDir = cfg.ProfileDir+ }+ if cfg.Headless && os.Getenv("BRIDGE_HEADLESS") == "" {+ headless = true+ }+ if cfg.NoRestore && os.Getenv("BRIDGE_NO_RESTORE") == "" {+ noRestore = true+ }+ if cfg.TimeoutSec > 0 && os.Getenv("BRIDGE_TIMEOUT") == "" {+ actionTimeout = time.Duration(cfg.TimeoutSec) * time.Second+ }+ if cfg.NavigateSec > 0 && os.Getenv("BRIDGE_NAV_TIMEOUT") == "" {+ navigateTimeout = time.Duration(cfg.NavigateSec) * time.Second+ }+ }+ }+}++// defaultConfig returns a default configuration+func defaultConfig() Config {+ return Config{+ Port: "9867",+ StateDir: filepath.Join(homeDir(), ".pinchtab"),+ ProfileDir: filepath.Join(homeDir(), ".pinchtab", "chrome-profile"),+ Headless: true,+ NoRestore: false,+ TimeoutSec: 15,+ NavigateSec: 30,+ }+}++// handleConfigCommand handles the 'config' subcommand+func handleConfigCommand() {+ if len(os.Args) < 3 {+ fmt.Println("Usage: pinchtab config <command>")+ fmt.Println("Commands:")+ fmt.Println(" init - Create default config file")+ fmt.Println(" show - Show current configuration")+ return+ }++ switch os.Args[2] {+ case "init":+ configPath := filepath.Join(homeDir(), ".pinchtab", "config.json")++ // Check if config already exists+ if _, err := os.Stat(configPath); err == nil {+ fmt.Printf("Config file already exists at %s\n", configPath)+ fmt.Print("Overwrite? (y/N): ")+ var response string+ fmt.Scanln(&response)+ if response != "y" && response != "Y" {+ return+ }+ }++ // Create directory if needed+ if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {+ fmt.Printf("Error creating directory: %v\n", err)+ os.Exit(1)+ }++ // Write default config+ cfg := defaultConfig()+ data, _ := json.MarshalIndent(cfg, "", " ")+ if err := os.WriteFile(configPath, data, 0644); err != nil {+ fmt.Printf("Error writing config: %v\n", err)+ os.Exit(1)+ }++ fmt.Printf("Config file created at %s\n", configPath)+ fmt.Println("\nExample with auth token:")+ fmt.Println(`{+ "port": "9867",+ "token": "your-secret-token",+ "headless": true,+ "stateDir": "` + cfg.StateDir + `",+ "profileDir": "` + cfg.ProfileDir + `"+}`)++ case "show":+ fmt.Println("Current configuration:")+ fmt.Printf(" Port: %s\n", port)+ fmt.Printf(" CDP URL: %s\n", cdpURL)+ fmt.Printf(" Token: %s\n", maskToken(token))+ fmt.Printf(" State Dir: %s\n", stateDir)+ fmt.Printf(" Profile: %s\n", profileDir)+ fmt.Printf(" Headless: %v\n", headless)+ fmt.Printf(" No Restore: %v\n", noRestore)+ fmt.Printf(" Timeouts: action=%v navigate=%v\n", actionTimeout, navigateTimeout)++ default:+ fmt.Printf("Unknown command: %s\n", os.Args[2])+ os.Exit(1)+ }+}++func maskToken(t string) string {+ if t == "" {+ return "(none)"+ }+ if len(t) <= 8 {+ return "***"+ }+ return t[:4] + "..." + t[len(t)-4:]+}diff --git a/main.go b/main.goindex 3bdf13e9..c9d05dde 100644--- a/main.go+++ b/main.go@@ -19,11 +19,20 @@ var version = "dev"var bridge Bridgefunc main() {+ // Load configuration from file or environment+ loadConfig()+if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {fmt.Printf("pinchtab %s\n", version)os.Exit(0)}+ // Handle config generation+ if len(os.Args) > 1 && os.Args[1] == "config" {+ handleConfigCommand()+ os.Exit(0)+ }+if err := os.MkdirAll(stateDir, 0755); err != nil {slog.Error("cannot create state dir", "err", err)os.Exit(1)