From 846fb3072a635dddebecb575eee6d66d373acc42 Mon Sep 17 00:00:00 2001 From: khmarbaise Date: Wed, 9 Dec 2020 03:06:15 +0800 Subject: [PATCH] Refactor tea labels command (#282) Refactor tea labels command Fix #278 Signed-off-by: Karl Heinz Marbaise Refactor tea labels command - fixed formatting code. Co-authored-by: Karl Heinz Marbaise Reviewed-on: https://gitea.com/gitea/tea/pulls/282 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Norwin Co-Authored-By: khmarbaise Co-Committed-By: khmarbaise --- cmd/labels.go | 32 +++++++--------------------- cmd/labels/list.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 cmd/labels/list.go diff --git a/cmd/labels.go b/cmd/labels.go index 468d7f4..ed953f8 100644 --- a/cmd/labels.go +++ b/cmd/labels.go @@ -7,13 +7,7 @@ package cmd import ( "log" - "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/cmd/labels" - "code.gitea.io/tea/modules/config" - "code.gitea.io/tea/modules/print" - "code.gitea.io/tea/modules/task" - - "code.gitea.io/sdk/gitea" "github.com/urfave/cli/v2" ) @@ -25,33 +19,21 @@ var CmdLabels = cli.Command{ Description: `Manage issue labels`, Action: runLabels, Subcommands: []*cli.Command{ + &labels.CmdLabelsList, &labels.CmdLabelCreate, &labels.CmdLabelUpdate, &labels.CmdLabelDelete, }, - Flags: append([]cli.Flag{ - &cli.StringFlag{ - Name: "save", - Aliases: []string{"s"}, - Usage: "Save all the labels as a file", - }, - &flags.PaginationPageFlag, - &flags.PaginationLimitFlag, - }, flags.AllDefaultFlags...), } func runLabels(ctx *cli.Context) error { - login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue) - - labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: flags.GetListOptions(ctx)}) - if err != nil { - log.Fatal(err) + if ctx.Args().Len() == 1 { + return runLabelsDetails(ctx) } + return labels.RunLabelsList(ctx) +} - if ctx.IsSet("save") { - return task.LabelsExport(labels, ctx.String("save")) - } - - print.LabelsList(labels, flags.GlobalOutputValue) +func runLabelsDetails(ctx *cli.Context) error { + log.Fatal("Not yet implemented.") return nil } diff --git a/cmd/labels/list.go b/cmd/labels/list.go new file mode 100644 index 0000000..df7891e --- /dev/null +++ b/cmd/labels/list.go @@ -0,0 +1,52 @@ +// 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 labels + +import ( + "log" + + "code.gitea.io/tea/cmd/flags" + "code.gitea.io/tea/modules/config" + "code.gitea.io/tea/modules/print" + "code.gitea.io/tea/modules/task" + + "code.gitea.io/sdk/gitea" + "github.com/urfave/cli/v2" +) + +// CmdLabelsList represents a sub command of labels to list labels +var CmdLabelsList = cli.Command{ + Name: "ls", + Aliases: []string{"list"}, + Usage: "List labels", + Description: "List labels", + Action: RunLabelsList, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "save", + Aliases: []string{"s"}, + Usage: "Save all the labels as a file", + }, + &flags.PaginationPageFlag, + &flags.PaginationLimitFlag, + }, flags.AllDefaultFlags...), +} + +// RunLabelsList list labels. +func RunLabelsList(ctx *cli.Context) error { + login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue) + + labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: flags.GetListOptions(ctx)}) + if err != nil { + log.Fatal(err) + } + + if ctx.IsSet("save") { + return task.LabelsExport(labels, ctx.String("save")) + } + + print.LabelsList(labels, flags.GlobalOutputValue) + return nil +}