// EnsureGoVendor ensures that the Go version is correct. func EnsureGoVendor() { // 6l was removed in 1.5, when vendoring was introduced. cmd := exec.Command("go", "tool", "6l") if _, err := cmd.CombinedOutput(); err == nil { msg.Warn("You must install the Go 1.5 or greater toolchain to work with Glide.\n") os.Exit(1) } if os.Getenv("GO15VENDOREXPERIMENT") != "1" { msg.Warn("To use Glide, you must set GO15VENDOREXPERIMENT=1\n") os.Exit(1) } // Verify the setup isn't for the old version of glide. That is, this is // no longer assuming the _vendor directory as the GOPATH. Inform of // the change. if _, err := os.Stat("_vendor/"); err == nil { msg.Warn(`Your setup appears to be for the previous version of Glide. Previously, vendor packages were stored in _vendor/src/ and _vendor was set as your GOPATH. As of Go 1.5 the go tools recognize the vendor directory as a location for these files. Glide has embraced this. Please remove the _vendor directory or move the _vendor/src/ directory to vendor/.` + "\n") os.Exit(1) } }
// Get fetches one or more dependencies and installs. // // This includes resolving dependency resolution and re-generating the lock file. func Get(names []string, installer *repo.Installer, insecure, skipRecursive bool) { base := gpath.Basepath() EnsureGopath() EnsureVendorDir() conf := EnsureConfig() glidefile, err := gpath.Glide() if err != nil { msg.Die("Could not find Glide file: %s", err) } // Add the packages to the config. if err := addPkgsToConfig(conf, names, insecure); err != nil { msg.Die("Failed to get new packages: %s", err) } // Fetch the new packages. Can't resolve versions via installer.Update if // get is called while the vendor/ directory is empty so we checkout // everything. installer.Checkout(conf, false) // Prior to resolving dependencies we need to start working with a clone // of the conf because we'll be making real changes to it. confcopy := conf.Clone() if !skipRecursive { // Get all repos and update them. // TODO: Can we streamline this in any way? The reason that we update all // of the dependencies is that we need to re-negotiate versions. For example, // if an existing dependency has the constraint >1.0 and this new package // adds the constraint <2.0, then this may re-resolve the existing dependency // to be between 1.0 and 2.0. But changing that dependency may then result // in that dependency's dependencies changing... so we sorta do the whole // thing to be safe. err = installer.Update(confcopy) if err != nil { msg.Die("Could not update packages: %s", err) } } // Set Reference if err := repo.SetReference(confcopy); err != nil { msg.Error("Failed to set references: %s", err) } // VendoredCleanup if installer.UpdateVendored { repo.VendoredCleanup(confcopy) } // Write YAML if err := conf.WriteFile(glidefile); err != nil { msg.Die("Failed to write glide YAML file: %s", err) } if !skipRecursive { // Write lock writeLock(conf, confcopy, base) } else { msg.Warn("Skipping lockfile generation because full dependency tree is not being calculated") } }
// Parse parses a GPM-flavored Godeps file. func Parse(dir string) ([]*cfg.Dependency, error) { path := filepath.Join(dir, "Godeps") if i, err := os.Stat(path); err != nil { return []*cfg.Dependency{}, nil } else if i.IsDir() { msg.Info("Godeps is a directory. This is probably a Godep project.\n") return []*cfg.Dependency{}, nil } msg.Info("Found Godeps file.\n") buf := []*cfg.Dependency{} file, err := os.Open(path) if err != nil { return buf, err } scanner := bufio.NewScanner(file) for scanner.Scan() { parts, ok := parseGodepsLine(scanner.Text()) if ok { dep := &cfg.Dependency{Name: parts[0]} if len(parts) > 1 { dep.Reference = parts[1] } buf = append(buf, dep) } } if err := scanner.Err(); err != nil { msg.Warn("Scan failed: %s\n", err) return buf, err } return buf, nil }
func (i *Installer) List(conf *cfg.Config) []*cfg.Dependency { base := "." vpath := i.VendorPath() ic := newImportCache() v := &VersionHandler{ Destination: vpath, Use: ic, Imported: make(map[string]bool), Conflicts: make(map[string]bool), Config: conf, } // Update imports res, err := dependency.NewResolver(base) if err != nil { msg.Die("Failed to create a resolver: %s", err) } res.Config = conf res.VersionHandler = v msg.Info("Resolving imports") _, err = allPackages(conf.Imports, res) if err != nil { msg.Die("Failed to retrieve a list of dependencies: %s", err) } msg.Warn("devImports not resolved.") return conf.Imports }
func guessImportDeps(base string, config *cfg.Config) { msg.Info("Attempting to import from other package managers (use --skip-import to skip)") deps := []*cfg.Dependency{} absBase, err := filepath.Abs(base) if err != nil { msg.Die("Failed to resolve location of %s: %s", base, err) } if d, ok := guessImportGodep(absBase); ok { msg.Info("Importing Godep configuration") msg.Warn("Godep uses commit id versions. Consider using Semantic Versions with Glide") deps = d } else if d, ok := guessImportGPM(absBase); ok { msg.Info("Importing GPM configuration") deps = d } else if d, ok := guessImportGB(absBase); ok { msg.Info("Importing GB configuration") deps = d } for _, i := range deps { if i.Reference == "" { msg.Info("--> Found imported reference to %s", i.Name) } else { msg.Info("--> Found imported reference to %s at revision %s", i.Name, i.Reference) } config.Imports = append(config.Imports, i) } }
func singleWarn(ft string, v ...interface{}) { m := fmt.Sprintf(ft, v...) _, f := warningMessage[m] if !f { msg.Warn(m) warningMessage[m] = true } }
// EnsureConfig loads and returns a config file. // // Any error will cause an immediate exit, with an error printed to Stderr. func EnsureConfig() *cfg.Config { yamlpath, err := gpath.Glide() if err != nil { msg.ExitCode(2) msg.Die("Failed to find %s file in directory tree: %s", gpath.GlideFile, err) } yml, err := ioutil.ReadFile(yamlpath) if err != nil { msg.ExitCode(2) msg.Die("Failed to load %s: %s", yamlpath, err) } conf, err := cfg.ConfigFromYaml(yml) if err != nil { msg.ExitCode(3) msg.Die("Failed to parse %s: %s", yamlpath, err) } b := filepath.Dir(yamlpath) buildContext, err := util.GetBuildContext() if err != nil { msg.Die("Failed to build an import context while ensuring config: %s", err) } cwd, err := os.Getwd() if err != nil { msg.Err("Unable to get the current working directory") } else { // Determining a package name requires a relative path b, err = filepath.Rel(b, cwd) if err == nil { name := buildContext.PackageName(b) if name != conf.Name { msg.Warn("The name listed in the config file (%s) does not match the current location (%s)", conf.Name, name) } } else { msg.Warn("Problem finding the config file path (%s) relative to the current directory (%s): %s", b, cwd, err) } } err = mirrors.Load() if err != nil { msg.Err("Unable to load mirrors: %s", err) } return conf }
// EnsureGoVendor ensures that the Go version is correct. func EnsureGoVendor() { // 6l was removed in 1.5, when vendoring was introduced. cmd := exec.Command(goExecutable(), "tool", "6l") if _, err := cmd.CombinedOutput(); err == nil { msg.Warn("You must install the Go 1.5 or greater toolchain to work with Glide.\n") os.Exit(1) } // Check if this is go15, which requires GO15VENDOREXPERIMENT // Any release after go15 does not require that env var. cmd = exec.Command(goExecutable(), "version") if out, err := cmd.CombinedOutput(); err != nil { msg.Err("Error getting version: %s.\n", err) os.Exit(1) } else if strings.HasPrefix(string(out), "go version 1.5") { // This works with 1.5 and 1.6. cmd = exec.Command(goExecutable(), "env", "GO15VENDOREXPERIMENT") if out, err := cmd.CombinedOutput(); err != nil { msg.Err("Error looking for $GOVENDOREXPERIMENT: %s.\n", err) os.Exit(1) } else if strings.TrimSpace(string(out)) != "1" { msg.Err("To use Glide, you must set GO15VENDOREXPERIMENT=1") os.Exit(1) } } // In the case where vendoring is explicitly disabled, balk. if os.Getenv("GO15VENDOREXPERIMENT") == "0" { msg.Err("To use Glide, you must set GO15VENDOREXPERIMENT=1") os.Exit(1) } // Verify the setup isn't for the old version of glide. That is, this is // no longer assuming the _vendor directory as the GOPATH. Inform of // the change. if _, err := os.Stat("_vendor/"); err == nil { msg.Warn(`Your setup appears to be for the previous version of Glide. Previously, vendor packages were stored in _vendor/src/ and _vendor was set as your GOPATH. As of Go 1.5 the go tools recognize the vendor directory as a location for these files. Glide has embraced this. Please remove the _vendor directory or move the _vendor/src/ directory to vendor/.` + "\n") os.Exit(1) } }
// Install installs a vendor directory based on an existing Glide configuration. func Install(installer *repo.Installer, stripVendor bool) { cache.SystemLock() base := "." // Ensure GOPATH EnsureGopath() EnsureVendorDir() conf := EnsureConfig() // Lockfile exists if !gpath.HasLock(base) { msg.Info("Lock file (glide.lock) does not exist. Performing update.") Update(installer, false, stripVendor) return } // Load lockfile lock, err := cfg.ReadLockFile(filepath.Join(base, gpath.LockFile)) if err != nil { msg.Die("Could not load lockfile.") } // Verify lockfile hasn't changed hash, err := conf.Hash() if err != nil { msg.Die("Could not load lockfile.") } else if hash != lock.Hash { fmt.Println(hash, lock.Hash) foo, _ := conf.Marshal() fmt.Println(string(foo)) msg.Warn("Lock file may be out of date. Hash check of YAML failed. You may need to run 'update'") } // Install newConf, err := installer.Install(lock, conf) if err != nil { msg.Die("Failed to install: %s", err) } msg.Info("Setting references.") // Set reference if err := repo.SetReference(newConf, installer.ResolveTest); err != nil { msg.Die("Failed to set references: %s (Skip to cleanup)", err) } err = installer.Export(newConf) if err != nil { msg.Die("Unable to export dependencies to vendor directory: %s", err) } if stripVendor { msg.Info("Removing nested vendor and Godeps/_workspace directories...") err := gpath.StripVendor() if err != nil { msg.Err("Unable to strip vendor directories: %s", err) } } }
// NotFound attempts to retrieve a package when not found in the local vendor/ // folder. It will attempt to get it from the remote location info. func (m *MissingPackageHandler) NotFound(pkg string, addTest bool) (bool, error) { root := util.GetRootFromPackage(pkg) // Skip any references to the root package. if root == m.Config.Name { return false, nil } dest := filepath.Join(m.destination, root) // This package may have been placed on the list to look for when it wasn't // downloaded but it has since been downloaded before coming to this entry. if _, err := os.Stat(dest); err == nil { // Make sure the location contains files. It may be an empty directory. empty, err := gpath.IsDirectoryEmpty(dest) if err != nil { return false, err } if empty { msg.Warn("%s is an existing location with no files. Fetching a new copy of the dependency.", dest) msg.Debug("Removing empty directory %s", dest) err := os.RemoveAll(dest) if err != nil { msg.Debug("Installer error removing directory %s: %s", dest, err) return false, err } } else { msg.Debug("Found %s", dest) return true, nil } } msg.Info("Fetching %s into %s", pkg, m.destination) d := m.Config.Imports.Get(root) if d == nil && addTest { d = m.Config.DevImports.Get(root) } // If the dependency is nil it means the Config doesn't yet know about it. if d == nil { d, _ = m.Use.Get(root) // We don't know about this dependency so we create a basic instance. if d == nil { d = &cfg.Dependency{Name: root} } if addTest { m.Config.DevImports = append(m.Config.DevImports, d) } else { m.Config.Imports = append(m.Config.Imports, d) } } if err := VcsGet(d, dest, m.home, m.cache, m.cacheGopath, m.useGopath); err != nil { return false, err } return true, nil }
func buildPath(path string) error { msg.Info("Running go build %s\n", path) // . in a filepath.Join is removed so it needs to be prepended separately. p := "." + string(filepath.Separator) + filepath.Join("vendor", path) out, err := exec.Command("go", "install", p).CombinedOutput() if err != nil { msg.Warn("Failed to run 'go install' for %s: %s", path, string(out)) } return err }
func walkDeps(b *util.BuildCtxt, base, myName string) []string { externalDeps := []string{} filepath.Walk(base, func(path string, fi os.FileInfo, err error) error { if err != nil { return err } if !dependency.IsSrcDir(fi) { if fi.IsDir() { return filepath.SkipDir } return nil } var imps []string pkg, err := b.ImportDir(path, 0) if err != nil && strings.HasPrefix(err.Error(), "found packages ") { // If we got here it's because a package and multiple packages // declared. This is often because of an example with a package // or main but +build ignore as a build tag. In that case we // try to brute force the packages with a slower scan. imps, _, err = dependency.IterativeScan(path) if err != nil { msg.Err("Error walking dependencies for %s: %s", path, err) return err } } else if err != nil { if !strings.HasPrefix(err.Error(), "no buildable Go source") { msg.Warn("Error: %s (%s)", err, path) // Not sure if we should return here. //return err } } else { imps = pkg.Imports } if pkg.Goroot { return nil } for _, imp := range imps { //if strings.HasPrefix(imp, myName) { ////Info("Skipping %s because it is a subpackage of %s", imp, myName) //continue //} if imp == myName { continue } externalDeps = append(externalDeps, imp) } return nil }) return externalDeps }
// Rebuild rebuilds '.a' files for a project. // // Prior to Go 1.4, this could substantially reduce time on incremental compiles. // It remains to be seen whether this is tremendously beneficial to modern Go // programs. func Rebuild() { msg.Warn("The rebuild command is deprecated and will be removed in a future version") msg.Warn("Use the go install command instead") conf := EnsureConfig() vpath, err := gpath.Vendor() if err != nil { msg.Die("Could not get vendor path: %s", err) } msg.Info("Building dependencies.\n") if len(conf.Imports) == 0 { msg.Info("No dependencies found. Nothing built.\n") return } for _, dep := range conf.Imports { if err := buildDep(dep, vpath); err != nil { msg.Warn("Failed to build %s: %s\n", dep.Name, err) } } }
// NoVendor generates a list of source code directories, excepting `vendor/`. // // If "onlyGo" is true, only folders that have Go code in them will be returned. // // If suffix is true, this will append `/...` to every directory. func NoVendor(path string, onlyGo, suffix bool) { // This is responsible for printing the results of noVend. paths, err := noVend(path, onlyGo, suffix) if err != nil { msg.Error("Failed to walk file tree: %s", err) msg.Warn("FIXME: NoVendor should exit with non-zero exit code.") return } for _, p := range paths { msg.Puts(p) } }
// Update updates all dependencies. // // It begins with the dependencies in the config file, but also resolves // transitive dependencies. The returned lockfile has all of the dependencies // listed, but the version reconciliation has not been done. // // In other words, all versions in the Lockfile will be empty. func (i *Installer) Update(conf *cfg.Config) error { base := "." vpath := i.VendorPath() ic := newImportCache() m := &MissingPackageHandler{ destination: vpath, cache: i.UseCache, cacheGopath: i.UseCacheGopath, useGopath: i.UseGopath, home: i.Home, force: i.Force, updateVendored: i.UpdateVendored, Config: conf, Use: ic, updated: i.Updated, } v := &VersionHandler{ Destination: vpath, Use: ic, Imported: make(map[string]bool), Conflicts: make(map[string]bool), Config: conf, } // Update imports res, err := dependency.NewResolver(base) if err != nil { msg.Die("Failed to create a resolver: %s", err) } res.Config = conf res.Handler = m res.VersionHandler = v res.ResolveAllFiles = i.ResolveAllFiles msg.Info("Resolving imports") _, err = allPackages(conf.Imports, res) if err != nil { msg.Die("Failed to retrieve a list of dependencies: %s", err) } if len(conf.DevImports) > 0 { msg.Warn("dev imports not resolved.") } err = ConcurrentUpdate(conf.Imports, vpath, i, conf) return err }
// SetVersion sets the version for a package. If that package version is already // set it handles the case by: // - keeping the already set version // - proviting messaging about the version conflict // TODO(mattfarina): The way version setting happens can be improved. Currently not optimal. func (d *VersionHandler) SetVersion(pkg string) (e error) { root := util.GetRootFromPackage(pkg) // Skip any references to the root package. if root == d.Config.Name { return nil } v := d.Config.Imports.Get(root) dep, req := d.Use.Get(root) if dep != nil && v != nil { if v.Reference == "" && dep.Reference != "" { v.Reference = dep.Reference // Clear the pin, if set, so the new version can be used. v.Pin = "" dep = v } else if v.Reference != "" && dep.Reference != "" && v.Reference != dep.Reference { dest := filepath.Join(d.Destination, filepath.FromSlash(v.Name)) dep = determineDependency(v, dep, dest, req) } else { dep = v } } else if v != nil { dep = v } else if dep != nil { // We've got an imported dependency to use and don't already have a // record of it. Append it to the Imports. d.Config.Imports = append(d.Config.Imports, dep) } else { // If we've gotten here we don't have any depenency objects. r, sp := util.NormalizeName(pkg) dep = &cfg.Dependency{ Name: r, } if sp != "" { dep.Subpackages = []string{sp} } d.Config.Imports = append(d.Config.Imports, dep) } err := VcsVersion(dep, d.Destination) if err != nil { msg.Warn("Unable to set version on %s to %s. Err: %s", root, dep.Reference, err) e = err } return }
// ConcurrentUpdate takes a list of dependencies and updates in parallel. func ConcurrentUpdate(deps []*cfg.Dependency, cwd string, i *Installer) error { done := make(chan struct{}, concurrentWorkers) in := make(chan *cfg.Dependency, concurrentWorkers) var wg sync.WaitGroup var lock sync.Mutex var returnErr error msg.Info("Downloading dependencies. Please wait...") for ii := 0; ii < concurrentWorkers; ii++ { go func(ch <-chan *cfg.Dependency) { for { select { case dep := <-ch: dest := filepath.Join(i.VendorPath(), dep.Name) if err := VcsUpdate(dep, dest, i.Home, i.UseCache, i.UseCacheGopath, i.UseGopath, i.Force, i.UpdateVendored); err != nil { msg.Warn("Update failed for %s: %s\n", dep.Name, err) // Capture the error while making sure the concurrent // operations don't step on each other. lock.Lock() if returnErr == nil { returnErr = err } else { returnErr = cli.NewMultiError(returnErr, err) } lock.Unlock() } wg.Done() case <-done: return } } }(in) } for _, dep := range deps { wg.Add(1) in <- dep } wg.Wait() // Close goroutines setting the version for ii := 0; ii < concurrentWorkers; ii++ { done <- struct{}{} } return returnErr }
// ListDeps lists all of the dependencies of the current project. // // Params: // - dir (string): basedir // - deep (bool): whether to do a deep scan or a shallow scan // // Returns: // func ListDeps(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) { basedir := p.Get("dir", ".").(string) deep := p.Get("deep", true).(bool) basedir, err := filepath.Abs(basedir) if err != nil { return nil, err } r, err := dependency.NewResolver(basedir) if err != nil { return nil, err } h := &dependency.DefaultMissingPackageHandler{Missing: []string{}, Gopath: []string{}} r.Handler = h sortable, err := r.ResolveLocal(deep) if err != nil { return nil, err } sort.Strings(sortable) fmt.Println("INSTALLED packages:") for _, k := range sortable { v, err := filepath.Rel(basedir, k) if err != nil { msg.Warn("Failed to Rel path: %s", err) v = k } fmt.Printf("\t%s\n", v) } if len(h.Missing) > 0 { fmt.Println("\nMISSING packages:") for _, pkg := range h.Missing { fmt.Printf("\t%s\n", pkg) } } if len(h.Gopath) > 0 { fmt.Println("\nGOPATH packages:") for _, pkg := range h.Gopath { fmt.Printf("\t%s\n", pkg) } } return nil, nil }
// MirrorsRemove removes a mirrors setting func MirrorsRemove(k string) error { if k == "" { msg.Err("The mirror to remove is required") return nil } home := gpath.Home() op := filepath.Join(home, "mirrors.yaml") if _, err := os.Stat(op); os.IsNotExist(err) { msg.Err("mirrors.yaml file not found") return nil } ov, err := mirrors.ReadMirrorsFile(op) if err != nil { msg.Die("Unable to read mirrors.yaml file: %s", err) } var nre mirrors.MirrorRepos var found bool for _, re := range ov.Repos { if re.Original != k { nre = append(nre, re) } else { found = true } } if !found { msg.Warn("%s was not found in mirrors", k) } else { msg.Info("%s was removed from mirrors", k) ov.Repos = nre err = ov.WriteFile(op) if err != nil { msg.Err("Error writing mirrors.yaml file: %s", err) } else { msg.Info("mirrors.yaml written with changes") } } return nil }
// ConcurrentUpdate takes a list of dependencies and updates in parallel. func ConcurrentUpdate(deps []*cfg.Dependency, cwd string, i *Installer) error { done := make(chan struct{}, concurrentWorkers) in := make(chan *cfg.Dependency, concurrentWorkers) var wg sync.WaitGroup var lock sync.Mutex var returnErr error for ii := 0; ii < concurrentWorkers; ii++ { go func(ch <-chan *cfg.Dependency) { for { select { case dep := <-ch: if err := VcsUpdate(dep, cwd, i); err != nil { msg.Warn("Update failed for %s: %s\n", dep.Name, err) // Capture the error while making sure the concurrent // operations don't step on each other. lock.Lock() if returnErr == nil { returnErr = err } else { returnErr = cli.NewMultiError(returnErr, err) } lock.Unlock() } wg.Done() case <-done: return } } }(in) } for _, dep := range deps { wg.Add(1) in <- dep } wg.Wait() // Close goroutines setting the version for ii := 0; ii < concurrentWorkers; ii++ { done <- struct{}{} } return returnErr }
// List lists all of the dependencies of the current project. // // Params: // - dir (string): basedir // - deep (bool): whether to do a deep scan or a shallow scan func List(basedir string, deep bool) { basedir, err := filepath.Abs(basedir) if err != nil { msg.Die("Could not read directory: %s", err) } r, err := dependency.NewResolver(basedir) if err != nil { msg.Die("Could not create a resolver: %s", err) } h := &dependency.DefaultMissingPackageHandler{Missing: []string{}, Gopath: []string{}} r.Handler = h sortable, err := r.ResolveLocal(deep) if err != nil { msg.Die("Error listing dependencies: %s", err) } sort.Strings(sortable) msg.Puts("INSTALLED packages:") for _, k := range sortable { v, err := filepath.Rel(basedir, k) if err != nil { msg.Warn("Failed to Rel path: %s", err) v = k } msg.Puts("\t%s", v) } if len(h.Missing) > 0 { msg.Puts("\nMISSING packages:") for _, pkg := range h.Missing { msg.Puts("\t%s", pkg) } } if len(h.Gopath) > 0 { msg.Puts("\nGOPATH packages:") for _, pkg := range h.Gopath { msg.Puts("\t%s", pkg) } } }
// Rebuild rebuilds '.a' files for a project. // // Prior to Go 1.4, this could substantially reduce time on incremental compiles. // It remains to be seen whether this is tremendously beneficial to modern Go // programs. func Rebuild() { conf := EnsureConfig() vpath, err := gpath.Vendor() if err != nil { msg.Die("Could not get vendor path: %s", err) } msg.Info("Building dependencies.\n") if len(conf.Imports) == 0 { msg.Info("No dependencies found. Nothing built.\n") return } for _, dep := range conf.Imports { if err := buildDep(dep, vpath); err != nil { msg.Warn("Failed to build %s: %s\n", dep.Name, err) } } }
// EnsureGopath fails if GOPATH is not set, or if $GOPATH/src is missing. // // Otherwise it returns the value of GOPATH. func EnsureGopath() string { gps := gpath.Gopaths() if len(gps) == 0 { msg.Die("$GOPATH is not set.") } for _, gp := range gps { _, err := os.Stat(path.Join(gp, "src")) if err != nil { msg.Warn("%s", err) continue } return gp } msg.Error("Could not find any of %s/src.\n", strings.Join(gps, "/src, ")) msg.Info("As of Glide 0.5/Go 1.5, this is required.\n") msg.Die("Wihtout src, cannot continue.") return "" }
func buildDep(dep *cfg.Dependency, vpath string) error { if len(dep.Subpackages) == 0 { buildPath(dep.Name) } for _, pkg := range dep.Subpackages { if pkg == "**" || pkg == "..." { //Info("Building all packages in %s\n", dep.Name) buildPath(path.Join(dep.Name, "...")) } else { paths, err := resolvePackages(vpath, dep.Name, pkg) if err != nil { msg.Warn("Error resolving packages: %s", err) } buildPaths(paths) } } return nil }
// LoadLockfile loads the contents of a glide.lock file. // // TODO: This should go in another package. func LoadLockfile(base string, conf *cfg.Config) (*cfg.Lockfile, error) { yml, err := ioutil.ReadFile(filepath.Join(base, gpath.LockFile)) if err != nil { return nil, err } lock, err := cfg.LockfileFromYaml(yml) if err != nil { return nil, err } hash, err := conf.Hash() if err != nil { return nil, err } if hash != lock.Hash { msg.Warn("Lock file may be out of date. Hash check of YAML failed. You may need to run 'update'") } return lock, nil }
// Tree prints a tree representing dependencies. func Tree(basedir string, showcore bool) { msg.Warn("The tree command is deprecated and will be removed in a future version") buildContext, err := util.GetBuildContext() if err != nil { msg.Die("Failed to get a build context: %s", err) } myName := buildContext.PackageName(basedir) if basedir == "." { var err error basedir, err = os.Getwd() if err != nil { msg.Die("Could not get working directory") } } msg.Puts(myName) l := list.New() l.PushBack(myName) tree.Display(buildContext, basedir, myName, 1, showcore, l) }
func walkDeps(b *util.BuildCtxt, base, myName string) []string { externalDeps := []string{} filepath.Walk(base, func(path string, fi os.FileInfo, err error) error { if !dependency.IsSrcDir(fi) { if fi.IsDir() { return filepath.SkipDir } return nil } pkg, err := b.ImportDir(path, 0) if err != nil { if !strings.HasPrefix(err.Error(), "no buildable Go source") { msg.Warn("Error: %s (%s)", err, path) // Not sure if we should return here. //return err } } if pkg.Goroot { return nil } for _, imp := range pkg.Imports { //if strings.HasPrefix(imp, myName) { ////Info("Skipping %s because it is a subpackage of %s", imp, myName) //continue //} if imp == myName { continue } externalDeps = append(externalDeps, imp) } return nil }) return externalDeps }
// List resolves the complete dependency tree and returns a list of dependencies. func (i *Installer) List(conf *cfg.Config) []*cfg.Dependency { base := "." ic := newImportCache() v := &VersionHandler{ Use: ic, Imported: make(map[string]bool), Conflicts: make(map[string]bool), Config: conf, } // Update imports res, err := dependency.NewResolver(base) if err != nil { msg.Die("Failed to create a resolver: %s", err) } res.Config = conf res.VersionHandler = v res.ResolveAllFiles = i.ResolveAllFiles msg.Info("Resolving imports") _, _, err = res.ResolveLocal(false) if err != nil { msg.Die("Failed to resolve local packages: %s", err) } _, err = allPackages(conf.Imports, res, false) if err != nil { msg.Die("Failed to retrieve a list of dependencies: %s", err) } if len(conf.DevImports) > 0 { msg.Warn("dev imports not resolved.") } return conf.Imports }
func commands() []cli.Command { return []cli.Command{ { Name: "create", ShortName: "init", Usage: "Initialize a new project, creating a glide.yaml file", Description: `This command starts from a project without Glide and sets it up. It generates a glide.yaml file, parsing your codebase to guess the dependencies to include. Once this step is done you may edit the glide.yaml file to update imported dependency properties such as the version or version range to include. To fetch the dependencies you may run 'glide install'.`, Flags: []cli.Flag{ cli.BoolFlag{ Name: "skip-import", Usage: "When initializing skip importing from other package managers.", }, cli.BoolFlag{ Name: "non-interactive", Usage: "Disable interactive prompts.", }, }, Action: func(c *cli.Context) { action.Create(".", c.Bool("skip-import"), c.Bool("non-interactive")) }, }, { Name: "config-wizard", ShortName: "cw", Usage: "Wizard that makes optional suggestions to improve config in a glide.yaml file.", Description: `Glide will analyze a projects glide.yaml file and the imported projects to find ways the glide.yaml file can potentially be improved. It will then interactively make suggestions that you can skip or accept.`, Action: func(c *cli.Context) { action.ConfigWizard(".") }, }, { Name: "get", Usage: "Install one or more packages into `vendor/` and add dependency to glide.yaml.", Description: `Gets one or more package (like 'go get') and then adds that file to the glide.yaml file. Multiple package names can be specified on one line. $ glide get github.com/Masterminds/cookoo/web The above will install the project github.com/Masterminds/cookoo and add the subpackage 'web'. If a fetched dependency has a glide.yaml file, configuration from Godep, GPM, or GB Glide that configuration will be used to find the dependencies and versions to fetch. If those are not available the dependent packages will be fetched as either a version specified elsewhere or the latest version. When adding a new dependency Glide will perform an update to work out the the versions to use from the dependency tree. This will generate an updated glide.lock file with specific locked versions to use. If you are storing the outside dependencies in your version control system (VCS), also known as vendoring, there are a few flags that may be useful. The '--update-vendored' flag will cause Glide to update packages when VCS information is unavailable. This can be used with the '--strip-vcs' flag which will strip VCS data found in the vendor directory. This is useful for removing VCS data from transitive dependencies and initial setups. The '--strip-vendor' flag will remove any nested 'vendor' folders and 'Godeps/_workspace' folders after an update (along with undoing any Godep import rewriting). Note, The Godeps specific functionality is deprecated and will be removed when most Godeps users have migrated to using the vendor folder.`, Flags: []cli.Flag{ cli.BoolFlag{ Name: "insecure", Usage: "Use http:// rather than https:// to retrieve pacakges.", }, cli.BoolFlag{ Name: "no-recursive, quick", Usage: "Disable updating dependencies' dependencies.", }, cli.BoolFlag{ Name: "force", Usage: "If there was a change in the repo or VCS switch to new one. Warning, changes will be lost.", }, cli.BoolFlag{ Name: "all-dependencies", Usage: "This will resolve all dependencies for all packages, not just those directly used.", }, cli.BoolFlag{ Name: "update-vendored, u", Usage: "Update vendored packages (without local VCS repo). Warning, changes will be lost.", }, cli.BoolFlag{ Name: "cache", Usage: "When downloading dependencies attempt to cache them.", }, cli.BoolFlag{ Name: "cache-gopath", Usage: "When downloading dependencies attempt to put them in the GOPATH, too.", }, cli.BoolFlag{ Name: "use-gopath", Usage: "Copy dependencies from the GOPATH if they exist there.", }, cli.BoolFlag{ Name: "resolve-current", Usage: "Resolve dependencies for only the current system rather than all build modes.", }, cli.BoolFlag{ Name: "strip-vcs, s", Usage: "Removes version control metadata (e.g, .git directory) from the vendor folder.", }, cli.BoolFlag{ Name: "strip-vendor, v", Usage: "Removes nested vendor and Godeps/_workspace directories. Requires --strip-vcs.", }, cli.BoolFlag{ Name: "non-interactive", Usage: "Disable interactive prompts.", }, }, Action: func(c *cli.Context) { if c.Bool("strip-vendor") && !c.Bool("strip-vcs") { msg.Die("--strip-vendor cannot be used without --strip-vcs") } if len(c.Args()) < 1 { fmt.Println("Oops! Package name is required.") os.Exit(1) } if c.Bool("resolve-current") { util.ResolveCurrent = true msg.Warn("Only resolving dependencies for the current OS/Arch") } inst := repo.NewInstaller() inst.Force = c.Bool("force") inst.UseCache = c.Bool("cache") inst.UseGopath = c.Bool("use-gopath") inst.UseCacheGopath = c.Bool("cache-gopath") inst.UpdateVendored = c.Bool("update-vendored") inst.ResolveAllFiles = c.Bool("all-dependencies") packages := []string(c.Args()) insecure := c.Bool("insecure") action.Get(packages, inst, insecure, c.Bool("no-recursive"), c.Bool("strip-vcs"), c.Bool("strip-vendor"), c.Bool("non-interactive")) }, }, { Name: "remove", ShortName: "rm", Usage: "Remove a package from the glide.yaml file, and regenerate the lock file.", Description: `This takes one or more package names, and removes references from the glide.yaml file. This will rebuild the glide lock file with the following constraints: - Dependencies are re-negotiated. Any that are no longer used are left out of the lock. - Minor version re-nogotiation is performed on remaining dependencies. - No updates are peformed. You may want to run 'glide up' to accomplish that.`, Flags: []cli.Flag{ cli.BoolFlag{ Name: "delete,d", Usage: "Also delete from vendor/ any packages that are no longer used.", }, }, Action: func(c *cli.Context) { if len(c.Args()) < 1 { fmt.Println("Oops! At least one package name is required.") os.Exit(1) } if c.Bool("delete") { // FIXME: Implement this in the installer. fmt.Println("Delete is not currently implemented.") } inst := repo.NewInstaller() inst.Force = c.Bool("force") packages := []string(c.Args()) action.Remove(packages, inst) }, }, { Name: "import", Usage: "Import files from other dependency management systems.", Subcommands: []cli.Command{ { Name: "godep", Usage: "Import Godep's Godeps.json files and display the would-be yaml file", Flags: []cli.Flag{ cli.StringFlag{ Name: "file, f", Usage: "Save all of the discovered dependencies to a Glide YAML file.", }, }, Action: func(c *cli.Context) { action.ImportGodep(c.String("file")) }, }, { Name: "gpm", Usage: "Import GPM's Godeps and Godeps-Git files and display the would-be yaml file", Flags: []cli.Flag{ cli.StringFlag{ Name: "file, f", Usage: "Save all of the discovered dependencies to a Glide YAML file.", }, }, Action: func(c *cli.Context) { action.ImportGPM(c.String("file")) }, }, { Name: "gb", Usage: "Import gb's manifest file and display the would-be yaml file", Flags: []cli.Flag{ cli.StringFlag{ Name: "file, f", Usage: "Save all of the discovered dependencies to a Glide YAML file.", }, }, Action: func(c *cli.Context) { action.ImportGB(c.String("file")) }, }, { Name: "gom", Usage: "Import Gomfile and display the would-be yaml file", Flags: []cli.Flag{ cli.StringFlag{ Name: "file, f", Usage: "Save all of the discovered dependencies to a Glide YAML file.", }, }, Action: func(c *cli.Context) { action.ImportGom(c.String("file")) }, }, }, }, { Name: "name", Usage: "Print the name of this project.", Description: `Read the glide.yaml file and print the name given on the 'package' line.`, Action: func(c *cli.Context) { action.Name() }, }, { Name: "novendor", ShortName: "nv", Usage: "List all non-vendor paths in a directory.", Description: `Given a directory, list all the relevant Go paths that are not vendored. Example: $ go test $(glide novendor)`, Flags: []cli.Flag{ cli.StringFlag{ Name: "dir,d", Usage: "Specify a directory to run novendor against.", Value: ".", }, cli.BoolFlag{ Name: "no-subdir,x", Usage: "Specify this to prevent nv from append '/...' to all directories.", }, }, Action: func(c *cli.Context) { action.NoVendor(c.String("dir"), true, !c.Bool("no-subdir")) }, }, { Name: "rebuild", Usage: "Rebuild ('go build') the dependencies", Description: `This rebuilds the packages' '.a' files. On some systems this can improve performance on subsequent 'go run' and 'go build' calls.`, Action: func(c *cli.Context) { action.Rebuild() }, }, { Name: "install", ShortName: "i", Usage: "Install a project's dependencies", Description: `This uses the native VCS of each packages to install the appropriate version. There are two ways a projects dependencies can be installed. When there is a glide.yaml file defining the dependencies but no lock file (glide.lock) the dependencies are installed using the "update" command and a glide.lock file is generated pinning all dependencies. If a glide.lock file is already present the dependencies are installed or updated from the lock file.`, Flags: []cli.Flag{ cli.BoolFlag{ Name: "delete", Usage: "Delete vendor packages not specified in config.", }, cli.BoolFlag{ Name: "force", Usage: "If there was a change in the repo or VCS switch to new one. Warning: changes will be lost.", }, cli.BoolFlag{ Name: "update-vendored, u", Usage: "Update vendored packages (without local VCS repo). Warning: this may destroy local modifications to vendor/.", }, cli.StringFlag{ Name: "file, f", Usage: "Save all of the discovered dependencies to a Glide YAML file. (DEPRECATED: This has no impact.)", }, cli.BoolFlag{ Name: "cache", Usage: "When downloading dependencies attempt to cache them.", }, cli.BoolFlag{ Name: "cache-gopath", Usage: "When downloading dependencies attempt to put them in the GOPATH, too.", }, cli.BoolFlag{ Name: "use-gopath", Usage: "Copy dependencies from the GOPATH if they exist there.", }, cli.BoolFlag{ Name: "strip-vcs, s", Usage: "Removes version control metadata (e.g, .git directory) from the vendor folder.", }, cli.BoolFlag{ Name: "strip-vendor, v", Usage: "Removes nested vendor and Godeps/_workspace directories. Requires --strip-vcs.", }, }, Action: func(c *cli.Context) { if c.Bool("strip-vendor") && !c.Bool("strip-vcs") { msg.Die("--strip-vendor cannot be used without --strip-vcs") } installer := repo.NewInstaller() installer.Force = c.Bool("force") installer.UseCache = c.Bool("cache") installer.UseGopath = c.Bool("use-gopath") installer.UseCacheGopath = c.Bool("cache-gopath") installer.UpdateVendored = c.Bool("update-vendored") installer.Home = c.GlobalString("home") installer.DeleteUnused = c.Bool("delete") action.Install(installer, c.Bool("strip-vcs"), c.Bool("strip-vendor")) }, }, { Name: "update", ShortName: "up", Usage: "Update a project's dependencies", Description: `This uses the native VCS of each package to try to pull the most applicable updates. Packages with fixed refs (Versions or tags) will not be updated. Packages with no ref or with a branch ref will be updated as expected. If a dependency has a glide.yaml file, update will read that file and update those dependencies accordingly. Those dependencies are maintained in a the top level 'vendor/' directory. 'vendor/foo/bar' will have its dependencies stored in 'vendor/'. This behavior can be disabled with '--no-recursive'. When this behavior is skipped a glide.lock file is not generated because the full dependency tree cannot be known. Glide will also import Godep, GB, and GPM files as it finds them in dependencies. It will create a glide.yaml file from the Godeps data, and then update. This has no effect if '--no-recursive' is set. If you are storing the outside dependencies in your version control system (VCS), also known as vendoring, there are a few flags that may be useful. The '--update-vendored' flag will cause Glide to update packages when VCS information is unavailable. This can be used with the '--strip-vcs' flag which will strip VCS data found in the vendor directory. This is useful for removing VCS data from transitive dependencies and initial setups. The '--strip-vendor' flag will remove any nested 'vendor' folders and 'Godeps/_workspace' folders after an update (along with undoing any Godep import rewriting). Note, The Godeps specific functionality is deprecated and will be removed when most Godeps users have migrated to using the vendor folder. Note, Glide detects vendored dependencies. With the '--update-vendored' flag Glide will update vendored dependencies leaving them in a vendored state. Tertiary dependencies will not be vendored automatically unless the '--strip-vcs' flag is used along with it. By default, packages that are discovered are considered transient, and are not stored in the glide.yaml file. The --file=NAME.yaml flag allows you to save the discovered dependencies to a YAML file.`, Flags: []cli.Flag{ cli.BoolFlag{ Name: "delete", Usage: "Delete vendor packages not specified in config.", }, cli.BoolFlag{ Name: "no-recursive, quick", Usage: "Disable updating dependencies' dependencies. Only update things in glide.yaml.", }, cli.BoolFlag{ Name: "force", Usage: "If there was a change in the repo or VCS switch to new one. Warning, changes will be lost.", }, cli.BoolFlag{ Name: "all-dependencies", Usage: "This will resolve all dependencies for all packages, not just those directly used.", }, cli.BoolFlag{ Name: "update-vendored, u", Usage: "Update vendored packages (without local VCS repo). Warning, changes will be lost.", }, cli.StringFlag{ Name: "file, f", Usage: "Save all of the discovered dependencies to a Glide YAML file.", }, cli.BoolFlag{ Name: "cache", Usage: "When downloading dependencies attempt to cache them.", }, cli.BoolFlag{ Name: "cache-gopath", Usage: "When downloading dependencies attempt to put them in the GOPATH, too.", }, cli.BoolFlag{ Name: "use-gopath", Usage: "Copy dependencies from the GOPATH if they exist there.", }, cli.BoolFlag{ Name: "resolve-current", Usage: "Resolve dependencies for only the current system rather than all build modes.", }, cli.BoolFlag{ Name: "strip-vcs, s", Usage: "Removes version control metadata (e.g, .git directory) from the vendor folder.", }, cli.BoolFlag{ Name: "strip-vendor, v", Usage: "Removes nested vendor and Godeps/_workspace directories. Requires --strip-vcs.", }, }, Action: func(c *cli.Context) { if c.Bool("strip-vendor") && !c.Bool("strip-vcs") { msg.Die("--strip-vendor cannot be used without --strip-vcs") } if c.Bool("resolve-current") { util.ResolveCurrent = true msg.Warn("Only resolving dependencies for the current OS/Arch") } installer := repo.NewInstaller() installer.Force = c.Bool("force") installer.UseCache = c.Bool("cache") installer.UseGopath = c.Bool("use-gopath") installer.UseCacheGopath = c.Bool("cache-gopath") installer.UpdateVendored = c.Bool("update-vendored") installer.ResolveAllFiles = c.Bool("all-dependencies") installer.Home = c.GlobalString("home") installer.DeleteUnused = c.Bool("delete") action.Update(installer, c.Bool("no-recursive"), c.Bool("strip-vcs"), c.Bool("strip-vendor")) }, }, { Name: "tree", Usage: "Tree prints the dependencies of this project as a tree.", Description: `This scans a project's source files and builds a tree representation of the import graph. It ignores testdata/ and directories that begin with . or _. Packages in vendor/ are only included if they are referenced by the main project or one of its dependencies.`, Action: func(c *cli.Context) { action.Tree(".", false) }, }, { Name: "list", Usage: "List prints all dependencies that the present code references.", Description: `List scans your code and lists all of the packages that are used. It does not use the glide.yaml. Instead, it inspects the code to determine what packages are imported. Directories that begin with . or _ are ignored, as are testdata directories. Packages in vendor are only included if they are used by the project.`, Action: func(c *cli.Context) { action.List(".", true, c.String("output")) }, Flags: []cli.Flag{ cli.StringFlag{ Name: "output, o", Usage: "Output format. One of: json|json-pretty|text", Value: "text", }, }, }, { Name: "info", Usage: "Info prints information about this project", Flags: []cli.Flag{ cli.StringFlag{ Name: "format, f", Usage: `Format of the information wanted (required).`, }, }, Description: `A format containing the text with replacement variables has to be passed in. Those variables are: %n - name %d - description %h - homepage %l - license For example, given a project with the following glide.yaml: package: foo homepage: https://example.com license: MIT description: Some example description Then running the following commands: glide info -f %n prints 'foo' glide info -f "License: %l" prints 'License: MIT' glide info -f "%n - %d - %h - %l" prints 'foo - Some example description - https://example.com - MIT'`, Action: func(c *cli.Context) { if c.IsSet("format") { action.Info(c.String("format")) } else { cli.ShowCommandHelp(c, c.Command.Name) } }, }, { Name: "cache-clear", ShortName: "cc", Usage: "Clears the Glide cache.", Action: func(c *cli.Context) { action.CacheClear() }, }, { Name: "about", Usage: "Learn about Glide", Action: func(c *cli.Context) { action.About() }, }, } }
// ConfigWizard reads configuration from a glide.yaml file and attempts to suggest // improvements. The wizard is interactive. func ConfigWizard(base string) { _, err := gpath.Glide() glidefile := gpath.GlideFile if err != nil { msg.Info("Unable to find a glide.yaml file. Would you like to create one now? Yes (Y) or No (N)") bres := msg.PromptUntilYorN() if bres { // Guess deps conf := guessDeps(base, false) // Write YAML if err := conf.WriteFile(glidefile); err != nil { msg.Die("Could not save %s: %s", glidefile, err) } } else { msg.Err("Unable to find configuration file. Please create configuration information to continue.") } } conf := EnsureConfig() err = cache.Setup() if err != nil { msg.Die("Problem setting up cache: %s", err) } msg.Info("Looking for dependencies to make suggestions on") msg.Info("--> Scanning for dependencies not using version ranges") msg.Info("--> Scanning for dependencies using commit ids") var deps []*cfg.Dependency for _, dep := range conf.Imports { if wizardLookInto(dep) { deps = append(deps, dep) } } for _, dep := range conf.DevImports { if wizardLookInto(dep) { deps = append(deps, dep) } } msg.Info("Gathering information on each dependency") msg.Info("--> This may take a moment. Especially on a codebase with many dependencies") msg.Info("--> Gathering release information for dependencies") msg.Info("--> Looking for dependency imports where versions are commit ids") for _, dep := range deps { wizardFindVersions(dep) } var changes int for _, dep := range deps { var remote string if dep.Repository != "" { remote = dep.Repository } else { remote = "https://" + dep.Name } // First check, ask if the tag should be used instead of the commit id for it. cur := cache.MemCurrent(remote) if cur != "" && cur != dep.Reference { wizardSugOnce() var dres bool asked, use, val := wizardOnce("current") if !use { dres = wizardAskCurrent(cur, dep) } if !asked { as := wizardRemember() wizardSetOnce("current", as, dres) } if asked && use { dres = val.(bool) } if dres { msg.Info("Updating %s to use the tag %s instead of commit id %s", dep.Name, cur, dep.Reference) dep.Reference = cur changes++ } } // Second check, if no version is being used and there's a semver release ask about latest. memlatest := cache.MemLatest(remote) if dep.Reference == "" && memlatest != "" { wizardSugOnce() var dres bool asked, use, val := wizardOnce("latest") if !use { dres = wizardAskLatest(memlatest, dep) } if !asked { as := wizardRemember() wizardSetOnce("latest", as, dres) } if asked && use { dres = val.(bool) } if dres { msg.Info("Updating %s to use the release %s instead of no release", dep.Name, memlatest) dep.Reference = memlatest changes++ } } // Third check, if the version is semver offer to use a range instead. sv, err := semver.NewVersion(dep.Reference) if err == nil { wizardSugOnce() var res string asked, use, val := wizardOnce("range") if !use { res = wizardAskRange(sv, dep) } if !asked { as := wizardRemember() wizardSetOnce("range", as, res) } if asked && use { res = val.(string) } if res == "m" { r := "^" + sv.String() msg.Info("Updating %s to use the range %s instead of commit id %s", dep.Name, r, dep.Reference) dep.Reference = r changes++ } else if res == "p" { r := "~" + sv.String() msg.Info("Updating %s to use the range %s instead of commit id %s", dep.Name, r, dep.Reference) dep.Reference = r changes++ } } } if changes > 0 { msg.Info("Configuration changes have been made. Would you like to write these") msg.Info("changes to your configuration file? Yes (Y) or No (N)") dres := msg.PromptUntilYorN() if dres { msg.Info("Writing updates to configuration file (%s)", glidefile) if err := conf.WriteFile(glidefile); err != nil { msg.Die("Could not save %s: %s", glidefile, err) } msg.Info("You can now edit the glide.yaml file.:") msg.Info("--> For more information on versions and ranges see https://glide.sh/docs/versions/") msg.Info("--> For details on additional metadata see https://glide.sh/docs/glide.yaml/") } else { msg.Warn("Change not written to configuration file") } } else { msg.Info("No proposed changes found. Have a nice day.") } }