From 2d1907ea9c1941c1325130a776656b73d242b6ed Mon Sep 17 00:00:00 2001 From: Andreas Ulm Date: Mon, 6 May 2019 18:56:14 +0200 Subject: [PATCH] added subcommand ls & filter flags Signed-off-by: Andreas Ulm --- cmd/repos.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/cmd/repos.go b/cmd/repos.go index 08c0fa2..41ee698 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -8,6 +8,8 @@ import ( "fmt" "log" + "code.gitea.io/sdk/gitea" + "github.com/urfave/cli" ) @@ -16,7 +18,10 @@ var CmdRepos = cli.Command{ Name: "repos", Usage: "Operate with repositories", Description: `Operate with repositories`, - Action: runRepos, + Action: runReposList, + Subcommands: []cli.Command{ + CmdReposList, + }, Flags: []cli.Flag{ cli.StringFlag{ Name: "login, l", @@ -25,22 +30,87 @@ var CmdRepos = cli.Command{ }, } -func runRepos(ctx *cli.Context) error { +// 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: []cli.Flag{ + cli.StringFlag{ + Name: "login, l", + Usage: "Indicate one login, optional when inside a gitea repository", + }, + cli.StringFlag{ + Name: "mode", + Usage: "Indicate one login, optional when inside a gitea repository", + }, + cli.StringFlag{ + Name: "org", + Usage: "Indicate one login, optional when inside a gitea repository", + }, + cli.StringFlag{ + Name: "user", + Usage: "Indicate one login, optional when inside a gitea repository", + }, + }, +} + +// runReposList list repositories +func runReposList(ctx *cli.Context) error { login := initCommandLoginOnly(ctx) - rps, err := login.Client().ListMyRepos() + 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 { + fmt.Printf("Unknown mode '%s'\nUse one of the following:\n- fork\n- mirror\n- source\n", mode) + return nil + } + if len(rps) == 0 { fmt.Println("No repositories found") return nil } fmt.Println("Name | Type/Mode | SSH-URL | Owner") - for _, rp := range rps { + for _, rp := range repos { var mode = "source" if rp.Fork { mode = "fork"