func downloadTasksFromGitHub()

in prepare.go [33:109]


func downloadTasksFromGitHub(force bool, silent bool) (string, error) {
	debug("Download tasks from github")
	repoURL := getOpsRepo()
	branch := getOpsBranch()
	opsDir, err := homedir.Expand("~/.ops")
	if err != nil {
		return "", err
	}
	if err := os.MkdirAll(opsDir, 0755); err != nil {
		return "", err
	}

	opsBranchDir := joinpath(opsDir, branch)
	localDir, err := homedir.Expand(joinpath(opsBranchDir, "olaris"))
	if err != nil {
		return "", err
	}
	debug("localDir", localDir)

	// Updating existing tools
	if exists(opsBranchDir, "olaris") {
		trace("Updating olaris in", opsBranchDir)
		fmt.Println("Updating tasks...")
		r, err := git.PlainOpen(localDir)
		if err != nil {
			return "", err
		}
		// Get the working directory for the repository
		w, err := r.Worktree()
		if err != nil {
			return "", err
		}

		// Pull the latest changes from the origin remote and merge into the current branch
		// Clone the repo if not existing
		ref := plumbing.NewBranchReferenceName(branch)
		err = w.Pull(&git.PullOptions{
			RemoteName:    "origin",
			ReferenceName: ref,
			SingleBranch:  true,
		})
		if err != nil {
			if err.Error() == "already up-to-date" {
				fmt.Println("Tasks are already up to date!")
				return localDir, nil
			}
			return "", err
		}

		fmt.Println("Tasks updated successfully")
		touchLatestCheckFile(joinpath(opsBranchDir, LATESTCHECK))
		return localDir, nil
	}

	// Clone the repo if not existing
	ref := plumbing.NewBranchReferenceName(branch)
	cloneOpts := &git.CloneOptions{
		URL:           repoURL,
		Progress:      os.Stderr,
		ReferenceName: ref, // Specify the branch to clone
	}

	fmt.Println("Cloning tasks...")
	_, err = git.PlainClone(localDir, false, cloneOpts)
	if err != nil {
		os.RemoveAll(opsBranchDir)
		warn(fmt.Sprintf("failed to clone olaris on branch '%s'", branch))
		return "", err
	}

	fmt.Println("Tasks downloaded successfully")

	createLatestCheckFile(opsBranchDir)

	// clone
	return localDir, nil
}