diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7715080..2015635 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,13 +8,23 @@ This document provides guidelines and best practices to help you contribute effe ## 📇Topics -- [What we expect of you](#What-we-expect-of-you) -- [Code of Conduct](#Code-of-Conduct) -- [Getting Started](#Getting-Started) -- [Style and Specification](#Style-and-Specification) -- [Engage to help anything](#Engage-to-help-anything) -- [Release version](#Release-version) -- [Contact Us](#Contact-Us) +- [Contributing to github-label-syncer](#contributing-to-github-label-syncer) + - [📇Topics](#topics) + - [What we expect of you](#what-we-expect-of-you) + - [Code of ConductCode of Conduct](#code-of-conductcode-of-conduct) + - [Code and doc contribution](#code-and-doc-contribution) + - [Where should I start?](#where-should-i-start) + - [Design documents](#design-documents) + - [Getting Started](#getting-started) + - [Style and Specification](#style-and-specification) + - [Reporting security issues](#reporting-security-issues) + - [Reporting general issues](#reporting-general-issues) + - [Commit Rules](#commit-rules) + - [PR Description](#pr-description) + - [Docs Contribution](#docs-contribution) + - [Engage to help anything](#engage-to-help-anything) + - [Release version](#release-version) + - [Contact Us](#contact-us) ## What we expect of you @@ -175,7 +185,7 @@ To be honest, we regard every user of github-label-synceras a very kind contribu Since we collaborate project github-label-syncer in a distributed way, we appreciate **WELL-WRITTEN**, **DETAILED**, **EXPLICIT** issue reports. To make the communication more efficient, we wish everyone could search if your issue is an existing one in the searching list. If you find it existing, please add your details in comments under the existing issue instead of opening a brand new one. -To make the issue details as standard as possible, we setup an [ISSUE TEMPLATE](https://github.com/kubecub/github-label-syncer/tree/main/.github/ISSUE_TEMPLATE) for issue reporters. You can find three kinds of issue templates there: question, bug report and feature request. Please **BE SURE** to follow the instructions to fill fields in template. +To make the issue details as standard as possible, we setup an [ISSUE TEMPLATE](https://github.com/kubecub/.github/tree/main/.github/ISSUE_TEMPLATE) for issue reporters. You can find three kinds of issue templates there: question, bug report and feature request. Please **BE SURE** to follow the instructions to fill fields in template. **There are a lot of cases when you could open an issue:** diff --git a/README.md b/README.md index 650dea9..60025fc 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,6 @@ The labels are designed semantic and standard. We provide labels for priority, t ❯ make help # show help ❯ make build # build binary exporter and syncer ❯ echo 'export PATH=$PATH:/path/to/_output/platforms/linux/amd64/' | tee -a ~/.zshrc;source ~/.zshrc - ``` @@ -90,8 +89,10 @@ Comment in an issue:
Use +You can set your own `GITHUB_TOKEN` via env or via the `export GITHUB_TOKEN` environment variable, or use one of the default tokens we provide, which is `TOEKN` for our free automated [🤖 robot](https://github.com/kubbot) + ```bash -❯ ./_output/platforms/linux/amd64/exporter --help +❯ ./_output/bin/platforms/linux/amd64/exporter --help usage: exporter [] @@ -100,6 +101,10 @@ Flags: -y, --[no-]yaml Use the YAML format. -j, --[no-]json Use the JSON format. -t, --[no-]table Use the table format. + -x, --[no-]xml Use the XML format. + --[no-]toml Use the TOML format. + --[no-]ini Use the INI format. + --[no-]csv Use the CSV format. Args: Owner of the repository. diff --git a/cmd/exporter/main.go b/cmd/exporter/main.go index a36037c..72b3e16 100644 --- a/cmd/exporter/main.go +++ b/cmd/exporter/main.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "log" + "os" "runtime/debug" "github.com/alecthomas/kingpin/v2" @@ -23,14 +24,20 @@ var ( yaml = kingpin.Flag("yaml", "Use the YAML format.").Short('y').Bool() json = kingpin.Flag("json", "Use the JSON format.").Short('j').Bool() table = kingpin.Flag("table", "Use the table format.").Short('t').Bool() + xml = kingpin.Flag("xml", "Use the XML format.").Short('x').Bool() // TODO: Add support for these formats. - xml = kingpin.Flag("xml", "Use the XML format.").Short('x').Bool() toml = kingpin.Flag("toml", "Use the TOML format.").Bool() ini = kingpin.Flag("ini", "Use the INI format.").Bool() csv = kingpin.Flag("csv", "Use the CSV format.").Bool() ) +var ( + // New flags. + file = kingpin.Flag("file", "Export labels to file.").Short('f').String() + token = kingpin.Flag("token", "GitHub token.").Envar("GITHUB_TOKEN").String() +) + var ( goVersion = "unknown" // Populated by goreleaser during build @@ -67,7 +74,15 @@ func main() { if err != nil { log.Fatal(err) } - fmt.Println(string(b)) + if *file != "" { + err = os.WriteFile(*file, b, 0644) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Labels exported to %s\n", *file) + } else { + fmt.Println(string(b)) + } return } @@ -76,7 +91,15 @@ func main() { if err != nil { log.Fatal(err) } - fmt.Println(string(b)) + if *file != "" { + err = os.WriteFile(*file, b, 0644) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Labels exported to %s\n", *file) + } else { + fmt.Println(string(b)) + } return } @@ -85,7 +108,15 @@ func main() { if err != nil { log.Fatal(err) } - fmt.Println(string(b)) + if *file != "" { + err = os.WriteFile(*file, b, 0644) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Labels exported to %s\n", *file) + } else { + fmt.Println(string(b)) + } return } @@ -94,7 +125,15 @@ func main() { if err != nil { log.Fatal(err) } - fmt.Println(string(b)) + if *file != "" { + err = os.WriteFile(*file, b, 0644) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Labels exported to %s\n", *file) + } else { + fmt.Println(string(b)) + } return } diff --git a/go.mod b/go.mod index f6dba55..89eba94 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 golang.org/x/oauth2 v0.8.0 golang.org/x/sync v0.2.0 + gopkg.in/ini.v1 v1.67.0 gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 sigs.k8s.io/yaml v1.3.0 @@ -67,6 +68,5 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect - gopkg.in/ini.v1 v1.67.0 // indirect k8s.io/klog v1.0.0 // indirect ) diff --git a/pkg/exporter/client.go b/pkg/exporter/client.go index 120baff..f82fbd4 100644 --- a/pkg/exporter/client.go +++ b/pkg/exporter/client.go @@ -3,6 +3,7 @@ package exporter import ( "context" "errors" + "fmt" "os" "os/exec" "path/filepath" @@ -23,6 +24,7 @@ func NewClient() (*githubClient, error) { envPath := filepath.Join(".", ".env") if _, err := os.Stat(envPath); os.IsNotExist(err) { envTemplatePath := filepath.Join(".", ".env.template") + fmt.Println("envPath: ", envPath) if _, err := os.Stat(envTemplatePath); os.IsNotExist(err) { return nil, errors.New("missing .env.template file") } diff --git a/pkg/exporter/exporter.go b/pkg/exporter/exporter.go index 5863e03..36b729a 100644 --- a/pkg/exporter/exporter.go +++ b/pkg/exporter/exporter.go @@ -6,9 +6,13 @@ package exporter import ( "bytes" + "encoding/csv" "encoding/json" + "encoding/xml" + "fmt" "github.com/olekukonko/tablewriter" + "gopkg.in/ini.v1" "sigs.k8s.io/yaml" ) @@ -31,14 +35,17 @@ type LabelsToObject interface { } func LabelsToJSON(labels []*Label) ([]byte, error) { + fmt.Println("hits labels to json") return json.Marshal(labels) } func LabelsToYAML(labels []*Label) ([]byte, error) { + fmt.Println("hits labels to yaml") return yaml.Marshal(labels) } func LabelsToTable(labels []*Label) ([]byte, error) { + fmt.Println("hits labels to table") labelRows := make([][]string, 0, len(labels)) for _, l := range labels { labelRows = append(labelRows, []string{l.Name, l.Description, l.Color}) @@ -54,17 +61,62 @@ func LabelsToTable(labels []*Label) ([]byte, error) { } func LabelsToXML(labels []*Label) ([]byte, error) { - return nil, nil + fmt.Println("hits labels to xml") + type XMLLabels struct { + XMLName xml.Name `xml:"labels"` + Labels []*Label `xml:"label"` + } + xmlLabels := &XMLLabels{Labels: labels} + return xml.MarshalIndent(xmlLabels, "", " ") } func LabelsToTOML(labels []*Label) ([]byte, error) { + fmt.Println("hits labels to toml") + type TOMLLabel struct { + Name string `toml:"name"` + Description string `toml:"description"` + Color string `toml:"color"` + } + type TOMLLabels struct { + Labels []*TOMLLabel `toml:"label"` + } + tomlLabels := &TOMLLabels{} + for _, label := range labels { + tomlLabels.Labels = append(tomlLabels.Labels, &TOMLLabel{ + Name: label.Name, + Description: label.Description, + Color: label.Color, + }) + } return nil, nil } func LabelsToINI(labels []*Label) ([]byte, error) { + fmt.Println("hits labels to ini") + cfg := ini.Empty() + for _, label := range labels { + section, err := cfg.NewSection(label.Name) + if err != nil { + return nil, err + } + section.NewKey("description", label.Description) + section.NewKey("color", label.Color) + } return nil, nil } func LabelsToCSV(labels []*Label) ([]byte, error) { - return nil, nil + fmt.Println("hits labels to csv") + b := &bytes.Buffer{} + w := csv.NewWriter(b) + if err := w.Write([]string{"Name", "Description", "Color"}); err != nil { + return nil, err + } + for _, label := range labels { + if err := w.Write([]string{label.Name, label.Description, label.Color}); err != nil { + return nil, err + } + } + w.Flush() + return b.Bytes(), nil }