add repos subcommand [continue #44] (#3) #65

Merged
lunny merged 23 commits from 6543/tea:repos_subcommand into master 2019-11-08 01:33:49 +00:00
4 changed files with 151 additions and 1 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
tea
.idea/
.history/
dist/

View File

@ -105,3 +105,20 @@ func initCommand() (*Login, string, string) {
owner, repo := splitRepo(repoPath)
return login, owner, repo
}
// initCommandLoginOnly return *Login based on flags
func initCommandLoginOnly() *Login {
err := loadConfig(yamlConfigPath)
if err != nil {
log.Fatal("load config file failed ", yamlConfigPath)
}
var login *Login
login = getLoginByName(loginValue)
if login == nil {
log.Fatal("indicated login name ", loginValue, " does not exist")
}
return login
}

132
cmd/repos.go Normal file
View File

@ -0,0 +1,132 @@
// Copyright 2019 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 cmd
import (
"log"
"code.gitea.io/sdk/gitea"
"github.com/urfave/cli"
)
// CmdRepos represents to login a gitea server.
var CmdRepos = cli.Command{
Name: "repos",
Usage: "Operate with repositories",
Description: `Operate with repositories`,
Action: runReposList,
Subcommands: []cli.Command{
CmdReposList,
},
Flags: LoginOutputFlags,
}
// CmdReposList represents a sub command of issues to list issues
var CmdReposList = cli.Command{
Name: "ls",
Usage: "List available repositories",
Description: `List available repositories`,
Action: runReposList,
Flags: append([]cli.Flag{
cli.StringFlag{
Name: "mode",
Usage: "Filter listed repositories based on mode, optional - fork, mirror, source",
},
cli.StringFlag{
Name: "org",
Usage: "Filter listed repositories based on organization, optional",
},
cli.StringFlag{
Name: "user",
Usage: "Filter listed repositories absed on user, optional",
},
}, LoginOutputFlags...),
}
// runReposList list repositories
func runReposList(ctx *cli.Context) error {
login := initCommandLoginOnly()
mode := ctx.String("mode")
org := ctx.String("org")
user := ctx.String("user")
var rps []*gitea.Repository
var err error
if org != "" {
rps, err = login.Client().ListOrgRepos(org)
} else if user != "" {
rps, err = login.Client().ListUserRepos(user)
} else {
rps, err = login.Client().ListMyRepos()
}
if err != nil {
log.Fatal(err)
}
var repos []*gitea.Repository
if mode == "" {
repos = rps
} else if mode == "fork" {
for _, rp := range rps {
if rp.Fork == true {
repos = append(repos, rp)
}
}
} else if mode == "mirror" {
for _, rp := range rps {
if rp.Mirror == true {
repos = append(repos, rp)
}
}
} else if mode == "source" {
for _, rp := range rps {
if rp.Mirror != true && rp.Fork != true {
repos = append(repos, rp)
}
}
} else {
log.Fatal("Unknown mode: ", mode, "\nUse one of the following:\n- fork\n- mirror\n- source\n")
return nil
}
if len(rps) == 0 {
log.Fatal("No repositories found", rps)
return nil
}
headers := []string{
"Name",
"Type",
"SSH",
"Owner",
Review

Maybe use simpler headers like "Type" or "URL" since those are used as yaml keys too.

Maybe use simpler headers like "Type" or "URL" since those are used as yaml keys too.
Review

Type is ok

about the SSH-URL, since i piced an old PR up to bring it upstream ... i realy have no idear why @root360-AndreasUlm add this colum. and URL is not the right way to describe it because it returns the ssh clone url - i change it to SSH

example output:

+------------------------------+--------+------------------------------------------------+-------------------+
|             NAME             |  TYPE  |                      SSH                       |       OWNER       |
+------------------------------+--------+------------------------------------------------+-------------------+
| 6543/go-sdk                  | fork   | git@gitea.com:6543/go-sdk.git                  |              6543 |
| 6543/GitNex                  | fork   | git@gitea.com:6543/GitNex.git                  |              6543 |
| 6543/markdownTest            | source | git@gitea.com:6543/markdownTest.git            |              6543 |
...

one thing I think about is to change "Name" values to repo names only because you get the whole "name" by combining Owner and Name ...

Type is ok about the SSH-URL, since i piced an old PR up to bring it upstream ... i realy have no idear why @root360-AndreasUlm add this colum. and URL is not the right way to describe it because it returns the ssh clone url - i change it to SSH example output: ``` +------------------------------+--------+------------------------------------------------+-------------------+ | NAME | TYPE | SSH | OWNER | +------------------------------+--------+------------------------------------------------+-------------------+ | 6543/go-sdk | fork | git@gitea.com:6543/go-sdk.git | 6543 | | 6543/GitNex | fork | git@gitea.com:6543/GitNex.git | 6543 | | 6543/markdownTest | source | git@gitea.com:6543/markdownTest.git | 6543 | ... ``` one thing I think about is to change "Name" values to repo names only because you get the whole "name" by combining Owner and Name ...

@6543:
The intention of the clone-URL was that you can cut the URL out of the table and directly use as parameter for git-clone.
The use-case for that is that you list all repository forks you own and clone those into a location.
Idea of sample command:
tea repos ls --mode fork --user my_user --output simple | cut -d' ' -f3 | xargs git clone

@6543: The intention of the clone-URL was that you can cut the URL out of the table and directly use as parameter for git-clone. The use-case for that is that you list all repository forks you own and clone those into a location. Idea of sample command: `tea repos ls --mode fork --user my_user --output simple | cut -d' ' -f3 | xargs git clone`
Review

It will stay as SSH

It will stay as SSH
}
var values [][]string
for _, rp := range repos {
var mode = "source"
if rp.Fork {
mode = "fork"
}
if rp.Mirror {
mode = "mirror"
}
values = append(
values,
[]string{
rp.FullName,
mode,
rp.SSHURL,
rp.Owner.UserName,
},
)
}
Output(outputValue, headers, values)
return nil
}

View File

@ -39,6 +39,7 @@ func main() {
cmd.CmdIssues,
cmd.CmdPulls,
cmd.CmdReleases,
cmd.CmdRepos,
cmd.CmdLabels,
}
app.EnableBashCompletion = true