| 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "runtime" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | const defaultConfigFilename = "config.toml" |
| 11 | |
| 12 | type Definition struct { |
| 13 | Name string |
| 14 | DisplayName string |
| 15 | Description string |
| 16 | Executable string |
| 17 | Args []string |
| 18 | WorkingDir string |
| 19 | } |
| 20 | |
| 21 | func DefaultConfigPath() string { |
| 22 | switch runtime.GOOS { |
| 23 | case "windows": |
| 24 | return filepath.Join(windowsProgramDataDir(), "Portal Tunnel", "Agent", defaultConfigFilename) |
| 25 | case "darwin": |
| 26 | if os.Geteuid() == 0 { |
| 27 | return filepath.Join(string(filepath.Separator), "Library", "Application Support", "Portal Tunnel", "Agent", defaultConfigFilename) |
| 28 | } |
| 29 | home, err := os.UserHomeDir() |
| 30 | if err != nil { |
| 31 | return filepath.Join(".", "Library", "Application Support", "Portal Tunnel", "Agent", defaultConfigFilename) |
| 32 | } |
| 33 | return filepath.Join(home, "Library", "Application Support", "Portal Tunnel", "Agent", defaultConfigFilename) |
| 34 | default: |
| 35 | if os.Geteuid() == 0 { |
| 36 | return filepath.Join(string(filepath.Separator), "etc", "portal-tunnel", "agent", defaultConfigFilename) |
| 37 | } |
| 38 | configHome := strings.TrimSpace(os.Getenv("XDG_CONFIG_HOME")) |
| 39 | if configHome == "" { |
| 40 | home, err := os.UserHomeDir() |
| 41 | if err != nil { |
| 42 | return filepath.Join(".", ".config", "portal-tunnel", "agent", defaultConfigFilename) |
| 43 | } |
| 44 | configHome = filepath.Join(home, ".config") |
| 45 | } |
| 46 | return filepath.Join(configHome, "portal-tunnel", "agent", defaultConfigFilename) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func DefaultDataDir() string { |
| 51 | switch runtime.GOOS { |
| 52 | case "windows": |
| 53 | return filepath.Join(windowsProgramDataDir(), "Portal Tunnel", "Agent") |
| 54 | case "darwin": |
| 55 | if os.Geteuid() == 0 { |
| 56 | return filepath.Join(string(filepath.Separator), "Library", "Application Support", "Portal Tunnel", "Agent") |
| 57 | } |
| 58 | home, err := os.UserHomeDir() |
| 59 | if err != nil { |
| 60 | return filepath.Join(".", "Library", "Application Support", "Portal Tunnel", "Agent") |
| 61 | } |
| 62 | return filepath.Join(home, "Library", "Application Support", "Portal Tunnel", "Agent") |
| 63 | default: |
| 64 | if os.Geteuid() == 0 { |
| 65 | return filepath.Join(string(filepath.Separator), "var", "lib", "portal-tunnel", "agent") |
| 66 | } |
| 67 | dataHome := strings.TrimSpace(os.Getenv("XDG_DATA_HOME")) |
| 68 | if dataHome == "" { |
| 69 | home, err := os.UserHomeDir() |
| 70 | if err != nil { |
| 71 | return filepath.Join(".", ".local", "share", "portal-tunnel", "agent") |
| 72 | } |
| 73 | dataHome = filepath.Join(home, ".local", "share") |
| 74 | } |
| 75 | return filepath.Join(dataHome, "portal-tunnel", "agent") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func windowsProgramDataDir() string { |
| 80 | programData := strings.TrimSpace(os.Getenv("ProgramData")) |
| 81 | if programData == "" { |
| 82 | return `C:\ProgramData` |
| 83 | } |
| 84 | return programData |
| 85 | } |