2019-10-19 10:54:16 +00:00
|
|
|
// 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 (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2019-10-26 04:50:30 +00:00
|
|
|
"strconv"
|
2019-10-19 10:54:16 +00:00
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
"code.gitea.io/tea/cmd/flags"
|
|
|
|
"code.gitea.io/tea/cmd/labels"
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
"code.gitea.io/tea/modules/print"
|
2019-10-19 10:54:16 +00:00
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-09-19 16:00:50 +00:00
|
|
|
"github.com/muesli/termenv"
|
2020-01-04 17:44:25 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-10-19 10:54:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdLabels represents to operate repositories' labels.
|
|
|
|
var CmdLabels = cli.Command{
|
|
|
|
Name: "labels",
|
2020-10-02 15:46:51 +00:00
|
|
|
Aliases: []string{"label"},
|
2019-11-03 20:34:41 +00:00
|
|
|
Usage: "Manage issue labels",
|
|
|
|
Description: `Manage issue labels`,
|
2019-10-19 10:54:16 +00:00
|
|
|
Action: runLabels,
|
2020-01-04 17:44:25 +00:00
|
|
|
Subcommands: []*cli.Command{
|
2020-09-30 05:11:33 +00:00
|
|
|
&labels.CmdLabelCreate,
|
|
|
|
&labels.CmdLabelUpdate,
|
|
|
|
&labels.CmdLabelDelete,
|
2019-10-19 10:54:16 +00:00
|
|
|
},
|
2019-10-26 04:50:30 +00:00
|
|
|
Flags: append([]cli.Flag{
|
2020-01-04 17:44:25 +00:00
|
|
|
&cli.StringFlag{
|
2020-03-06 03:43:28 +00:00
|
|
|
Name: "save",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "Save all the labels as a file",
|
2019-10-19 10:54:16 +00:00
|
|
|
},
|
2020-09-30 05:11:33 +00:00
|
|
|
&flags.PaginationPageFlag,
|
|
|
|
&flags.PaginationLimitFlag,
|
|
|
|
}, flags.AllDefaultFlags...),
|
2019-10-19 10:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runLabels(ctx *cli.Context) error {
|
2020-09-30 05:11:33 +00:00
|
|
|
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
|
2019-10-19 10:54:16 +00:00
|
|
|
|
2019-10-26 04:50:30 +00:00
|
|
|
headers := []string{
|
|
|
|
"Index",
|
|
|
|
"Color",
|
|
|
|
"Name",
|
2019-10-28 19:47:41 +00:00
|
|
|
"Description",
|
2019-10-26 04:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
labels, _, err := login.Client().ListRepoLabels(owner, repo, gitea.ListLabelsOptions{ListOptions: flags.GetListOptions(ctx)})
|
2019-10-19 10:54:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(labels) == 0 {
|
2020-09-30 05:11:33 +00:00
|
|
|
print.OutputList(flags.GlobalOutputValue, headers, values)
|
2019-10-19 10:54:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:00:50 +00:00
|
|
|
p := termenv.ColorProfile()
|
|
|
|
|
2019-10-19 10:54:16 +00:00
|
|
|
fPath := ctx.String("save")
|
|
|
|
if len(fPath) > 0 {
|
|
|
|
f, err := os.Create(fPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
for _, label := range labels {
|
|
|
|
fmt.Fprintf(f, "#%s %s\n", label.Color, label.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, label := range labels {
|
2020-09-19 16:00:50 +00:00
|
|
|
color := termenv.String(label.Color)
|
|
|
|
|
2019-10-26 04:50:30 +00:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
strconv.FormatInt(label.ID, 10),
|
2020-09-19 16:00:50 +00:00
|
|
|
fmt.Sprint(color.Background(p.Color("#" + label.Color))),
|
2019-10-26 04:50:30 +00:00
|
|
|
label.Name,
|
2019-10-28 19:47:41 +00:00
|
|
|
label.Description,
|
2019-10-26 04:50:30 +00:00
|
|
|
},
|
|
|
|
)
|
2019-10-19 10:54:16 +00:00
|
|
|
}
|
2020-09-30 05:11:33 +00:00
|
|
|
print.OutputList(flags.GlobalOutputValue, headers, values)
|
2019-10-19 10:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|