From 5b3c92ee11cd3a7841a7f04bb75646db13a593ef Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply@gitea.io> Date: Fri, 8 Nov 2019 01:33:46 +0000 Subject: [PATCH] add repos subcommand [continue #44] (#3) (#65) --- .gitignore | 2 +- cmd/flags.go | 17 +++++++ cmd/repos.go | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 cmd/repos.go diff --git a/.gitignore b/.gitignore index ee4f52b..7133997 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tea .idea/ .history/ - +dist/ diff --git a/cmd/flags.go b/cmd/flags.go index a74f335..5d5d962 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -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 +} diff --git a/cmd/repos.go b/cmd/repos.go new file mode 100644 index 0000000..2ef04cb --- /dev/null +++ b/cmd/repos.go @@ -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", + } + 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 +} diff --git a/main.go b/main.go index 8cd481f..05c506f 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func main() { cmd.CmdIssues, cmd.CmdPulls, cmd.CmdReleases, + cmd.CmdRepos, cmd.CmdLabels, } app.EnableBashCompletion = true