github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runbits/checkout/path.go (about) 1 package checkout 2 3 import ( 4 "path/filepath" 5 6 "github.com/ActiveState/cli/internal/constants" 7 "github.com/ActiveState/cli/internal/errs" 8 "github.com/ActiveState/cli/internal/fileutils" 9 "github.com/ActiveState/cli/internal/locale" 10 "github.com/ActiveState/cli/internal/logging" 11 "github.com/ActiveState/cli/internal/osutils" 12 "github.com/ActiveState/cli/pkg/project" 13 ) 14 15 func (r *Checkout) pathToUse(namespace *project.Namespaced, preferredPath string) (string, error) { 16 if preferredPath == "" && namespace == nil { 17 return "", errs.New("No namespace or path provided") 18 } 19 20 path := preferredPath 21 if path == "" { 22 logging.Debug("No path provided, using default") 23 24 // Get path from working directory 25 wd, err := osutils.Getwd() 26 if err != nil { 27 return "", errs.Wrap(err, "Could not get working directory") 28 } 29 path = filepath.Join(wd, namespace.Project) 30 } 31 32 if err := validatePath(namespace, path); err != nil { 33 return "", errs.Wrap(err, "Validation failed") 34 } 35 36 return path, nil 37 } 38 39 type ErrAlreadyCheckedOut struct { 40 error 41 Path string 42 } 43 44 type ErrNoPermission struct { 45 error 46 Path string 47 } 48 49 func validatePath(ns *project.Namespaced, path string) error { 50 if !fileutils.TargetExists(path) { 51 return nil 52 } 53 54 empty, err := fileutils.IsEmptyDir(path) 55 if err != nil { 56 return locale.WrapError(err, "err_namespace_empty_dir", "Could not verify if directory '{{.V0}}' is empty", path) 57 } 58 if empty { 59 return nil 60 } 61 62 configFile := filepath.Join(path, constants.ConfigFileName) 63 if fileutils.FileExists(configFile) { 64 return &ErrAlreadyCheckedOut{errs.New("already checked out at %s", path), path} 65 } 66 67 return nil 68 }