Skip to content

Commit 9ee64e5

Browse files
committed
Return error in source_control.go
1 parent 20f7d73 commit 9ee64e5

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

cli/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ func main() {
356356
app.QueueUpdateDraw(func() {
357357
_, _ = outputView.Write([]byte("Repository creation failed: " + err.Error() + "\n"))
358358
})
359+
outputView.ScrollToEnd()
359360
continue
360361
}
361362
}
@@ -545,7 +546,11 @@ func main() {
545546
fmt.Println("Some arguments are missing for the cloneandpush command")
546547
os.Exit(1)
547548
}
548-
CloneAndPush(*sourcerepo, *sourceusername, *sourcepassword, *destrepo, *destusername, *destpassword, *hasWiki)
549+
err := CloneAndPush(*sourcerepo, *sourceusername, *sourcepassword, *destrepo, *destusername, *destpassword, *hasWiki)
550+
if err != nil {
551+
fmt.Fprintf(os.Stderr, "CloneAndPush failed: %v\n", err)
552+
os.Exit(1)
553+
}
549554

550555
default:
551556
fmt.Fprintln(os.Stderr, "Unknown command")

cli/source_control.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
)
1212

1313
// cloneRepository clones a repository to a local directory.
14-
func cloneRepository(repo, directory, username, password string) {
15-
var err error
14+
func cloneRepository(repo, directory, username, password string) error {
1615
cloneOptions := &git.CloneOptions{
1716
URL: repo,
1817
}
@@ -25,39 +24,35 @@ func cloneRepository(repo, directory, username, password string) {
2524
Password: password,
2625
}
2726
}
28-
_, err = git.PlainClone(directory, true, cloneOptions)
27+
_, err := git.PlainClone(directory, true, cloneOptions)
2928
if err != nil {
30-
fmt.Printf("Error cloning repository: %v\n", err)
31-
os.Exit(1)
29+
return fmt.Errorf("error cloning repository: %w", err)
3230
}
33-
fmt.Println("Repository cloned successfully.")
31+
return nil
3432
}
3533

3634
// addRemote adds a new remote to the local repository.
37-
func addRemote(directory, repo string) {
35+
func addRemote(directory, repo string) error {
3836
r, err := git.PlainOpen(directory)
3937
if err != nil {
40-
fmt.Printf("Error opening repository: %v\n", err)
41-
os.Exit(1)
38+
return fmt.Errorf("error opening repository: %w", err)
4239
}
4340

4441
_, err = r.CreateRemote(&config.RemoteConfig{
4542
Name: "origin2",
4643
URLs: []string{repo},
4744
})
4845
if err != nil {
49-
fmt.Printf("Error adding remote: %v\n", err)
50-
os.Exit(1)
46+
return fmt.Errorf("error adding remote: %w", err)
5147
}
52-
fmt.Println("Remote added successfully.")
48+
return nil
5349
}
5450

5551
// pushToRemote pushes the local repository to the specified remote.
56-
func pushToRemote(directory, username, password string) {
52+
func pushToRemote(directory, username, password string) error {
5753
r, err := git.PlainOpen(directory)
5854
if err != nil {
59-
fmt.Printf("Error opening repository: %v\n", err)
60-
os.Exit(1)
55+
return fmt.Errorf("error opening repository: %w", err)
6156
}
6257

6358
pushOptions := &git.PushOptions{
@@ -79,14 +74,13 @@ func pushToRemote(directory, username, password string) {
7974

8075
err = r.Push(pushOptions)
8176
if err != nil {
82-
fmt.Printf("Error pushing to remote: %v\n", err)
83-
os.Exit(1)
77+
return fmt.Errorf("error pushing to remote: %w", err)
8478
}
85-
fmt.Println("Pushed to remote successfully.")
79+
return nil
8680
}
8781

8882
// CloneAndPush clones a repository from the source URL and pushes it to the destination URL.
89-
func CloneAndPush(sourcerepo, sourceusername, sourcepassword, destrepo, destusername, destpassword string, hasWiki bool) {
83+
func CloneAndPush(sourcerepo, sourceusername, sourcepassword, destrepo, destusername, destpassword string, hasWiki bool) error {
9084
cnpdirectory := "temp.git"
9185

9286
defer func() {
@@ -96,9 +90,18 @@ func CloneAndPush(sourcerepo, sourceusername, sourcepassword, destrepo, destuser
9690
}
9791
}()
9892

99-
cloneRepository(sourcerepo, cnpdirectory, sourceusername, sourcepassword)
100-
addRemote(cnpdirectory, destrepo)
101-
pushToRemote(cnpdirectory, destusername, destpassword)
93+
if err := cloneRepository(sourcerepo, cnpdirectory, sourceusername, sourcepassword); err != nil {
94+
return fmt.Errorf("clone source repo: %w", err)
95+
}
96+
fmt.Println("Repository cloned successfully.")
97+
if err := addRemote(cnpdirectory, destrepo); err != nil {
98+
return fmt.Errorf("add destination remote: %w", err)
99+
}
100+
fmt.Println("Remote added successfully.")
101+
if err := pushToRemote(cnpdirectory, destusername, destpassword); err != nil {
102+
return fmt.Errorf("push to destination remote: %w", err)
103+
}
104+
fmt.Println("Pushed to remote successfully.")
102105

103106
if hasWiki {
104107
err := os.RemoveAll(cnpdirectory)
@@ -108,8 +111,19 @@ func CloneAndPush(sourcerepo, sourceusername, sourcepassword, destrepo, destuser
108111

109112
sourceWikiUrl := strings.Replace(sourcerepo, ".git", ".wiki.git", 1)
110113
destWikiUrl := strings.Replace(destrepo, ".git", ".wiki.git", 1)
111-
cloneRepository(sourceWikiUrl, cnpdirectory, sourceusername, sourcepassword)
112-
addRemote(cnpdirectory, destWikiUrl)
113-
pushToRemote(cnpdirectory, destusername, destpassword)
114+
if err := cloneRepository(sourceWikiUrl, cnpdirectory, sourceusername, sourcepassword); err != nil {
115+
return fmt.Errorf("clone source wiki: %w", err)
116+
}
117+
fmt.Println("Repository wiki cloned successfully.")
118+
if err := addRemote(cnpdirectory, destWikiUrl); err != nil {
119+
return fmt.Errorf("add wiki destination remote: %w", err)
120+
}
121+
fmt.Println("Remote wiki added successfully.")
122+
if err := pushToRemote(cnpdirectory, destusername, destpassword); err != nil {
123+
return fmt.Errorf("push wiki to destination remote: %w", err)
124+
}
125+
fmt.Println("Pushed wiki to remote successfully.")
114126
}
127+
128+
return nil
115129
}

0 commit comments

Comments
 (0)