mirror of
				https://gitea.com/gitea/tea.git
				synced 2025-10-30 16:55:25 +01: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:
		| @@ -5,7 +5,7 @@ | ||||
| package issues | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| @@ -34,7 +34,7 @@ func editIssueState(cmd *cli.Context, opts gitea.EditIssueOption) error { | ||||
| 	ctx := context.InitCommand(cmd) | ||||
| 	ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) | ||||
| 	if ctx.Args().Len() == 0 { | ||||
| 		log.Fatal(ctx.Command.ArgsUsage) | ||||
| 		return fmt.Errorf(ctx.Command.ArgsUsage) | ||||
| 	} | ||||
|  | ||||
| 	index, err := utils.ArgToIndex(ctx.Args().First()) | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package issues | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| 	"code.gitea.io/tea/modules/print" | ||||
| @@ -47,7 +45,7 @@ func RunIssuesList(cmd *cli.Context) error { | ||||
| 	}) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	print.IssuesList(issues, ctx.Output) | ||||
|   | ||||
| @@ -5,7 +5,7 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/labels" | ||||
| 	"github.com/urfave/cli/v2" | ||||
| @@ -34,6 +34,5 @@ func runLabels(ctx *cli.Context) error { | ||||
| } | ||||
|  | ||||
| func runLabelsDetails(ctx *cli.Context) error { | ||||
| 	log.Fatal("Not yet implemented.") | ||||
| 	return nil | ||||
| 	return fmt.Errorf("Not yet implemented") | ||||
| } | ||||
|   | ||||
| @@ -80,11 +80,7 @@ func runLabelCreate(cmd *cli.Context) error { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func splitLabelLine(line string) (string, string, string) { | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package labels | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
|  | ||||
| 	"github.com/urfave/cli/v2" | ||||
| @@ -31,9 +29,5 @@ func runLabelDelete(cmd *cli.Context) error { | ||||
| 	ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) | ||||
|  | ||||
| 	_, err := ctx.Login.Client().DeleteLabel(ctx.Owner, ctx.Repo, ctx.Int64("id")) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 	return err | ||||
| } | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package labels | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| 	"code.gitea.io/tea/modules/print" | ||||
| @@ -44,7 +42,7 @@ func RunLabelsList(cmd *cli.Context) error { | ||||
| 		ListOptions: ctx.GetListOptions(), | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if ctx.IsSet("save") { | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package labels | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
|  | ||||
| 	"code.gitea.io/sdk/gitea" | ||||
| @@ -68,7 +66,7 @@ func runLabelUpdate(cmd *cli.Context) error { | ||||
| 	}) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package login | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/config" | ||||
| 	"code.gitea.io/tea/modules/print" | ||||
| @@ -28,7 +26,7 @@ var CmdLoginList = cli.Command{ | ||||
| func RunLoginList(cmd *cli.Context) error { | ||||
| 	logins, err := config.GetLogins() | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
| 	print.LoginsList(logins, cmd.String("output")) | ||||
| 	return nil | ||||
|   | ||||
| @@ -6,7 +6,6 @@ package milestones | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| @@ -62,7 +61,7 @@ func runMilestonesCreate(cmd *cli.Context) error { | ||||
| 		State:       state, | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	print.MilestoneDetails(mile) | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package milestones | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| 	"code.gitea.io/tea/modules/print" | ||||
| @@ -53,7 +51,7 @@ func RunMilestonesList(cmd *cli.Context) error { | ||||
| 	}) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	print.MilestonesList(milestones, ctx.Output, state) | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| 	"code.gitea.io/tea/modules/print" | ||||
| @@ -76,7 +74,7 @@ func runNotifications(cmd *cli.Context) error { | ||||
| 		}) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	print.NotificationsList(news, ctx.Output, ctx.Bool("all")) | ||||
|   | ||||
							
								
								
									
										15
									
								
								cmd/open.go
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								cmd/open.go
									
									
									
									
									
								
							| @@ -5,7 +5,6 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"path" | ||||
| 	"strings" | ||||
|  | ||||
| @@ -42,12 +41,11 @@ func runOpen(cmd *cli.Context) error { | ||||
| 	case strings.EqualFold(number, "commits"): | ||||
| 		repo, err := local_git.RepoForWorkdir() | ||||
| 		if err != nil { | ||||
| 			log.Fatal(err) | ||||
| 			return err | ||||
| 		} | ||||
| 		b, err := repo.Head() | ||||
| 		if err != nil { | ||||
| 			log.Fatal(err) | ||||
| 			return nil | ||||
| 			return err | ||||
| 		} | ||||
| 		name := b.Name() | ||||
| 		switch { | ||||
| @@ -74,11 +72,6 @@ func runOpen(cmd *cli.Context) error { | ||||
| 		suffix = number | ||||
| 	} | ||||
|  | ||||
| 	u := path.Join(ctx.Login.URL, ctx.RepoSlug, suffix) | ||||
| 	err := open.Run(u) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 	u := path.Join(ctx.Login.URL, ctx.Owner, ctx.Repo, suffix) | ||||
| 	return open.Run(u) | ||||
| } | ||||
|   | ||||
| @@ -5,7 +5,7 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/organizations" | ||||
|  | ||||
| @@ -34,7 +34,5 @@ func runOrganizations(ctx *cli.Context) error { | ||||
| } | ||||
|  | ||||
| func runOrganizationDetail(path string) error { | ||||
|  | ||||
| 	log.Fatal("Not yet implemented.") | ||||
| 	return nil | ||||
| 	return fmt.Errorf("Not yet implemented") | ||||
| } | ||||
|   | ||||
| @@ -5,7 +5,7 @@ | ||||
| package organizations | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| 	"github.com/urfave/cli/v2" | ||||
| @@ -28,14 +28,12 @@ func RunOrganizationDelete(cmd *cli.Context) error { | ||||
| 	client := ctx.Login.Client() | ||||
|  | ||||
| 	if ctx.Args().Len() < 1 { | ||||
| 		log.Fatal("You have to specify the organization name you want to delete.") | ||||
| 		return nil | ||||
| 		return fmt.Errorf("You have to specify the organization name you want to delete") | ||||
| 	} | ||||
|  | ||||
| 	response, err := client.DeleteOrg(ctx.Args().First()) | ||||
| 	if response != nil && response.StatusCode == 404 { | ||||
| 		log.Fatal("The given organization does not exist.") | ||||
| 		return nil | ||||
| 		return fmt.Errorf("The given organization does not exist") | ||||
| 	} | ||||
|  | ||||
| 	return err | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package organizations | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| 	"code.gitea.io/tea/modules/print" | ||||
| @@ -37,7 +35,7 @@ func RunOrganizationList(cmd *cli.Context) error { | ||||
| 		ListOptions: ctx.GetListOptions(), | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	print.OrganizationsList(userOrganizations, ctx.Output) | ||||
|   | ||||
| @@ -5,7 +5,7 @@ | ||||
| package pulls | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| @@ -30,7 +30,7 @@ func runPullsCheckout(cmd *cli.Context) error { | ||||
| 	ctx := context.InitCommand(cmd) | ||||
| 	ctx.Ensure(context.CtxRequirement{LocalRepo: true}) | ||||
| 	if ctx.Args().Len() != 1 { | ||||
| 		log.Fatal("Must specify a PR index") | ||||
| 		return fmt.Errorf("Must specify a PR index") | ||||
| 	} | ||||
| 	idx, err := utils.ArgToIndex(ctx.Args().First()) | ||||
| 	if err != nil { | ||||
|   | ||||
| @@ -5,8 +5,6 @@ | ||||
| package pulls | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| 	"code.gitea.io/tea/modules/print" | ||||
| @@ -45,7 +43,7 @@ func RunPullsList(cmd *cli.Context) error { | ||||
| 	}) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	print.PullsList(prs, ctx.Output) | ||||
|   | ||||
| @@ -6,7 +6,6 @@ package releases | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| @@ -76,24 +75,23 @@ func runReleaseCreate(cmd *cli.Context) error { | ||||
|  | ||||
| 	if err != nil { | ||||
| 		if resp != nil && resp.StatusCode == http.StatusConflict { | ||||
| 			fmt.Println("error: There already is a release for this tag") | ||||
| 			return nil | ||||
| 			return fmt.Errorf("There already is a release for this tag") | ||||
| 		} | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	for _, asset := range ctx.StringSlice("asset") { | ||||
| 		var file *os.File | ||||
|  | ||||
| 		if file, err = os.Open(asset); err != nil { | ||||
| 			log.Fatal(err) | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		filePath := filepath.Base(asset) | ||||
|  | ||||
| 		if _, _, err = ctx.Login.Client().CreateReleaseAttachment(ctx.Owner, ctx.Repo, release.ID, file, filePath); err != nil { | ||||
| 			file.Close() | ||||
| 			log.Fatal(err) | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		file.Close() | ||||
|   | ||||
| @@ -6,7 +6,6 @@ package releases | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| @@ -38,7 +37,7 @@ func RunReleasesList(cmd *cli.Context) error { | ||||
| 		ListOptions: ctx.GetListOptions(), | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	print.ReleasesList(releases, ctx.Output) | ||||
|   | ||||
| @@ -5,7 +5,7 @@ | ||||
| package repos | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| @@ -67,7 +67,7 @@ func runReposSearch(cmd *cli.Context) error { | ||||
| 		if err != nil { | ||||
| 			// HACK: the client does not return a response on 404, so we can't check res.StatusCode | ||||
| 			if err.Error() != "404 Not Found" { | ||||
| 				log.Fatal("could not find owner: ", err) | ||||
| 				return fmt.Errorf("Could not find owner: %s", err) | ||||
| 			} | ||||
|  | ||||
| 			// if owner is no org, its a user | ||||
|   | ||||
| @@ -6,7 +6,6 @@ package times | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| @@ -41,20 +40,16 @@ func runTrackedTimesAdd(cmd *cli.Context) error { | ||||
|  | ||||
| 	issue, err := utils.ArgToIndex(ctx.Args().First()) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	duration, err := time.ParseDuration(strings.Join(ctx.Args().Tail(), "")) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	_, _, err = ctx.Login.Client().AddTime(ctx.Owner, ctx.Repo, issue, gitea.AddTimeOption{ | ||||
| 		Time: int64(duration.Seconds()), | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 	return err | ||||
| } | ||||
|   | ||||
| @@ -6,7 +6,6 @@ package times | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"strconv" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| @@ -37,18 +36,14 @@ func runTrackedTimesDelete(cmd *cli.Context) error { | ||||
|  | ||||
| 	issue, err := utils.ArgToIndex(ctx.Args().First()) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	timeID, err := strconv.ParseInt(ctx.Args().Get(1), 10, 64) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	_, err = client.DeleteTime(ctx.Owner, ctx.Repo, issue, timeID) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 	return err | ||||
| } | ||||
|   | ||||
| @@ -6,7 +6,6 @@ package times | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
|  | ||||
| 	"code.gitea.io/tea/cmd/flags" | ||||
| 	"code.gitea.io/tea/modules/context" | ||||
| @@ -36,13 +35,9 @@ func runTrackedTimesReset(cmd *cli.Context) error { | ||||
|  | ||||
| 	issue, err := utils.ArgToIndex(ctx.Args().First()) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	_, err = client.ResetIssueTime(ctx.Owner, ctx.Repo, issue) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 	return err | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Norwin
					Norwin