zutils/cmd/autodetect.go

26 lines
523 B
Go
Raw Normal View History

package cmd
import (
"fmt"
"os"
"path/filepath"
)
// AutoDetectInfo automatically determines whether the path is a file or directory
func AutoDetectInfo(target string) error {
absPath, err := filepath.Abs(target)
if err != nil {
return fmt.Errorf("error resolving path: %w", err)
}
fileInfo, err := os.Stat(absPath)
if err != nil {
return fmt.Errorf("error accessing %s: %w", absPath, err)
}
if fileInfo.IsDir() {
return DirInfoCommand([]string{absPath})
}
return FileInfoCommand([]string{absPath})
}