From 15c4edba1a6ab19353519efd015b6fa2661cdb4f Mon Sep 17 00:00:00 2001 From: Norwin Date: Fri, 5 Mar 2021 16:56:15 +0800 Subject: [PATCH] Don't exit if we can't find a local repo with a remote matching to a login (#336) This enables to run commands that need minimal context (i.e. `tea n --all`) to run anywhere. fixes #329 Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/336 Reviewed-by: Lunny Xiao Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Norwin Co-committed-by: Norwin --- modules/context/context.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/modules/context/context.go b/modules/context/context.go index 80c8c92..909c4d5 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -20,15 +20,19 @@ import ( "github.com/urfave/cli/v2" ) +var ( + errNotAGiteaRepo = errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository") +) + // TeaContext contains all context derived during command initialization and wraps cli.Context type TeaContext struct { *cli.Context - Login *config.Login - RepoSlug string // / - Owner string // repo owner as derived from context - Repo string // repo name as derived from context or provided in flag - Output string // value of output flag - LocalRepo *git.TeaRepo // maybe, we have opened it already anyway + Login *config.Login // config data & client for selected login + RepoSlug string // /, optional + Owner string // repo owner as derived from context or provided in flag, optional + Repo string // repo name as derived from context or provided in flag, optional + Output string // value of output flag + LocalRepo *git.TeaRepo // is set if flags specified a local repo via --repo, or if $PWD is a git repo } // GetListOptions return ListOptions based on PaginationFlags @@ -84,8 +88,7 @@ func InitCommand(ctx *cli.Context) *TeaContext { // check if repoFlag can be interpreted as path to local repo. if len(repoFlag) != 0 { - repoFlagPathExists, err = utils.DirExists(repoFlag) - if err != nil { + if repoFlagPathExists, err = utils.DirExists(repoFlag); err != nil { log.Fatal(err.Error()) } if repoFlagPathExists { @@ -95,9 +98,12 @@ func InitCommand(ctx *cli.Context) *TeaContext { if len(repoFlag) == 0 || repoFlagPathExists { // try to read git repo & extract context, ignoring if PWD is not a repo - c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag) - if err != nil && err != gogit.ErrRepositoryNotExists { - log.Fatal(err.Error()) + if c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag); err != nil { + if err == errNotAGiteaRepo || err == gogit.ErrRepositoryNotExists { + // we can deal with that, commands needing the optional values use ctx.Ensure() + } else { + log.Fatal(err.Error()) + } } } @@ -187,5 +193,5 @@ func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.L } } - return repo, nil, "", errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository") + return repo, nil, "", errNotAGiteaRepo }