// Exists checks for codebase existence by using the basic pkg rev Exists() // routine to see if the specified codebase (at any specified rev, or from // the default VCS rev) can be found. If so it'll return the fullest URI // it can to that codebase pkg repo, otherwise an error is returned. // Config file and env var settings can help to find the codebase, ie: // * DVLN_CODEBASE_PATH in env or codebase_path in cfg file // - > space separated list of paths to prepend to the codebase name // - > eg: "github.com/dvln git+ssh://host.com/path/to/clones /some/local/dir" // - - note: 'hub' and 'hub:<uri>' reserved for future user/central dvln hub // - > all values are *always* forward slash regardless of platform // - > 'dvln' as the codebase would result in looking, in order, here: // - > "github.com/dvln/dvln[.git]" // - > "git+ssh://host.com/clone/path/dvln[.git]" // - > "/some/local/dir/dvln[.git]" // - > "dvln" // - > final fallback will be on the individual dvln itself // Based on the above existence checks it'll return: // - string: URI: full path (on filesystem) or URL (if remote), "" if not found // - locality: where it exists (LocalDir, RemoteURL, NonExistent) // - error: any error that is detected in scanning for the workspace func (cb *Defn) Exists(codebaseVerSel string) (string, Locality, error) { //eriknow, make some choices around codebase existence checks... // should be a challenge, that's for sure var err error exists := false if codebaseVerSel != "" { exists, err = file.Exists(codebaseVerSel) } wkspcRootDir := "" if !exists { wkspcRootDir = globs.GetString("wkspcRootDir") } if wkspcRootDir == "" { wkspcRootDir = globs.GetString("dvlnCfgDir") } if codebaseVerSel == "" || !exists || err != nil { if err == nil { out.Debugln("No codebase file to read, skipping (considered normal)") } else { out.Debugf("No codebase file to read, skipping (abnormal, unexpected err: %s)\n", err) } return "", NonExistent, err } return codebaseVerSel, LocalDir, nil }
// RootDir will return the path to the top-most workspace root directory // based on our current path unless a "starting directory" path is given // (all other params are ignored). It will return the path to the // workspace root if it found one, otherwise "" (a non-nil error implies // there was an unexpected problem... ie: not being able to find a workspace // root directory is NOT an error condition). Note: if you wish to bypass // cached workspace root info then use RootDirFind() directly instead. func RootDir(path ...string) (string, error) { // wkspcRootDir starts out as "none", if something else we've calc'd it.. wkspcRootDir := globs.GetString("wkspcrootDir") if wkspcRootDir != "none" { return wkspcRootDir, nil } // otherwise find it (this will always try and find it and cache it) return RootDirFind(path...) }
// SetRootDir will set the given dir as the workspace root directory, it will also // set some "derived" settings in globs (viper) such as: // wkspcMetaDir: $wsroot/.dvln // wkspcLogDir: $wsroot/.dvln/log // wkspcTmpDir: $wsroot/.dvln/tmp // wkspcVCSDir: $wsroot/.dvln/vcs // wkspcVCSDataDir: $wsroot/.dvln/vcs/wkspc // wkspcDBDir: $wsroot/.dvln/db // wkspcDB: $wsroot/.dvln/db/wkspc.db // As other dirs or files are added relative to the .dvln/ wkspc meta-data dir // they can be added here to "bootstap" them. Note that if func SetRootDir(rootDir string) error { if rootDir == "" { // if there is no root dir found allow it to be set to "" and bail globs.Set("wkspcRootDir", rootDir) return nil } directory := true _, err := prepIfNotThere(directory, rootDir, "", "wkspcRootDir") if err != nil { return err } wkspcMetaDir, err := prepIfNotThere(directory, rootDir, globs.GetString("wkspcMetaDirName"), "wkspcMetaDir") if err != nil { return err } _, err = prepIfNotThere(directory, wkspcMetaDir, "log", "wkspcLogDir") if err != nil { return err } _, err = prepIfNotThere(directory, wkspcMetaDir, "tmp", "wkspcTmpDir") if err != nil { return err } wkspcVCSDir, err := prepIfNotThere(directory, wkspcMetaDir, "vcs", "wkspcVCSDir") if err != nil { return err } wkspcVCSDataDir, err := prepIfNotThere(directory, wkspcVCSDir, "wkspc", "wkspcVCSDataDir") if err != nil { return err } _, err = prepIfNotThere(!directory, wkspcVCSDataDir, "static.dvln", "wkspcStaticDvln") if err != nil { return err } wkspcDBDir, err := prepIfNotThere(directory, wkspcMetaDir, "db", "wkspcDBDir") if err != nil { return err } _, err = prepIfNotThere(!directory, wkspcDBDir, "wkspc.db", "wkspcDB") if err != nil { return err } return nil }
// RootDirFind gets the workspaces root dir (if there is one), note that this will // not use any "cached" values and it will store the result in globs (viper) // under the "wkspcRootDir" key (you can access that with any upper/lower case // as viper is case insensitive). func RootDirFind(path ...string) (string, error) { startDir := "" var err error if path == nil || path[0] == "" { startDir, err = os.Getwd() if err != nil { return "", out.WrapErr(err, "Unable to find the workspace root directory (get current working dir failed)", 4100) } } else { startDir = path[0] } rootDir, err := dir.FindDirInOrAbove(startDir, globs.GetString("wkspcMetaDirName")) if err == nil { // Cache the root dir in "globs" (viper) memory if no error globs.Set("wkspcRootDir", rootDir) } return rootDir, err }