Show a warning when using wrong executable

This commit updates the 'wdcli' command to show a warning when using the
wrong executable.
This commit is contained in:
Tom Wiesing 2022-09-09 13:35:02 +02:00
parent c4de1f2a06
commit 35bb95c5ca
No known key found for this signature in database
29 changed files with 176 additions and 30 deletions

View file

@ -12,7 +12,7 @@ var ErrCopySameFile = errors.New("src and dst must be different files")
// CopyFile copies a file from src to dst.
// When dst and src are the same file, returns ErrCopySameFile.
func CopyFile(dst, src string) error {
if src == dst {
if SameFile(src, dst) {
return ErrCopySameFile
}
@ -51,7 +51,7 @@ func CopyDirectory(dst, src string, onCopy func(dst, src string)) error {
// TODO: Allow copying in parallel? Maybe with a mutex?
// sanity checks
if src == dst {
if SameFile(src, dst) {
return ErrCopySameFile
}
if !IsDirectory(dst) {

87
internal/fsx/same.go Normal file
View file

@ -0,0 +1,87 @@
package fsx
import (
"os"
"path/filepath"
)
// SameFile checks if path1 and path2 refer to the same file.
// If both files exist, they are compared using [os.SameFile].
// If both files do not exist, the paths are first compared syntactically and then via recursion on [filepath.Dir].
func SameFile(path1, path2 string) bool {
// initial attempt: check if directly
same, certain := couldBeSameFile(path1, path2)
if certain {
return same
}
// second attempt: find the directory names and base paths
d1, n1 := filepath.Dir(path1), filepath.Base(path1)
d2, n2 := filepath.Dir(path2), filepath.Base(path2)
// if we have different file names (and they don't exist)
// we don't need to continue
if n1 != n2 {
return false
}
// compare the base names!
{
same, _ := couldBeSameFile(d1, d2)
return same
}
}
// couldBeSameFile checks if path1 might be the same as path2.
//
// If both files exist, compares using [os.SameFile].
// Otherwise compares absolute paths using string comparison.
//
// same indicates if they might be the same file.
// authorative indiciates if the result is authorative.
func couldBeSameFile(path1, path2 string) (same, authorative bool) {
{
// stat both files
info1, err1 := os.Stat(path1)
info2, err2 := os.Stat(path2)
// both files exist => check using os.SameFile
// the result is always authorative
if err1 == nil && err2 == nil {
same = os.SameFile(info1, info2)
authorative = true
return
}
// only 1 file errored => they could be different
if (err1 == nil) != (err2 == nil) {
return
}
// only 1 file does not exist => they could be different
if os.IsNotExist(err1) != os.IsNotExist(err2) {
return
}
}
{
// resolve paths absolutely
rpath1, err1 := filepath.Abs(path1)
rpath2, err2 := filepath.Abs(path2)
// if either path could not be resolved absolutely
// fallback to just using clean!
if err1 != nil {
rpath1 = filepath.Clean(path1)
}
if err2 != nil {
rpath2 = filepath.Clean(path2)
}
// compare using strings
same = rpath1 == rpath2
authorative = same // positive result is authorative!
return
}
}

View file

@ -5,7 +5,11 @@ import (
"time"
)
// Touch touches a file
// Touch touches a file.
// It is similar to the unix 'touch' command.
//
// If the file does not exist exists, it is created using [os.Create].
// If the file does exist, it's access and modification times are updated to the current time.
func Touch(path string) error {
_, err := os.Stat(path)
switch {

View file

@ -1,17 +1,23 @@
// Package fsx provides convenient abstractions to work with the filesystem.
package fsx
import "os"
import (
"os"
)
// Exists checks if the given path exists
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// IsDirectory checks if the provided path exists and is a directory
func IsDirectory(path string) bool {
info, err := os.Stat(path)
return err == nil && info.Mode().IsDir()
}
// IsFile checks if the provided path exists and is a regular file
func IsFile(path string) bool {
info, err := os.Stat(path)
return err == nil && info.Mode().IsRegular()

View file

@ -12,7 +12,7 @@ import (
)
// Installable represents a Stack that can be automatically installed from a set of resources
// See the Install() method.
// See the [Install] method.
type Installable struct {
Stack