Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commands_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"github.com/creativeprojects/resticprofile/filesearch"
"github.com/creativeprojects/resticprofile/shell"
"github.com/creativeprojects/resticprofile/term"
"github.com/creativeprojects/resticprofile/util"
"github.com/creativeprojects/resticprofile/util/collect"
"github.com/fatih/color"
colorable "github.com/mattn/go-colorable"
Expand Down Expand Up @@ -258,7 +259,12 @@
// allow for the general verbose flag, or specified after the command
arguments := ctx.request.arguments
if ctx.flags.verbose || (len(arguments) > 0 && (arguments[0] == "-v" || arguments[0] == "--verbose")) {
executablePath, err := util.Executable()
if err != nil {
executablePath = "unknown"
}

Check warning on line 265 in commands_display.go

View check run for this annotation

Codecov / codecov/patch

commands_display.go#L264-L265

Added lines #L264 - L265 were not covered by tests
out("\n")
out("\t%s:\t%s\n", "executable", executablePath)
out("\t%s:\t%s\n", "home", "https://github.com/creativeprojects/resticprofile")
out("\t%s:\t%s\n", "version", version)
out("\t%s:\t%s\n", "commit", commit)
Expand Down
3 changes: 2 additions & 1 deletion schedule_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/creativeprojects/clog"
"github.com/creativeprojects/resticprofile/config"
"github.com/creativeprojects/resticprofile/schedule"
"github.com/creativeprojects/resticprofile/util"
)

func scheduleJobs(handler schedule.Handler, configs []*config.Schedule) error {
wd, err := os.Getwd()
if err != nil {
return err
}
binary, err := os.Executable()
binary, err := util.Executable()
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions util/executable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !linux

package util

import "os"

// Executable returns the path name for the executable that started the current process.
// On non-Linux systems, it behaves like os.Executable.
// On Linux, it returns the path to the executable as specified in the command line arguments.
func Executable() (string, error) {
return os.Executable()
}
28 changes: 28 additions & 0 deletions util/executable_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build linux

package util

import (
"errors"
"os"
"path/filepath"
)

// Executable returns the path name for the executable that started the current process.
// On non-Linux systems, it behaves like os.Executable.
// On Linux, it returns the path to the executable as specified in the command line arguments.
func Executable() (string, error) {
executable := os.Args[0]
if len(executable) == 0 {
return "", errors.New("executable path is empty")
}

Check warning on line 18 in util/executable_linux.go

View check run for this annotation

Codecov / codecov/patch

util/executable_linux.go#L17-L18

Added lines #L17 - L18 were not covered by tests
if executable[0] != '/' {
wd, err := os.Getwd()
if err != nil {
return "", err
}

Check warning on line 23 in util/executable_linux.go

View check run for this annotation

Codecov / codecov/patch

util/executable_linux.go#L20-L23

Added lines #L20 - L23 were not covered by tests
// If the executable path is relative, prepend the current working directory to form an absolute path.
executable = filepath.Join(wd, executable)

Check warning on line 25 in util/executable_linux.go

View check run for this annotation

Codecov / codecov/patch

util/executable_linux.go#L25

Added line #L25 was not covered by tests
}
return executable, nil
}
17 changes: 17 additions & 0 deletions util/executable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestExecutableIsAbsolute(t *testing.T) {
executable, err := Executable()
require.NoError(t, err)
assert.NotEmpty(t, executable)

assert.True(t, filepath.IsAbs(executable))
}