From 5bd2eee592e83c67c68adac13e3ef82d573b1d63 Mon Sep 17 00:00:00 2001 From: Andreas Ulm Date: Mon, 15 Apr 2019 22:40:27 +0200 Subject: [PATCH 1/4] initial imlementation of list repos (#3) Signed-off-by: Andreas Ulm --- cmd/repos.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 85 insertions(+) create mode 100644 cmd/repos.go diff --git a/cmd/repos.go b/cmd/repos.go new file mode 100644 index 0000000..f0fec30 --- /dev/null +++ b/cmd/repos.go @@ -0,0 +1,84 @@ +// Copyright 2018 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 ( + "fmt" + "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: runRepos, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "login, l", + Usage: "Indicate one login, optional when inside a gitea repository", + }, + cli.BoolFlag{ + Name: "noheader", + Usage: "Skip header on output.", + }, + }, +} + +func runRepos(ctx *cli.Context) error { + login := initCommandLoginOnly(ctx) + + rps, err := login.Client().ListMyRepos() + + if err != nil { + log.Fatal(err) + } + + if len(rps) == 0 { + fmt.Println("No repositories found") + return nil + } + + if noheader := getGlobalFlag(ctx, "noheader"); noheader != "true" { + fmt.Printf("Name\tType/Mode\tSSH-URL\tOwner\n") + } + for _, rp := range rps { + var mode = "source" + if rp.Fork { + mode = "fork" + } + if rp.Mirror { + mode = "mirror" + } + fmt.Printf("%s\t%s\t%s\t%s\n", rp.FullName, mode, rp.SSHURL, rp.Owner.UserName) + } + + return nil +} + +func initCommandLoginOnly(ctx *cli.Context) (*Login) { + err := loadConfig(yamlConfigPath) + if err != nil { + log.Fatal("load config file failed", yamlConfigPath) + } + + var login *Login + if loginFlag := getGlobalFlag(ctx, "login"); loginFlag == "" { + login, err = getActiveLogin() + if err != nil { + log.Fatal(err) + } + } else { + login = getLoginByName(loginFlag) + if login == nil { + log.Fatal("indicated login name", loginFlag, "does not exist") + } + } + return login +} diff --git a/main.go b/main.go index e5b0a42..a67c6f4 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func main() { cmd.CmdIssues, cmd.CmdPulls, cmd.CmdReleases, + cmd.CmdRepos, } err := app.Run(os.Args) if err != nil { From 2599cff8a63263bbf7d7c25595ac19d4cf5e9659 Mon Sep 17 00:00:00 2001 From: Andreas Ulm Date: Mon, 15 Apr 2019 22:58:32 +0200 Subject: [PATCH 2/4] improved code style Signed-off-by: Andreas Ulm --- cmd/repos.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/repos.go b/cmd/repos.go index f0fec30..a50f29d 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Gitea Authors. All rights reserved. +// 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. @@ -8,8 +8,6 @@ import ( "fmt" "log" - //"code.gitea.io/sdk/gitea" - "github.com/urfave/cli" ) From b96a190c7485318ba709e62ac20b7c703f7374f5 Mon Sep 17 00:00:00 2001 From: Andreas Ulm Date: Thu, 25 Apr 2019 15:59:41 +0200 Subject: [PATCH 3/4] removed header & changed delimiter to pipe Signed-off-by: Andreas Ulm --- cmd/repos.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/cmd/repos.go b/cmd/repos.go index a50f29d..956450f 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -22,10 +22,6 @@ var CmdRepos = cli.Command{ Name: "login, l", Usage: "Indicate one login, optional when inside a gitea repository", }, - cli.BoolFlag{ - Name: "noheader", - Usage: "Skip header on output.", - }, }, } @@ -43,9 +39,7 @@ func runRepos(ctx *cli.Context) error { return nil } - if noheader := getGlobalFlag(ctx, "noheader"); noheader != "true" { - fmt.Printf("Name\tType/Mode\tSSH-URL\tOwner\n") - } + fmt.Println("Name | Type/Mode | SSH-URL | Owner") for _, rp := range rps { var mode = "source" if rp.Fork { @@ -54,7 +48,7 @@ func runRepos(ctx *cli.Context) error { if rp.Mirror { mode = "mirror" } - fmt.Printf("%s\t%s\t%s\t%s\n", rp.FullName, mode, rp.SSHURL, rp.Owner.UserName) + fmt.Printf("%s | %s | %s | %s\n", rp.FullName, mode, rp.SSHURL, rp.Owner.UserName) } return nil From 9021a0e643e6bd39c737cb2b8416fab463606565 Mon Sep 17 00:00:00 2001 From: Andreas Ulm Date: Sun, 28 Apr 2019 14:27:33 +0200 Subject: [PATCH 4/4] ran 'make fmt' Signed-off-by: Andreas Ulm --- cmd/repos.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/repos.go b/cmd/repos.go index 956450f..08c0fa2 100644 --- a/cmd/repos.go +++ b/cmd/repos.go @@ -54,7 +54,7 @@ func runRepos(ctx *cli.Context) error { return nil } -func initCommandLoginOnly(ctx *cli.Context) (*Login) { +func initCommandLoginOnly(ctx *cli.Context) *Login { err := loadConfig(yamlConfigPath) if err != nil { log.Fatal("load config file failed", yamlConfigPath)