-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
76 lines (57 loc) · 1.71 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"strings"
)
func addRepo(config Config, gitUrl string) {
clonePath := getClonePath(gitUrl, config)
log := NewLogUtil()
if directoryExists(clonePath) {
log.Negative("Directory already exists!", nil)
return
}
log.Positive("Cloning repository...")
if err := git("clone", gitUrl, clonePath); err != nil {
log.Negative("Error cloning repository:", err)
return
}
log.Positive("Repository cloned successfully")
}
func getClonePath(gitUrl string, config Config) string {
log := NewLogUtil()
if !isValidGitUrl(gitUrl) {
log.Negative("Invalid Git URL", nil)
return ""
}
username, repoName, err := parseGitUrl(gitUrl)
if err != nil {
log.Negative("Error parsing Git URL", err)
}
usernameDir := fmt.Sprintf("%s/%s", config.ReposBasePath, username)
// Check if the repository for the user exists
if directoryExists(usernameDir) {
log.Positive(fmt.Sprintf("Directory for GitHub user '%s' found!\nAdding new repository '%s'.", username, repoName))
} else {
log.Positive(fmt.Sprintf("Directory for GitHub user '%s' not found!\nCreating directory.", username))
}
clonePath := fmt.Sprintf("%s/%s/%s", config.ReposBasePath, username, repoName)
return clonePath
}
func parseGitUrl(gitUrl string) (string, string, error) {
gitUrl = strings.TrimSuffix(gitUrl, ".git")
var parts []string
if isSshUrl(gitUrl) {
parts = strings.Split(gitUrl, ":")
parts = strings.Split(parts[1], "/")
} else if isHttpUrl(gitUrl) {
parts = strings.Split(gitUrl, "/")
} else {
return "", "", fmt.Errorf("invalid Git Url")
}
if len(parts) < 2 {
return "", "", fmt.Errorf("invalid Git Url")
}
username := parts[len(parts)-2]
repoName := parts[len(parts)-1]
return username, repoName, nil
}