// Change directory to the specified directory, checking for errors // Returns the path to the old working directory func defaultCd(dir string) (err error) { err = os.Chdir(dir) if err != nil { result.RegisterError() logger.Output(2, indent()+colors.Red("$ cd "+dir)) logger.Output(2, indent()+colors.Red(err.Error())) } else if verbose { logger.Output(2, indent()+"$ cd "+dir) } return }
// Self upgrades this version of depman to the latest on the master branch func Self(version string) { selfCalled = true util.Print(colors.Blue("Upgrading depman...")) util.RunCommand("go get -u github.com/vube/depman") cmd := exec.Command("depman", "--version") out, err := cmd.CombinedOutput() if err != nil { result.RegisterError() util.Print(colors.Red(string(out))) return } newVersion := strings.TrimSuffix(strings.TrimPrefix(string(out), "Depman Version "), "\n") if newVersion != version { util.Print("Upgraded to Version " + newVersion) } else { util.Print("No upgrade found") } }
// Wrapper on os.exec to catch errors, and print useful messages func defaultRun(cmd string) (err error) { if verbose { logger.Output(2, indent()+"$ "+cmd) } parts := strings.Split(cmd, " ") c := exec.Command(parts[0], parts[1:]...) out, err := c.CombinedOutput() if err != nil { result.RegisterError() logger.Output(2, indent()+colors.Red("$ "+cmd)) o := strings.TrimRight(string(out), "\n") logger.Output(2, indent()+colors.Red(o)) } if len(out) > 0 && debug { logger.Output(2, indent()+string(out)) } return }
func main() { var help bool var path string var command string var arguments []string var deps dep.DependencyMap var err error log.SetFlags(0) flag.BoolVar(&help, "help", false, "Display help") flag.StringVar(&path, "path", ".", "Directory or full path to deps.json") util.Parse() util.Version(VERSION) fmt.Println(colors.Red("Depman was deprecated on 25 February 2015")) fmt.Println(colors.Red("We recommend using 'godep' instead: https://github.com/tools/godep")) util.GoPathIsSet() if timelock.Clear() { return } timelock.Read() // check for a new version of depman go upgrade.Check(VERSION) runtime.Gosched() defer upgrade.Print() path = dep.GetPath(path) if flag.NArg() > 0 { command = strings.ToLower(flag.Arg(0)) } if flag.NArg() > 1 { arguments = flag.Args()[1:] } if help { command = "help" } // switch to check for deps.json switch command { case "add", "", "install", "update", "show-frozen": // check for deps.json util.CheckPath(path) deps, err = dep.Read(path) if err != nil { util.Fatal(colors.Red("Error Reading deps.json: " + err.Error())) } } // switch to exec the sub command switch command { case "init", "create": create.Create(path) case "add": if len(arguments) < 1 { util.Print(colors.Red("Add command requires 1 argument: Add [nickname]")) Help() } else { add.Add(deps, arguments[0]) } case "update": if len(arguments) < 2 { util.Print(colors.Red("Update command requires 2 arguments: Update [nickname] [branch]")) Help() } else { update.Update(deps, arguments[0], arguments[1]) } case "install", "": install.Install(deps) case "self-upgrade": upgrade.Self(VERSION) case "show-frozen": var recursive bool flagset := flag.NewFlagSet("show-frozen", flag.ExitOnError) flagset.BoolVar(&recursive, "recursive", false, "descend recursively (depth-first) into dependencies") flagset.Parse(flag.Args()[1:]) if recursive { fmt.Println(showfrozen.ReadRecursively(deps, nil)) } else { fmt.Print(showfrozen.Read(deps)) } default: result.RegisterError() log.Println(colors.Red("Unknown Command: " + command)) fallthrough case "help": Help() } timelock.Write() if result.ShouldExitWithError() { os.Exit(1) } else { util.Print("Success") } }
// recursively install a DependencyMap func recursiveInstall(deps dep.DependencyMap, set map[string]string) (err error) { for name, d := range deps.Map { start := time.Now() if duplicate(*d, set) { continue } stale := timelock.IsStale(d.Repo) util.PrintDep(name, d.Version, d.Repo, stale) subPath := d.Path() err = d.VCS.Clone(d) if err != nil { continue } util.Cd(subPath) if clean { d.VCS.Clean(d) } if stale { util.VerboseIndent(" # repo is stale, fetching") err = d.VCS.Fetch(d) if err != nil { continue } } err = d.VCS.Checkout(d) if err != nil { continue } if stale { err = d.VCS.Update(d) if err != nil { continue } } util.VerboseIndent(fmt.Sprintf("# time to install: %.3fs", time.Since(start).Seconds())) // Recursive depsFile := util.UpwardFind(subPath, dep.DepsFile) if depsFile != "" && Recurse { subDeps, err := dep.Read(depsFile) if err != nil { util.Print(colors.Red("Error reading deps from '" + depsFile + "': " + err.Error())) result.RegisterError() } else { util.IncreaseIndent() recursiveInstall(subDeps, set) util.DecreaseIndent() } } } return }