|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/go-git/go-git/v5" |
| 6 | + "github.com/intelops/compage/internal/utils" |
| 7 | + log "github.com/sirupsen/logrus" |
| 8 | + "os" |
| 9 | +) |
| 10 | + |
| 11 | +func CloneOrPullRepository(language string) error { |
| 12 | + repoURL, repositoryPath, err := getRepositoryURLByLanguage(language) |
| 13 | + if err != nil { |
| 14 | + log.Errorf("error:%v", err) |
| 15 | + return err |
| 16 | + } |
| 17 | + if repoURL == "" || repositoryPath == "" { |
| 18 | + log.Errorf("language %s not supported", language) |
| 19 | + return errors.New("language not supported") |
| 20 | + } |
| 21 | + |
| 22 | + exists, err := utils.DirectoryExists(repositoryPath) |
| 23 | + if err != nil { |
| 24 | + log.Errorf("error checking directory existence: %v", err) |
| 25 | + return err |
| 26 | + } |
| 27 | + if exists { |
| 28 | + log.Debugf("directory %s exists.\n", repositoryPath) |
| 29 | + return pullExistingRepository(repositoryPath) |
| 30 | + } |
| 31 | + log.Debugf("directory %s does not exist.\n", repositoryPath) |
| 32 | + log.Infof("cloning repository %s into %s\n", repoURL, repositoryPath) |
| 33 | + return cloneNewRepository(repoURL, repositoryPath) |
| 34 | +} |
| 35 | + |
| 36 | +func getRepositoryURLByLanguage(language string) (string, string, error) { |
| 37 | + userHomeDir, err := os.UserHomeDir() |
| 38 | + if err != nil { |
| 39 | + return "", "", err |
| 40 | + } |
| 41 | + repositoryPath := userHomeDir + "/.compage/templates/compage-template-" + language |
| 42 | + repositoryURL := "https://github.com/intelops/compage-template-" + language + ".git" |
| 43 | + if language == "common" { |
| 44 | + repositoryPath = userHomeDir + "/.compage/templates/common-templates" |
| 45 | + return "https://github.com/intelops/common-templates.git", repositoryPath, nil |
| 46 | + } |
| 47 | + return repositoryURL, repositoryPath, nil |
| 48 | +} |
| 49 | + |
| 50 | +func cloneNewRepository(repoURL string, cloneDir string) error { |
| 51 | + _, err := git.PlainClone(cloneDir, false, &git.CloneOptions{ |
| 52 | + URL: repoURL, |
| 53 | + Progress: os.Stdout, |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + log.Errorf("error:%v", err) |
| 57 | + return err |
| 58 | + } |
| 59 | + log.Infof("repository[%s] cloned successfully.", repoURL) |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func pullExistingRepository(existingRepositoryPath string) error { |
| 64 | + r, err := git.PlainOpen(existingRepositoryPath) |
| 65 | + if err != nil { |
| 66 | + log.Errorf("error:%v", err) |
| 67 | + return err |
| 68 | + } |
| 69 | + // Get the working directory for the repository |
| 70 | + w, err := r.Worktree() |
| 71 | + if err != nil { |
| 72 | + log.Errorf("error:%v", err) |
| 73 | + return err |
| 74 | + } |
| 75 | + // Pull the latest changes from the origin remote and merge into the current branch |
| 76 | + log.Info("git pull origin") |
| 77 | + err = w.Pull(&git.PullOptions{RemoteName: "origin"}) |
| 78 | + if err != nil { |
| 79 | + if errors.Is(err, git.NoErrAlreadyUpToDate) { |
| 80 | + // This is a special error that means we don't have any new changes |
| 81 | + log.Info(err) |
| 82 | + return nil |
| 83 | + } |
| 84 | + log.Errorf("error:%v", err) |
| 85 | + return err |
| 86 | + } |
| 87 | + // Print the latest commit that was just pulled |
| 88 | + ref, err := r.Head() |
| 89 | + if err != nil { |
| 90 | + log.Errorf("error:%v", err) |
| 91 | + return err |
| 92 | + } |
| 93 | + commit, err := r.CommitObject(ref.Hash()) |
| 94 | + if err != nil { |
| 95 | + log.Errorf("error:%v", err) |
| 96 | + return err |
| 97 | + } |
| 98 | + log.Info(commit) |
| 99 | + return nil |
| 100 | +} |
0 commit comments