Skip to content

Commit 25c2f06

Browse files
linux: don't resolve symlink on executable to keep a constant name when installed via homebrew (#508)
1 parent 1509c41 commit 25c2f06

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

commands_display.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/creativeprojects/resticprofile/filesearch"
1717
"github.com/creativeprojects/resticprofile/shell"
1818
"github.com/creativeprojects/resticprofile/term"
19+
"github.com/creativeprojects/resticprofile/util"
1920
"github.com/creativeprojects/resticprofile/util/collect"
2021
"github.com/fatih/color"
2122
colorable "github.com/mattn/go-colorable"
@@ -258,7 +259,12 @@ func displayVersion(output io.Writer, ctx commandContext) error {
258259
// allow for the general verbose flag, or specified after the command
259260
arguments := ctx.request.arguments
260261
if ctx.flags.verbose || (len(arguments) > 0 && (arguments[0] == "-v" || arguments[0] == "--verbose")) {
262+
executablePath, err := util.Executable()
263+
if err != nil {
264+
executablePath = "unknown"
265+
}
261266
out("\n")
267+
out("\t%s:\t%s\n", "executable", executablePath)
262268
out("\t%s:\t%s\n", "home", "https://github.com/creativeprojects/resticprofile")
263269
out("\t%s:\t%s\n", "version", version)
264270
out("\t%s:\t%s\n", "commit", commit)

schedule_jobs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"github.com/creativeprojects/clog"
99
"github.com/creativeprojects/resticprofile/config"
1010
"github.com/creativeprojects/resticprofile/schedule"
11+
"github.com/creativeprojects/resticprofile/util"
1112
)
1213

1314
func scheduleJobs(handler schedule.Handler, configs []*config.Schedule) error {
1415
wd, err := os.Getwd()
1516
if err != nil {
1617
return err
1718
}
18-
binary, err := os.Executable()
19+
binary, err := util.Executable()
1920
if err != nil {
2021
return err
2122
}

util/executable.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !linux
2+
3+
package util
4+
5+
import "os"
6+
7+
// Executable returns the path name for the executable that started the current process.
8+
// On non-Linux systems, it behaves like os.Executable.
9+
// On Linux, it returns the path to the executable as specified in the command line arguments.
10+
func Executable() (string, error) {
11+
return os.Executable()
12+
}

util/executable_linux.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build linux
2+
3+
package util
4+
5+
import (
6+
"errors"
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
// Executable returns the path name for the executable that started the current process.
12+
// On non-Linux systems, it behaves like os.Executable.
13+
// On Linux, it returns the path to the executable as specified in the command line arguments.
14+
func Executable() (string, error) {
15+
executable := os.Args[0]
16+
if len(executable) == 0 {
17+
return "", errors.New("executable path is empty")
18+
}
19+
if executable[0] != '/' {
20+
wd, err := os.Getwd()
21+
if err != nil {
22+
return "", err
23+
}
24+
// If the executable path is relative, prepend the current working directory to form an absolute path.
25+
executable = filepath.Join(wd, executable)
26+
}
27+
return executable, nil
28+
}

util/executable_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package util
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestExecutableIsAbsolute(t *testing.T) {
12+
executable, err := Executable()
13+
require.NoError(t, err)
14+
assert.NotEmpty(t, executable)
15+
16+
assert.True(t, filepath.IsAbs(executable))
17+
}

0 commit comments

Comments
 (0)