mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-02 01:48:30 +02:00
Refactor error handling (#308)
use fmt instead of log log.Fatal -> return err set non-zero exit code on error print to default err log cleanup fix vet Co-authored-by: Norwin Roosen <git@nroo.de> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/308 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: 6543 <6543@obermui.de> Co-Authored-By: Norwin <noerw@noreply.gitea.io> Co-Committed-By: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
@ -6,7 +6,6 @@ package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"code.gitea.io/tea/modules/config"
|
||||
@ -34,12 +33,12 @@ func CreateIssue(login *config.Login, repoOwner, repoName, title, description st
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("could not create issue: %s", err)
|
||||
return fmt.Errorf("could not create issue: %s", err)
|
||||
}
|
||||
|
||||
print.IssueDetails(issue)
|
||||
|
||||
fmt.Println(issue.HTMLURL)
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
@ -16,7 +15,7 @@ import (
|
||||
func LabelsExport(labels []*gitea.Label, path string) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
|
@ -6,7 +6,6 @@ package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@ -21,7 +20,7 @@ func CreateLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bo
|
||||
// checks ...
|
||||
// ... if we have a url
|
||||
if len(giteaURL) == 0 {
|
||||
log.Fatal("You have to input Gitea server URL")
|
||||
return fmt.Errorf("You have to input Gitea server URL")
|
||||
}
|
||||
|
||||
// ... if there already exist a login with same name
|
||||
@ -35,17 +34,17 @@ func CreateLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bo
|
||||
|
||||
// .. if we have enough information to authenticate
|
||||
if len(token) == 0 && (len(user)+len(passwd)) == 0 {
|
||||
log.Fatal("No token set")
|
||||
return fmt.Errorf("No token set")
|
||||
} else if len(user) != 0 && len(passwd) == 0 {
|
||||
log.Fatal("No password set")
|
||||
return fmt.Errorf("No password set")
|
||||
} else if len(user) == 0 && len(passwd) != 0 {
|
||||
log.Fatal("No user set")
|
||||
return fmt.Errorf("No user set")
|
||||
}
|
||||
|
||||
// Normalize URL
|
||||
serverURL, err := utils.NormalizeURL(giteaURL)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to parse URL", err)
|
||||
return fmt.Errorf("Unable to parse URL: %s", err)
|
||||
}
|
||||
|
||||
login := config.Login{
|
||||
@ -60,23 +59,21 @@ func CreateLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bo
|
||||
client := login.Client()
|
||||
|
||||
if len(token) == 0 {
|
||||
login.Token, err = generateToken(client, user, passwd)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
if login.Token, err = generateToken(client, user, passwd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Verify if authentication works and get user info
|
||||
u, _, err := client.GetMyUserInfo()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
login.User = u.UserName
|
||||
|
||||
if len(login.Name) == 0 {
|
||||
login.Name, err = GenerateLoginName(giteaURL, login.User)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
if login.Name, err = GenerateLoginName(giteaURL, login.User); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,9 +88,8 @@ func CreateLogin(name, token, user, passwd, sshKey, giteaURL string, insecure bo
|
||||
}
|
||||
}
|
||||
|
||||
err = config.AddLogin(&login)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
if err = config.AddLogin(&login); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Login as %s on %s successful. Added this login as %s\n", login.User, login.URL, login.Name)
|
||||
|
@ -6,7 +6,6 @@ package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
@ -24,14 +23,14 @@ func CreatePull(login *config.Login, repoOwner, repoName, base, head, title, des
|
||||
// open local git repo
|
||||
localRepo, err := local_git.RepoForWorkdir()
|
||||
if err != nil {
|
||||
log.Fatal("could not open local repo: ", err)
|
||||
return fmt.Errorf("Could not open local repo: %s", err)
|
||||
}
|
||||
|
||||
// push if possible
|
||||
log.Println("git push")
|
||||
fmt.Println("git push")
|
||||
err = localRepo.Push(&git.PushOptions{})
|
||||
if err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
log.Printf("Error occurred during 'git push':\n%s\n", err.Error())
|
||||
fmt.Printf("Error occurred during 'git push':\n%s\n", err.Error())
|
||||
}
|
||||
|
||||
// default is default branch
|
||||
@ -74,7 +73,7 @@ func CreatePull(login *config.Login, repoOwner, repoName, base, head, title, des
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("could not create PR from %s to %s:%s: %s", head, repoOwner, base, err)
|
||||
return fmt.Errorf("Could not create PR from %s to %s:%s: %s", head, repoOwner, base, err)
|
||||
}
|
||||
|
||||
print.PullDetails(pr, nil)
|
||||
|
Reference in New Issue
Block a user