-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (79 loc) · 2.29 KB
/
main.go
File metadata and controls
92 lines (79 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// ProxyLauncher - A utility to launch target applications with configurable options
package main
import (
"flag"
"os"
"os/exec"
"path/filepath"
"runtime"
)
// Function variables that can be overridden in tests
var (
// execCommand is a variable wrapping exec.Command for testing
execCommand = exec.Command
// fileExistsFunc is a function to check if a file exists
fileExistsFunc = func(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
// Check that it exists and is a regular file (not a directory)
return !info.IsDir()
}
)
func main() {
// Parse command-line flags
configPath := flag.String("config", "", "Path to config file")
flag.Parse()
// Determine config path (defaults to executable directory)
cfgPath := *configPath
if cfgPath == "" {
execPath, err := os.Executable()
if err != nil {
showErrorMessageBox("Failed to determine executable path: " + err.Error())
return
}
cfgPath = filepath.Join(filepath.Dir(execPath), "proxylauncher.cfg")
}
// Check if config file exists
if !fileExists(cfgPath) {
// Create default config file
if err := createDefaultConfig(cfgPath); err != nil {
showErrorMessageBox("Failed to create default configuration file: " + err.Error())
return
}
// Open the config file with the system default editor
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("notepad.exe", cfgPath)
case "darwin": // macOS
cmd = exec.Command("open", cfgPath)
default: // Linux and others
cmd = exec.Command("xdg-open", cfgPath)
}
if err := cmd.Start(); err != nil {
showErrorMessageBox("Failed to open new default configuration file: " + err.Error())
return
}
// Show message box and exit
showInfoMessageBox("No configuration file found. A default configuration has been created. Please edit it to your needs and restart the application.")
return
}
// Load configuration
config, err := loadConfig(cfgPath)
if err != nil {
showErrorMessageBox(err.Error())
return
}
// Create launcher
launcher := NewLauncher(config)
// Launch target
if err := launcher.Launch(); err != nil {
showErrorMessageBox(err.Error())
}
}
// fileExists checks if a file exists and is a regular file (not a directory)
func fileExists(path string) bool {
return fileExistsFunc(path)
}