Skip to content

Commit

Permalink
Make checkUsability Unix-only
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Pickering <[email protected]>
  • Loading branch information
adamkpickering committed Mar 2, 2023
1 parent 3aad632 commit 4092dc1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 46 deletions.
35 changes: 0 additions & 35 deletions src/go/rdctl/pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package utils

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
)

Expand All @@ -16,34 +12,3 @@ func getParentDir(fullPath string, steps int) string {
}
return fullPath
}

// Verify that the candidatePath is usable as a Rancher Desktop "executable". This means:
// - check that candidatePath exists
// - if checkExecutability is true, check that candidatePath is a regular file,
// and that it is executable
//
// Note that candidatePath may not always be a file; in macOS, it may be a
// .app directory.
func checkUsability(candidatePath string, checkExecutability bool) (bool, error) {
statResult, err := os.Stat(candidatePath)
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to get info on %q: %w", candidatePath, err)
}

if !checkExecutability {
return true, nil
}

if !statResult.Mode().IsRegular() {
return false, nil
}

if statResult.Mode().Perm()&0o111 == 0 {
return false, nil
}

return true, nil
}
4 changes: 2 additions & 2 deletions src/go/rdctl/pkg/utils/utils_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetRDPath() (string, error) {
// and want to move to the "R D.app" part
RDAppParentPath := getParentDir(rdctlPath, 6)
executablePath := filepath.Join(RDAppParentPath, "Contents", "MacOS", "Rancher Desktop")
usable, err := checkUsability(executablePath, true)
usable, err := checkUsableApplication(executablePath, true)
if err != nil {
return "", fmt.Errorf("failed to check usability of %q: %w", executablePath, err)
}
Expand All @@ -34,7 +34,7 @@ func GetRDPath() (string, error) {
// This fallback is mostly for running `npm run dev` and using the installed app because there is no app
// that rdctl would launch directly in dev mode.
candidatePath := filepath.Join("/Applications", "Rancher Desktop.app")
usable, err = checkUsability(candidatePath, false)
usable, err = checkUsableApplication(candidatePath, false)
if err != nil {
return "", fmt.Errorf("failed to check usability of %q: %w", candidatePath, err)
}
Expand Down
4 changes: 3 additions & 1 deletion src/go/rdctl/pkg/utils/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ func GetRDPath() (string, error) {
if err != nil {
return "", fmt.Errorf("failed to resolve %q: %w", rdctlSymlinkPath, err)
}
// rdctl should be at <installDir>/resources/resources/linux/bin/rdctl.
// rancher-desktop should be 5 directories up from that, at <installDir>/rancher-desktop.
normalParentPath := getParentDir(rdctlPath, 5)
candidatePaths := []string{
filepath.Join(normalParentPath, "rancher-desktop"),
"/opt/rancher-desktop/rancher-desktop",
}
for _, candidatePath := range candidatePaths {
usable, err := checkUsability(candidatePath, true)
usable, err := checkUsableApplication(candidatePath, true)
if err != nil {
return "", fmt.Errorf("failed to check usability of %q: %w", candidatePath, err)
}
Expand Down
39 changes: 39 additions & 0 deletions src/go/rdctl/pkg/utils/utils_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build linux || darwin

package utils

import (
"errors"
"fmt"
"golang.org/x/sys/unix"
"io/fs"
"os"
)

// Verify that the candidatePath is usable as a Rancher Desktop "executable". This means:
// - check that candidatePath exists
// - if checkExecutability is true, check that candidatePath is a regular file,
// and that it is executable
//
// Note that candidatePath may not always be a file; in macOS, it may be a
// .app directory.
func checkUsableApplication(candidatePath string, checkExecutability bool) (bool, error) {
statResult, err := os.Stat(candidatePath)
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to get info on %q: %w", candidatePath, err)
}

if !checkExecutability {
return true, nil
}

if !statResult.Mode().IsRegular() {
return false, nil
}

err = unix.Access(candidatePath, unix.X_OK)
return err == nil, nil
}
19 changes: 11 additions & 8 deletions src/go/rdctl/pkg/utils/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"

Expand All @@ -20,13 +21,15 @@ func GetRDPath() (string, error) {
if err != nil {
return "", fmt.Errorf("failed to resolve %q: %w", rdctlSymlinkPath, err)
}
// rdctl should be at <installDir>/resources/resources/win32/bin/rdctl.exe.
// rancher-desktop should be 5 directories up from that, at <installDir>/Rancher Desktop.exe.
normalParentPath := getParentDir(rdctlPath, 5)
candidatePath := filepath.Join(normalParentPath, "Rancher Desktop.exe")
usable, err := checkUsability(candidatePath, false)
if err != nil {
return "", fmt.Errorf("failed to check usability of %q: %w", candidatePath, err)
_, err = os.Stat(candidatePath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("failed to check existence of %q: %w", candidatePath, err)
}
if usable {
if err == nil {
return candidatePath, nil
}

Expand All @@ -52,11 +55,11 @@ func GetRDPath() (string, error) {
)
for _, dataDir := range dataPaths {
candidatePath := filepath.Join(dataDir, "Programs", "Rancher Desktop", "Rancher Desktop.exe")
usable, err := checkUsability(candidatePath, false)
if err != nil {
return "", fmt.Errorf("failed to check usability of %q: %w", candidatePath, err)
_, err := os.Stat(candidatePath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("failed to check existence of %q: %w", candidatePath, err)
}
if usable {
if err == nil {
return candidatePath, nil
}
}
Expand Down

0 comments on commit 4092dc1

Please sign in to comment.