mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-24 11:31:36 +01:00
f445ac7521
fix lint fix lint Move print TrackedTimesList to print package Move AbsPathWithExpansion to utils/path.go rename module intern to config Move Subcomands into it's own Packages Split times subcomands into own sourcefiles Split repos subcomands into own sourcefiles Split releases subcomands into own sourcefiles Split pulls subcomands into own sourcefiles Split milestones subcomands into own sourcefiles Split login subcomands into own sourcefiles Split labels subcomands into own sourcefiles split issues subcomands into own sourcefiles mv Move Interactive Login Creation to interact package Move Add Login function to intern/login.go apply from review lint: add description to exported func smal nits Move DetailViews stdout print func to print package Refactor: * Move Config & Login routines into intern package * rename global var in cmd * Move help func to utils Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/206 Reviewed-by: Norwin <noerw@noreply.gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
110 lines
2.4 KiB
Go
110 lines
2.4 KiB
Go
// 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 (
|
|
"bufio"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
// CmdLabelCreate represents a sub command of labels to create label.
|
|
var CmdLabelCreate = cli.Command{
|
|
Name: "create",
|
|
Usage: "Create a label",
|
|
Description: `Create a label`,
|
|
Action: runLabelCreate,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "name",
|
|
Usage: "label name",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "color",
|
|
Usage: "label color value",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "description",
|
|
Usage: "label description",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "file",
|
|
Usage: "indicate a label file",
|
|
},
|
|
},
|
|
}
|
|
|
|
func runLabelCreate(ctx *cli.Context) error {
|
|
login, owner, repo := config.InitCommand(flags.GlobalRepoValue, flags.GlobalLoginValue, flags.GlobalRemoteValue)
|
|
|
|
labelFile := ctx.String("file")
|
|
var err error
|
|
if len(labelFile) == 0 {
|
|
_, _, err = login.Client().CreateLabel(owner, repo, gitea.CreateLabelOption{
|
|
Name: ctx.String("name"),
|
|
Color: ctx.String("color"),
|
|
Description: ctx.String("description"),
|
|
})
|
|
} else {
|
|
f, err := os.Open(labelFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
var i = 1
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
color, name, description := splitLabelLine(line)
|
|
if color == "" || name == "" {
|
|
log.Printf("Line %d ignored because lack of enough fields: %s\n", i, line)
|
|
} else {
|
|
_, _, err = login.Client().CreateLabel(owner, repo, gitea.CreateLabelOption{
|
|
Name: name,
|
|
Color: color,
|
|
Description: description,
|
|
})
|
|
}
|
|
|
|
i++
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func splitLabelLine(line string) (string, string, string) {
|
|
fields := strings.SplitN(line, ";", 2)
|
|
var color, name, description string
|
|
if len(fields) < 1 {
|
|
return "", "", ""
|
|
} else if len(fields) >= 2 {
|
|
description = strings.TrimSpace(fields[1])
|
|
}
|
|
fields = strings.Fields(fields[0])
|
|
if len(fields) <= 0 {
|
|
return "", "", ""
|
|
}
|
|
color = fields[0]
|
|
if len(fields) == 2 {
|
|
name = fields[1]
|
|
} else if len(fields) > 2 {
|
|
name = strings.Join(fields[1:], " ")
|
|
}
|
|
return color, name, description
|
|
}
|