Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 7, 2020
1 parent bb5e238 commit 93a880a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 82 deletions.
64 changes: 12 additions & 52 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
76 changes: 46 additions & 30 deletions interface.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 93a880a

Please sign in to comment.