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 <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/336
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:
Norwin 2021-03-05 16:56:15 +08:00 committed by 6543
parent e96cfdbbe7
commit 15c4edba1a
1 changed files with 18 additions and 12 deletions

View File

@ -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>/<repo>
Owner string // repo owner as derived from context
Repo string // repo name as derived from context or provided in flag
Login *config.Login // config data & client for selected login
RepoSlug string // <owner>/<repo>, 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 // maybe, we have opened it already anyway
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,11 +98,14 @@ 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 {
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())
}
}
}
if len(repoFlag) != 0 && !repoFlagPathExists {
// if repoFlag is not a valid path, use it to override repoSlug
@ -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
}