-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (137 loc) · 3.76 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"errors"
"fmt"
"github.com/manifoldco/promptui"
"log"
"os"
"os/exec"
"strings"
)
func main() {
fmt.Println(os.Args)
// Convert SSH remote url to HTTPS
url := getRemoteUrl()
fmt.Println("url", url)
// Is the tag already?
tagName, err := checkTag(os.Args[1])
if err != nil {
log.Fatal(err)
}
fmt.Println("tag", tagName)
if len(tagName) == 1 {
fmt.Println("Tag name too short")
os.Exit(1)
}
// Ask for a tag message type?
var tagMessage string
askedMessage, err := askMessageType()
if err != nil {
log.Fatal(err)
}
if askedMessage == "Compare tags (default)" {
recentTag := getRecentTag()
fmt.Println("recentTag", recentTag)
tagMessage = url + "/compare/" + recentTag + "..." + tagName
} else {
tagMessage = askedMessage
}
fmt.Println("Message:", tagMessage)
// Create the Git tag
createTag(tagName, tagMessage)
// Push the tag to the remote
//pushTag(tagName, url)
}
func getRemoteUrl() string {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
out, err := cmd.Output()
if err != nil {
log.Fatal("Error getting remote URL", err)
}
remoteOriginUrl := strings.TrimSpace(string(out))
// TODO: Is the URL ssh or http?
//vcsType := strings.Split(remoteOriginUrl, "@")[0]
originUrl := strings.Split(remoteOriginUrl, "@")[1]
hostname := strings.Split(originUrl, ":")[0]
pathname := strings.Split(strings.Split(originUrl, ":")[1], ".")[0]
return "https://" + hostname + "/" + pathname
}
func createTag(tagName, tagMessage string) {
cmd := exec.Command("git", "tag", "-a", tagName, "-m", tagMessage)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Error creating tag: %v", err)
}
fmt.Printf("Tag %s created.\n", tagName)
}
func pushTag(tagName, remoteURL string) {
//pushTagToRemotePrompt := promptui.Select{
// Label: "Push tag remote",
// Items: []string{"yes", "no"},
//}
//
//_, pushTagToRemoteResult, err := pushTagToRemotePrompt.Run()
//if err != nil {
// log.Fatalf("Push tag to remote prompt failed %v\n", err)
//}
//if pushTagToRemoteResult == 'yes' {
cmd := exec.Command("git", "push", "origin", tagName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Error pushing tag to remote: %v", err)
}
fmt.Printf("Tag %s pushed to remote %s.\n", tagName, remoteURL)
//}
}
func checkTag(tagName string) (string, error) {
// get last tag
// git for-each-ref --sort=creatordate --format '%(refname:short)' --count=1
cmd := exec.Command("git", "describe", "--abbrev=0", "--tags", tagName)
isTagExists, _ := cmd.Output()
tag := strings.TrimSpace(string(isTagExists))
if tag == "" {
return tagName, nil
} else {
return "", errors.New("error: tag already exists")
}
}
func askMessageType() (string, error) {
/**
Q1: Ask for a tag message type
*/
messageTypePrompt := promptui.Select{
Label: "Select message type",
Items: []string{"Compare tags (default)", "Custom message"},
}
_, messageTypeResult, err := messageTypePrompt.Run()
if err != nil {
return "", fmt.Errorf("Select message type prompt failed %v\n", err)
}
/**
Q2: Prompt for a custom tag message
*/
if messageTypeResult == "Custom message" {
customMessagePrompt := promptui.Prompt{
Label: "Tag message",
Default: "",
}
customMessageResult, err := customMessagePrompt.Run()
if err != nil {
return "", fmt.Errorf("Enter custom message prompt failed %v\n", err)
}
return customMessageResult, nil
} else {
return messageTypeResult, nil
}
}
func getRecentTag() string {
// NOTE: If it is a new repository, should it get the oldest commit?
cmd := exec.Command("git", "describe", "--abbrev=0")
result, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return strings.TrimSpace(string(result))
}