Exemplo n.º 1
0
Arquivo: set.go Projeto: vito/gocart
func (s *Set) UnmarshalText(text []byte) error {
	lines := bufio.NewScanner(bytes.NewReader(text))

	for lines.Scan() {
		words := bufio.NewScanner(bytes.NewReader(lines.Bytes()))
		words.Split(bufio.ScanWords)

		count := 0
		dep := dependency.Dependency{}

		for words.Scan() {
			if strings.HasPrefix(words.Text(), "#") {
				break
			}

			if count == 0 {
				dep.Path = words.Text()
			} else if count == 1 {
				if words.Text() == "*" {
					dep.BleedingEdge = true
				} else {
					dep.Version = words.Text()
				}
			} else if count == 2 {
				dep.Tags = strings.Split(words.Text(), ",")
			}

			count++
		}

		if count == 0 {
			// blank line
			continue
		}

		if count == 1 {
			return MissingVersionError{dep.Path}
		}

		// check for dupes
		for _, existing := range s.Dependencies {
			if strings.HasPrefix(dep.Path+"/", existing.Path+"/") {
				return DuplicateDependencyError{existing, dep}
			}

			if strings.HasPrefix(existing.Path+"/", dep.Path+"/") {
				return DuplicateDependencyError{existing, dep}
			}
		}

		s.Dependencies = append(s.Dependencies, dep)
	}

	return nil
}
Exemplo n.º 2
0
func (f *Fetcher) Fetch(dep dependency.Dependency) (dependency.Dependency, error) {
	var goGet *exec.Cmd

	repoPath := dep.FullPath(f.gopath)

	lockDown := true
	updateRepo := false

	if dep.BleedingEdge {
		// update the repo only if bleeding-edge and repo is clean
		if _, err := os.Stat(repoPath); err == nil {
			lockDown = false

			repo, err := repository.New(repoPath, f.runner)
			if err != nil {
				return dependency.Dependency{}, err
			}

			statusOut, err := repo.Status()
			if err != nil {
				return dependency.Dependency{}, err
			}

			if len(statusOut) == 0 {
				updateRepo = true
			}
		}
	}

	if updateRepo {
		goGet = exec.Command("go", "get", "-u", "-d", "-v", dep.Path)
	} else {
		goGet = exec.Command("go", "get", "-d", "-v", dep.Path)
	}

	err := f.runner.Run(goGet)
	if err != nil {
		return dependency.Dependency{}, err
	}

	repo, err := repository.New(repoPath, f.runner)
	if err != nil {
		return dependency.Dependency{}, err
	}

	if lockDown {
		err := f.syncRepo(repo, dep.Version)
		if err != nil {
			return dependency.Dependency{}, err
		}
	}

	currentVersion, err := repo.CurrentVersion()
	if err != nil {
		return dependency.Dependency{}, err
	}

	dep.Version = currentVersion

	fetched, found := f.fetchedDependencies[dep.Path]
	if found {
		if fetched.Version != dep.Version {
			return dependency.Dependency{}, VersionConflictError{
				Path:     dep.Path,
				VersionA: fetched.Version,
				VersionB: dep.Version,
			}
		}
	} else {
		f.fetchedDependencies[dep.Path] = dep
	}

	return dep, nil
}