// 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 }
// prepIfNotThere will create the dir if it doesn't exist and store info about // it in viper (if a name is given), returns the full dir name and any // error that occurs. Note that "item" can be "" to indicate no sub dir. func prepIfNotThere(directory bool, parent, item, name string) (string, error) { var err error fullPath := "" if item != "" { fullPath = filepath.Join(parent, item) } else { fullPath = parent } if name != "" { globs.Set(name, fullPath) } if fullPath != "" { if directory { err = dir.CreateIfNotExists(fullPath) } else { err = file.CreateIfNotExists(fullPath) } } return fullPath, err }