Skip to content

Commit 3bb9493

Browse files
refactoring: added better impl for retrieving repo language
1 parent 6d5035a commit 3bb9493

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
6060
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
6161

6262
COPY --from=builder /app /
63-
COPY templates /app/templates
6463

6564
# test if the below command avoids loading the files later.
6665
RUN go version

cmd/git-utils.go

+16-29
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ func CloneOrPullRepository(language string) error {
2626
}
2727
if exists {
2828
log.Debugf("directory %s exists.\n", repositoryPath)
29-
pullExistingRepository(repositoryPath)
30-
return nil
29+
return pullExistingRepository(repositoryPath)
3130
}
3231
log.Debugf("directory %s does not exist.\n", repositoryPath)
3332
log.Infof("cloning repository %s into %s\n", repoURL, repositoryPath)
34-
cloneNewRepository(repoURL, repositoryPath)
35-
return nil
33+
return cloneNewRepository(repoURL, repositoryPath)
3634
}
3735

3836
func getRepositoryURLByLanguage(language string) (string, string, error) {
@@ -41,50 +39,38 @@ func getRepositoryURLByLanguage(language string) (string, string, error) {
4139
return "", "", err
4240
}
4341
repositoryPath := userHomeDir + "/.compage/templates/compage-template-" + language
44-
if language == "go" {
45-
return "https://github.com/intelops/compage-template-go.git", repositoryPath, nil
46-
} else if language == "python" {
47-
return "https://github.com/intelops/compage-template-python.git", repositoryPath, nil
48-
} else if language == "java" {
49-
return "https://github.com/intelops/compage-template-java.git", repositoryPath, nil
50-
} else if language == "javascript" {
51-
return "https://github.com/intelops/compage-template-javascript.git", repositoryPath, nil
52-
} else if language == "ruby" {
53-
return "https://github.com/intelops/compage-template-ruby.git", repositoryPath, nil
54-
} else if language == "rust" {
55-
return "https://github.com/intelops/compage-template-rust.git", repositoryPath, nil
56-
} else if language == "typescript" {
57-
return "https://github.com/intelops/compage-template-typescript.git", repositoryPath, nil
58-
} else if language == "common" {
42+
repositoryURL := "https://github.com/intelops/compage-template-" + language + ".git"
43+
if language == "common" {
5944
repositoryPath = userHomeDir + "/.compage/templates/common-templates"
6045
return "https://github.com/intelops/common-templates.git", repositoryPath, nil
6146
}
62-
return "", "", nil
47+
return repositoryURL, repositoryPath, nil
6348
}
6449

65-
func cloneNewRepository(repoURL string, cloneDir string) {
50+
func cloneNewRepository(repoURL string, cloneDir string) error {
6651
_, err := git.PlainClone(cloneDir, false, &git.CloneOptions{
6752
URL: repoURL,
6853
Progress: os.Stdout,
6954
})
7055
if err != nil {
7156
log.Errorf("error:%v", err)
72-
return
57+
return err
7358
}
7459
log.Infof("repository[%s] cloned successfully.", repoURL)
60+
return nil
7561
}
7662

77-
func pullExistingRepository(existingRepositoryPath string) {
63+
func pullExistingRepository(existingRepositoryPath string) error {
7864
r, err := git.PlainOpen(existingRepositoryPath)
7965
if err != nil {
8066
log.Errorf("error:%v", err)
81-
return
67+
return err
8268
}
8369
// Get the working directory for the repository
8470
w, err := r.Worktree()
8571
if err != nil {
8672
log.Errorf("error:%v", err)
87-
return
73+
return err
8874
}
8975
// Pull the latest changes from the origin remote and merge into the current branch
9076
log.Info("git pull origin")
@@ -93,21 +79,22 @@ func pullExistingRepository(existingRepositoryPath string) {
9379
if errors.Is(err, git.NoErrAlreadyUpToDate) {
9480
// This is a special error that means we don't have any new changes
9581
log.Info(err)
96-
return
82+
return nil
9783
}
9884
log.Errorf("error:%v", err)
99-
return
85+
return err
10086
}
10187
// Print the latest commit that was just pulled
10288
ref, err := r.Head()
10389
if err != nil {
10490
log.Errorf("error:%v", err)
105-
return
91+
return err
10692
}
10793
commit, err := r.CommitObject(ref.Hash())
10894
if err != nil {
10995
log.Errorf("error:%v", err)
110-
return
96+
return err
11197
}
11298
log.Info(commit)
99+
return nil
113100
}

0 commit comments

Comments
 (0)