コード例 #1
0
ファイル: packages.go プロジェクト: abutre/bunch
func installPackage(repo string) error {
	wd, err := os.Getwd()
	if err != nil {
		return errors.Trace(err)
	}

	gopath := os.Getenv("GOPATH")
	packageDir := path.Join(gopath, "src", getRealRepoPath(repo))

	defer func() {
		_ = os.Chdir(wd)
	}()

	err = os.Chdir(packageDir)
	if err != nil {
		return errors.Trace(err)
	}

	var s *spinner.Spinner

	if Verbose {
		s = spinner.New(spinner.CharSets[SpinnerCharSet], SpinnerInterval)
		s.Prefix = fmt.Sprintf("  - installing package %s ", repo)
		s.Color("green")
		s.Start()
	}

	goInstallCommand := []string{"go", "install", repo}
	goInstallOutput, err := exec.Command(goInstallCommand[0], goInstallCommand[1:]...).CombinedOutput()

	if Verbose {
		s.Stop()
		fmt.Printf("\r  - installing package %s ... %s\n", repo, color.GreenString("done"))
	}

	if err != nil {
		return errors.Annotatef(err, "failed installing package %s, error: %s, output: %s", repo, goInstallOutput)
	}

	return nil
}
コード例 #2
0
ファイル: packages.go プロジェクト: abutre/bunch
func fetchPackage(repo string) error {
	wd, err := os.Getwd()
	if err != nil {
		return errors.Trace(err)
	}

	gopath := os.Getenv("GOPATH")
	packageDir := path.Join(gopath, "src", getRealRepoPath(repo))

	if _, err := os.Stat(packageDir); err != nil {
		if os.IsNotExist(err) {
			var s *spinner.Spinner
			if Verbose {
				s = spinner.New(spinner.CharSets[SpinnerCharSet], SpinnerInterval)
				s.Prefix = fmt.Sprintf("fetching %s ", repo)
				s.Color("green")
				s.Start()
			}

			goGetCommand := []string{"go", "get", "-d", repo}
			goGetCmd := exec.Command(goGetCommand[0], goGetCommand[1:]...)
			err := goGetCmd.Run()

			if Verbose {
				s.Stop()
			}

			if err != nil {
				return errors.Annotatef(err, "failed cloning repo for package %s", repo)
			} else {
				if Verbose {
					fmt.Printf("\rfetching %s ... %s\n", repo, color.GreenString("done"))
				}
			}

			return nil
		}
	}

	defer func() {
		_ = os.Chdir(wd)
	}()

	packageDir, err = getPackageRootDir(getRealRepoPath(repo))
	if err != nil {
		return errors.Trace(err)
	}

	err = os.Chdir(packageDir)
	if err != nil {
		return errors.Trace(err)
	}

	var s *spinner.Spinner
	if Verbose {
		s = spinner.New(spinner.CharSets[SpinnerCharSet], SpinnerInterval)
		s.Prefix = fmt.Sprintf("refreshing %s ", repo)
		s.Color("green")
		s.Start()
	}

	var refreshCommand []string

	if exists, _ := pathExists(path.Join(packageDir, ".git")); exists {
		refreshCommand = []string{"git", "fetch", "--all"}
	} else if exists, _ := pathExists(path.Join(packageDir, ".hg")); exists {
		refreshCommand = []string{"hg", "pull"}
	} else if exists, _ := pathExists(path.Join(packageDir, ".bzr")); exists {
		refreshCommand = []string{"bzr", "pull"}
	} else if exists, _ := pathExists(path.Join(packageDir, ".svn")); exists {
		refreshCommand = []string{"svn", "up"}
	}

	if len(refreshCommand) > 0 {
		refreshOutput, err := exec.Command(refreshCommand[0], refreshCommand[1:]...).CombinedOutput()

		if Verbose {
			s.Stop()
			fmt.Printf("\rrefreshing %s ... %s\n", repo, color.GreenString("done"))
		}

		if err != nil {
			return errors.Annotatef(err, "failed updating repo for package %s, output: %s", repo, refreshOutput)
		}
	} else {
		if Verbose {
			s.Stop()
			fmt.Printf("\rrefreshing %s ... %s\n", repo, color.YellowString("skipped"))
		}
	}

	return nil
}
コード例 #3
0
ファイル: packages.go プロジェクト: abutre/bunch
func setPackageVersion(repo string, version string, humanVersion string) error {
	if version == "" {
		return nil
	}

	wd, err := os.Getwd()
	if err != nil {
		return errors.Trace(err)
	}

	packageDir, err := getPackageRootDir(getRealRepoPath(repo))
	if err != nil {
		return errors.Trace(err)
	}

	defer func() {
		_ = os.Chdir(wd)
	}()

	err = os.Chdir(packageDir)
	if err != nil {
		return errors.Trace(err)
	}

	var checkoutCommand []string
	if exists, _ := pathExists(".git"); exists {
		checkoutCommand = []string{"git", "checkout", version}
	} else if exists, _ := pathExists(".hg"); exists {
		checkoutCommand = []string{"hg", "update", "-c", version}
	} else if exists, _ := pathExists(".bzr"); exists {
		if version != "" {
			checkoutCommand = []string{"bzr", "update", "-r", version}
		} else {
			checkoutCommand = []string{"bzr", "update"}
		}
	} else {
		if Verbose {
			fmt.Printf("  - setting version of %s to %s (resolved as %s) ... %s\n", repo, humanVersion, version, color.GreenString("skipped, unknown repo type"))
		}
		return nil
	}

	var s *spinner.Spinner

	if Verbose {
		s = spinner.New(spinner.CharSets[SpinnerCharSet], SpinnerInterval)
		s.Prefix = fmt.Sprintf("  - setting version of %s to %s (resolved as %s) ", repo, humanVersion, version)
		s.Color("green")
		s.Start()
	}

	checkoutOutput, err := exec.Command(checkoutCommand[0], checkoutCommand[1:]...).CombinedOutput()

	if Verbose {
		s.Stop()
		fmt.Printf("\r  - setting version of %s to %s (resolved as %s) ... %s\n", repo, humanVersion, version, color.GreenString("done"))
	}

	if err != nil {
		return errors.Annotatef(err, "failed setting version of package %s, error: %s, output: %s", repo, checkoutOutput)
	}

	return nil
}