From 93a880aaa58f571619237cfea4121f0033ec7acc Mon Sep 17 00:00:00 2001 From: b4b4r07 Date: Sat, 8 Feb 2020 02:53:22 +0900 Subject: [PATCH] Refactor --- cli.go | 64 +++++++++---------------------------------- config.go | 6 +++++ interface.go | 76 +++++++++++++++++++++++++++++++--------------------- label.go | 20 ++++++++++++++ 4 files changed, 84 insertions(+), 82 deletions(-) diff --git a/cli.go b/cli.go index 627bc07..d2d2c2f 100644 --- a/cli.go +++ b/cli.go @@ -1,9 +1,9 @@ package main import ( - "context" "errors" "fmt" + "io" "log" "os" "strings" @@ -15,60 +15,20 @@ import ( yaml "gopkg.in/yaml.v2" ) -type Labeler interface { - GetLabel(ctx context.Context, owner string, repo string, name string) (*github.Label, *github.Response, error) - EditLabel(ctx context.Context, owner string, repo string, name string, label *github.Label) (*github.Label, *github.Response, error) - CreateLabel(ctx context.Context, owner string, repo string, label *github.Label) (*github.Label, *github.Response, error) - ListLabels(ctx context.Context, owner string, repo string, opt *github.ListOptions) ([]*github.Label, *github.Response, error) - DeleteLabel(ctx context.Context, owner string, repo string, name string) (*github.Response, error) -} - -type githubClientImpl struct { - ghClient *github.Client -} - -func (l githubClientImpl) GetLabel(ctx context.Context, owner string, repo string, name string) (*github.Label, *github.Response, error) { - return l.ghClient.Issues.GetLabel(ctx, owner, repo, name) -} - -func (l githubClientImpl) EditLabel(ctx context.Context, owner string, repo string, name string, label *github.Label) (*github.Label, *github.Response, error) { - return l.ghClient.Issues.EditLabel(ctx, owner, repo, name, label) -} - -func (l githubClientImpl) CreateLabel(ctx context.Context, owner string, repo string, label *github.Label) (*github.Label, *github.Response, error) { - return l.ghClient.Issues.CreateLabel(ctx, owner, repo, label) -} - -func (l githubClientImpl) ListLabels(ctx context.Context, owner string, repo string, opt *github.ListOptions) ([]*github.Label, *github.Response, error) { - return l.ghClient.Issues.ListLabels(ctx, owner, repo, opt) -} - -func (l githubClientImpl) DeleteLabel(ctx context.Context, owner string, repo string, name string) (*github.Response, error) { - return l.ghClient.Issues.DeleteLabel(ctx, owner, repo, name) -} - -type githubClientDryRun struct { - ghClient *github.Client -} - -func (l githubClientDryRun) GetLabel(ctx context.Context, owner string, repo string, name string) (*github.Label, *github.Response, error) { - return l.ghClient.Issues.GetLabel(ctx, owner, repo, name) -} - -func (l githubClientDryRun) EditLabel(ctx context.Context, owner string, repo string, name string, label *github.Label) (*github.Label, *github.Response, error) { - return nil, nil, nil -} - -func (l githubClientDryRun) CreateLabel(ctx context.Context, owner string, repo string, label *github.Label) (*github.Label, *github.Response, error) { - return nil, nil, nil -} +type CLI struct { + Stdout io.Writer + Stderr io.Writer + Option Option -func (l githubClientDryRun) ListLabels(ctx context.Context, owner string, repo string, opt *github.ListOptions) ([]*github.Label, *github.Response, error) { - return l.ghClient.Issues.ListLabels(ctx, owner, repo, opt) + GitHub *githubClient + Config Manifest } -func (l githubClientDryRun) DeleteLabel(ctx context.Context, owner string, repo string, name string) (*github.Response, error) { - return nil, nil +type Option struct { + DryRun bool `long:"dry-run" description:"Just dry run"` + Config string `short:"c" long:"config" description:"Path to YAML file that labels are defined" default:"labels.yaml"` + Import bool `long:"import" description:"Import existing labels if enabled"` + Version bool `long:"version" description:"Show version"` } type githubClient struct { diff --git a/config.go b/config.go index 3bf3740..0d70ab9 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,12 @@ import ( yaml "gopkg.in/yaml.v2" ) +// Manifest represents the YAML file described about labels and repos +type Manifest struct { + Labels Labels `yaml:"labels"` + Repos Repos `yaml:"repos"` +} + func loadManifest(path string) (Manifest, error) { var m Manifest buf, err := ioutil.ReadFile(path) diff --git a/interface.go b/interface.go index e416bfc..e531855 100644 --- a/interface.go +++ b/interface.go @@ -1,47 +1,63 @@ package main import ( - "io" + "context" + + "github.com/google/go-github/github" ) -// Manifest represents the YAML file described about labels and repos -type Manifest struct { - Labels Labels `yaml:"labels"` - Repos Repos `yaml:"repos"` +type Labeler interface { + GetLabel(ctx context.Context, owner string, repo string, name string) (*github.Label, *github.Response, error) + EditLabel(ctx context.Context, owner string, repo string, name string, label *github.Label) (*github.Label, *github.Response, error) + CreateLabel(ctx context.Context, owner string, repo string, label *github.Label) (*github.Label, *github.Response, error) + ListLabels(ctx context.Context, owner string, repo string, opt *github.ListOptions) ([]*github.Label, *github.Response, error) + DeleteLabel(ctx context.Context, owner string, repo string, name string) (*github.Response, error) +} + +type githubClientImpl struct { + ghClient *github.Client +} + +func (l githubClientImpl) GetLabel(ctx context.Context, owner string, repo string, name string) (*github.Label, *github.Response, error) { + return l.ghClient.Issues.GetLabel(ctx, owner, repo, name) +} + +func (l githubClientImpl) EditLabel(ctx context.Context, owner string, repo string, name string, label *github.Label) (*github.Label, *github.Response, error) { + return l.ghClient.Issues.EditLabel(ctx, owner, repo, name, label) } -// Label represents GitHub label -type Label struct { - Name string `yaml:"name"` - Description string `yaml:"description"` - Color string `yaml:"color"` - PreviousName string `yaml:"previous_name,omitempty"` +func (l githubClientImpl) CreateLabel(ctx context.Context, owner string, repo string, label *github.Label) (*github.Label, *github.Response, error) { + return l.ghClient.Issues.CreateLabel(ctx, owner, repo, label) } -// Labels represents a collection of Label -type Labels []Label +func (l githubClientImpl) ListLabels(ctx context.Context, owner string, repo string, opt *github.ListOptions) ([]*github.Label, *github.Response, error) { + return l.ghClient.Issues.ListLabels(ctx, owner, repo, opt) +} + +func (l githubClientImpl) DeleteLabel(ctx context.Context, owner string, repo string, name string) (*github.Response, error) { + return l.ghClient.Issues.DeleteLabel(ctx, owner, repo, name) +} -// Repo represents GitHub repository -type Repo struct { - Name string `yaml:"name"` - Labels []string `yaml:"labels"` +type githubClientDryRun struct { + ghClient *github.Client } -// Repos represents a collection of Repo -type Repos []Repo +func (l githubClientDryRun) GetLabel(ctx context.Context, owner string, repo string, name string) (*github.Label, *github.Response, error) { + return l.ghClient.Issues.GetLabel(ctx, owner, repo, name) +} -type CLI struct { - Stdout io.Writer - Stderr io.Writer - Option Option +func (l githubClientDryRun) EditLabel(ctx context.Context, owner string, repo string, name string, label *github.Label) (*github.Label, *github.Response, error) { + return nil, nil, nil +} + +func (l githubClientDryRun) CreateLabel(ctx context.Context, owner string, repo string, label *github.Label) (*github.Label, *github.Response, error) { + return nil, nil, nil +} - GitHub *githubClient - Config Manifest +func (l githubClientDryRun) ListLabels(ctx context.Context, owner string, repo string, opt *github.ListOptions) ([]*github.Label, *github.Response, error) { + return l.ghClient.Issues.ListLabels(ctx, owner, repo, opt) } -type Option struct { - DryRun bool `long:"dry-run" description:"Just dry run"` - Config string `short:"c" long:"config" description:"Path to YAML file that labels are defined" default:"labels.yaml"` - Import bool `long:"import" description:"Import existing labels if enabled"` - Version bool `long:"version" description:"Show version"` +func (l githubClientDryRun) DeleteLabel(ctx context.Context, owner string, repo string, name string) (*github.Response, error) { + return nil, nil } diff --git a/label.go b/label.go index 2a08b82..6a6b3f8 100644 --- a/label.go +++ b/label.go @@ -6,6 +6,26 @@ import ( "github.com/google/go-github/github" ) +// Label represents GitHub label +type Label struct { + Name string `yaml:"name"` + Description string `yaml:"description"` + Color string `yaml:"color"` + PreviousName string `yaml:"previous_name,omitempty"` +} + +// Labels represents a collection of Label +type Labels []Label + +// Repo represents GitHub repository +type Repo struct { + Name string `yaml:"name"` + Labels []string `yaml:"labels"` +} + +// Repos represents a collection of Repo +type Repos []Repo + // Get gets GitHub labels func (c *githubClient) GetLabel(owner, repo string, label Label) (Label, error) { ctx := context.Background()