Skip to content

Commit 3841f96

Browse files
committed
ghost: install binary to user local bin
1 parent 04a9745 commit 3841f96

File tree

3 files changed

+53
-65
lines changed

3 files changed

+53
-65
lines changed

overlord/sysutils_darwin.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"bytes"
1313
"errors"
1414
"fmt"
15-
"io"
15+
1616
"os"
1717
"os/exec"
1818
"path/filepath"
@@ -100,42 +100,13 @@ func getCurrentUserHomeDir() string {
100100

101101
// Install installs and configures the ghost service for automatic startup on macOS
102102
func Install() error {
103-
execPath, err := os.Executable()
104-
if err != nil {
105-
return fmt.Errorf("failed to get executable path: %v", err)
106-
}
107-
108103
// Install binary to filesystem
109-
homeDir := getCurrentUserHomeDir()
110-
targetPath := filepath.Join(homeDir, ".local", "bin", "ghost")
111-
binDir := filepath.Join(homeDir, ".local", "bin")
112-
113-
err = os.MkdirAll(binDir, 0755)
114-
if err != nil {
115-
return fmt.Errorf("failed to create ~/.local/bin directory: %v", err)
116-
}
117-
118-
srcFile, err := os.Open(execPath)
104+
targetPath, err := installBinaryToUserLocal()
119105
if err != nil {
120-
return fmt.Errorf("failed to open source file: %v", err)
106+
return fmt.Errorf("failed to install binary: %v", err)
121107
}
122-
defer srcFile.Close()
123108

124-
dstFile, err := os.Create(targetPath)
125-
if err != nil {
126-
return fmt.Errorf("failed to create target file: %v", err)
127-
}
128-
defer dstFile.Close()
129-
130-
_, err = io.Copy(dstFile, srcFile)
131-
if err != nil {
132-
return fmt.Errorf("failed to copy ghost binary: %v", err)
133-
}
134-
135-
err = os.Chmod(targetPath, 0755)
136-
if err != nil {
137-
return fmt.Errorf("failed to set executable permissions: %v", err)
138-
}
109+
homeDir := getCurrentUserHomeDir()
139110

140111
// Install service
141112
cmdParts := getServiceCommand()
@@ -172,10 +143,6 @@ func Install() error {
172143
<true/>
173144
<key>KeepAlive</key>
174145
<true/>
175-
<key>StandardOutPath</key>
176-
<string>/var/log/ghost.log</string>
177-
<key>StandardErrorPath</key>
178-
<string>/var/log/ghost.log</string>
179146
</dict>
180147
</plist>
181148
`, argsXML, homeDir)

overlord/sysutils_linux.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -155,37 +155,12 @@ func cpWithSudo(src, dst string) error {
155155
return nil
156156
}
157157

158-
// chmodWithSudo changes file permissions using sudo
159-
func chmodWithSudo(mode, path string) error {
160-
err := runWithSudo("chmod", mode, path)
161-
if err != nil {
162-
return fmt.Errorf("failed to set permissions: %v", err)
163-
}
164-
return nil
165-
}
166-
167158
// Install installs and configures the ghost service for automatic startup on Linux
168159
func Install() error {
169-
execPath, err := os.Executable()
170-
if err != nil {
171-
return fmt.Errorf("failed to get executable path: %v", err)
172-
}
173-
174160
// Install binary to filesystem
175-
targetPath := "/opt/bin/ghost"
176-
177-
err = runWithSudo("mkdir", "-p", "/opt/bin")
178-
if err != nil {
179-
return fmt.Errorf("failed to create /opt/bin directory: %v", err)
180-
}
181-
182-
err = cpWithSudo(execPath, targetPath)
161+
targetPath, err := installBinaryToUserLocal()
183162
if err != nil {
184-
return fmt.Errorf("failed to copy ghost binary: %v", err)
185-
}
186-
err = chmodWithSudo("755", targetPath)
187-
if err != nil {
188-
return fmt.Errorf("failed to set executable permissions: %v", err)
163+
return fmt.Errorf("failed to install binary: %v", err)
189164
}
190165

191166
homeDir := getCurrentUserHomeDir()
@@ -282,8 +257,8 @@ func getSystemdServicePath() (string, error) {
282257
// 3. /lib/systemd/system - Package manager installed services (Debian/Ubuntu)
283258

284259
serviceDirs := []string{
285-
"/usr/lib/systemd/system",
286260
"/etc/systemd/system",
261+
"/usr/lib/systemd/system",
287262
"/lib/systemd/system",
288263
}
289264

overlord/utils.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"os"
14+
"path/filepath"
1415
"runtime"
1516
"strconv"
1617
"strings"
@@ -102,6 +103,51 @@ func isGhostRunning() bool {
102103
return err == nil
103104
}
104105

106+
// installBinaryToUserLocal installs the ghost binary to ~/.local/bin
107+
func installBinaryToUserLocal() (string, error) {
108+
execPath, err := os.Executable()
109+
if err != nil {
110+
return "", fmt.Errorf("failed to get executable path: %v", err)
111+
}
112+
113+
homeDir := getCurrentUserHomeDir()
114+
targetPath := filepath.Join(homeDir, ".local", "bin", "ghost")
115+
binDir := filepath.Join(homeDir, ".local", "bin")
116+
117+
// Create the ~/.local/bin directory if it doesn't exist
118+
err = os.MkdirAll(binDir, 0755)
119+
if err != nil {
120+
return "", fmt.Errorf("failed to create ~/.local/bin directory: %v", err)
121+
}
122+
123+
// Copy the binary
124+
srcFile, err := os.Open(execPath)
125+
if err != nil {
126+
return "", fmt.Errorf("failed to open source file: %v", err)
127+
}
128+
defer srcFile.Close()
129+
130+
dstFile, err := os.Create(targetPath)
131+
if err != nil {
132+
return "", fmt.Errorf("failed to create target file: %v", err)
133+
}
134+
defer dstFile.Close()
135+
136+
_, err = io.Copy(dstFile, srcFile)
137+
if err != nil {
138+
return "", fmt.Errorf("failed to copy ghost binary: %v", err)
139+
}
140+
141+
// Set executable permissions
142+
err = os.Chmod(targetPath, 0755)
143+
if err != nil {
144+
return "", fmt.Errorf("failed to set executable permissions: %v", err)
145+
}
146+
147+
fmt.Printf("Ghost binary installed to %s\n", targetPath)
148+
return targetPath, nil
149+
}
150+
105151
// getServiceCommand constructs the command line arguments for the service
106152
// by filtering out the --install flag from os.Args
107153
func getServiceCommand() []string {

0 commit comments

Comments
 (0)