From 3c1bcdb1e252bdd58decae3d756901ffbf40c894 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Wed, 16 Sep 2020 13:47:52 +0000 Subject: [PATCH] More Options To Specify Repo (#178) use active user as owner by default Fix #163 Part1 Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/178 Reviewed-by: Lunny Xiao Reviewed-by: mrsdizzie --- cmd/config.go | 7 +++++-- cmd/flags.go | 21 +++++++++++++++++++-- modules/utils/path.go | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 modules/utils/path.go diff --git a/cmd/config.go b/cmd/config.go index 3f1a625..6e27042 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -100,12 +100,15 @@ func init() { yamlConfigPath = filepath.Join(dir, "tea.yml") } -func splitRepo(repoPath string) (string, string) { +func getOwnerAndRepo(repoPath, user string) (string, string) { + if len(repoPath) == 0 { + return "", "" + } p := strings.Split(repoPath, "/") if len(p) >= 2 { return p[0], p[1] } - return repoPath, "" + return user, repoPath } func getActiveLogin() (*Login, error) { diff --git a/cmd/flags.go b/cmd/flags.go index c503e56..b54fc87 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -7,6 +7,8 @@ package cmd import ( "log" + "code.gitea.io/tea/modules/utils" + "github.com/urfave/cli/v2" ) @@ -81,16 +83,31 @@ var AllDefaultFlags = append([]cli.Flag{ // initCommand returns repository and *Login based on flags func initCommand() (*Login, string, string) { + var ( + login *Login + repoPath string + ) err := loadConfig(yamlConfigPath) if err != nil { log.Fatal("load config file failed ", yamlConfigPath) } - login, repoPath, err := curGitRepoPath(repoValue) + if login, err = getActiveLogin(); err != nil { + log.Fatal(err.Error()) + } + + exist, err := utils.PathExists(repoValue) if err != nil { log.Fatal(err.Error()) } + if exist || len(repoValue) == 0 { + login, repoPath, err = curGitRepoPath(repoValue) + if err != nil { + log.Fatal(err.Error()) + } + } + if loginValue != "" { login = getLoginByName(loginValue) if login == nil { @@ -98,7 +115,7 @@ func initCommand() (*Login, string, string) { } } - owner, repo := splitRepo(repoPath) + owner, repo := getOwnerAndRepo(repoPath, login.User) return login, owner, repo } diff --git a/modules/utils/path.go b/modules/utils/path.go new file mode 100644 index 0000000..2253256 --- /dev/null +++ b/modules/utils/path.go @@ -0,0 +1,21 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package utils + +import ( + "os" +) + +// PathExists returns whether the given file or directory exists or not +func PathExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return true, err +}