Skip to content

Commit

Permalink
feat: add whatchanged package for Go
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 26, 2020
1 parent 46b9831 commit 0bb75dd
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
version: v1.32.2
args: --timeout=5m30s

- name: Format
run: make format-check
# - name: Format
# run: make format-check

- name: Test
run: |
Expand Down
24 changes: 18 additions & 6 deletions 4_generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/url"
"strings"

transformer "github.com/axetroy/whatchanged/3_transformer"
"github.com/axetroy/whatchanged/internal/client"
"github.com/axetroy/whatchanged/option"
"github.com/pkg/errors"
giturls "github.com/whilp/git-urls"
)

func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, format string, preset string, templateStr string) ([]byte, error) {
func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, format option.Format, preset option.Preset, templateFile string) ([]byte, error) {
var (
templateStr string
)

remote, err := g.GetRemote()

if err != nil {
Expand Down Expand Up @@ -52,30 +58,36 @@ func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, form
}

switch format {
case "json":
case option.FormatJSON:
output, err := json.Marshal(contexts)

if err != nil {
return nil, errors.WithStack(err)
}

return output, nil
case "md":
case option.FormatMarkdown:
var output []byte

if err != nil {
return nil, errors.WithStack(err)
}

if templateStr == "" {
if templateFile == "" {
switch preset {
case "default":
case option.PresetDefault:
templateStr = DEFAULT_TEMPLATE
case "full":
case option.PresetFull:
templateStr = FULL_TEMPLATE
default:
return nil, errors.Errorf("invalid preset '%s'", preset)
}
} else {
if b, err := ioutil.ReadFile(templateFile); err != nil {
return nil, err
} else {
templateStr = string(b)
}
}

for _, ctx := range contexts {
Expand Down
7 changes: 4 additions & 3 deletions 5_formatter/format.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package formatter

import (
"github.com/axetroy/whatchanged/option"
"github.com/pkg/errors"
"github.com/shurcooL/markdownfmt/markdown"
)

func Format(src []byte, format string, templateFile string) ([]byte, error) {
func Format(src []byte, format option.Format, templateFile string) ([]byte, error) {

switch format {
case "json":
case option.FormatJSON:
return src, nil
case "md":
case option.FormatMarkdown:
var (
filename string
isTerminal = true
Expand Down
29 changes: 2 additions & 27 deletions 6_writer/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,12 @@ package writer
import (
"bytes"
"io"
"os"
"path"

"github.com/pkg/errors"
)

func Write(src []byte, templateFile string) (int64, error) {
cwd, err := os.Getwd()

if err != nil {
return 0, errors.WithStack(err)
}

var writer io.Writer

if templateFile != "" {
if !path.IsAbs(templateFile) {
templateFile = path.Join(cwd, templateFile)
}
file, err := os.Create(templateFile)

if err != nil {
return 0, errors.WithStack(err)
}

writer = file
} else {
writer = os.Stdout
}

n, err := io.Copy(writer, bytes.NewBuffer(src))
func Write(src []byte, w io.Writer) (int64, error) {
n, err := io.Copy(w, bytes.NewBuffer(src))

if err != nil {
return n, errors.WithStack(err)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ format:
go fmt ./...

format-check:
gofmt -l main.go
gofmt -l -d ./..
74 changes: 14 additions & 60 deletions cmd/whatchanged/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"

parser "github.com/axetroy/whatchanged/1_parser"
extractor "github.com/axetroy/whatchanged/2_extractor"
transformer "github.com/axetroy/whatchanged/3_transformer"
generator "github.com/axetroy/whatchanged/4_generator"
formatter "github.com/axetroy/whatchanged/5_formatter"
writer "github.com/axetroy/whatchanged/6_writer"
"github.com/axetroy/whatchanged/internal/client"

"github.com/axetroy/whatchanged"
"github.com/axetroy/whatchanged/option"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -126,60 +119,26 @@ func run() error {

version := flag.Arg(0)

client, err := client.NewGitClient(projectDir)

if err != nil {
return errors.WithStack(err)
}

scope, err := parser.Parse(client, version)

if err != nil {
return errors.WithStack(err)
}

splices, err := extractor.Extract(client, scope)

if err != nil {
return errors.WithStack(err)
}

ctxs, err := transformer.Transform(client, splices)

if err != nil {
return errors.WithStack(err)
}
var output = os.Stdout

var templateStr string

if templateFile != "" {
if !path.IsAbs(templateFile) {
templateFile = path.Join(cwd, templateFile)
}
fileBytes, err := ioutil.ReadFile(templateFile)
if outputFile != "" {
f, err := os.OpenFile(outputFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm)

if err != nil {
return errors.WithStack(err)
}

templateStr = string(fileBytes)
}

output, err := generator.Generate(client, ctxs, format, preset, templateStr)
defer f.Close()

if err != nil {
return errors.WithStack(err)
output = f
}

formattedOutput, err := formatter.Format(output, format, templateFile)

if err != nil {
return errors.WithStack(err)
}

_, err = writer.Write(formattedOutput, outputFile)

if err != nil {
if err := whatchanged.Generate(projectDir, output, &option.Options{
Version: version,
Preset: option.Preset(preset),
Format: option.Format(format),
TemplateFile: templateFile,
}); err != nil {
return errors.WithStack(err)
}

Expand All @@ -192,11 +151,6 @@ func main() {
)

defer func() {
// if r := recover(); r != nil {
// fmt.Printf("%+v\n", r)
// os.Exit(255)
// }

if err != nil {
fmt.Printf("%+v\n", err)
os.Exit(255)
Expand Down
20 changes: 20 additions & 0 deletions option/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package option

type Format string
type Preset string

const (
// format
FormatMarkdown Format = "md"
FormatJSON Format = "json"
// preset
PresetDefault Preset = "default"
PresetFull Preset = "full"
)

type Options struct {
Version string
Format Format
Preset Preset
TemplateFile string // Priority is higher than preset
}
89 changes: 89 additions & 0 deletions whatchanged.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package whatchanged

import (
"io"
"os"
"path"

parser "github.com/axetroy/whatchanged/1_parser"
extractor "github.com/axetroy/whatchanged/2_extractor"
transformer "github.com/axetroy/whatchanged/3_transformer"
generator "github.com/axetroy/whatchanged/4_generator"
formatter "github.com/axetroy/whatchanged/5_formatter"
writer "github.com/axetroy/whatchanged/6_writer"
"github.com/axetroy/whatchanged/internal/client"
"github.com/axetroy/whatchanged/option"
"github.com/pkg/errors"
)

func Generate(project string, w io.Writer, options *option.Options) error {
if options == nil {
options = &option.Options{
Format: option.FormatMarkdown,
Preset: option.PresetDefault,
}
} else {
if options.Format == "" {
options.Format = option.FormatMarkdown
}
if options.Preset == "" {
options.Preset = option.PresetDefault
}
}

if options.TemplateFile != "" {
cwd, err := os.Getwd()

if err != nil {
return errors.WithStack(err)
}

if !path.IsAbs(options.TemplateFile) {
options.TemplateFile = path.Join(cwd, options.TemplateFile)
}
}

client, err := client.NewGitClient(project)

if err != nil {
return errors.WithStack(err)
}

scope, err := parser.Parse(client, options.Version)

if err != nil {
return errors.WithStack(err)
}

splices, err := extractor.Extract(client, scope)

if err != nil {
return errors.WithStack(err)
}

ctxs, err := transformer.Transform(client, splices)

if err != nil {
return errors.WithStack(err)
}

output, err := generator.Generate(client, ctxs, options.Format, options.Preset, options.TemplateFile)

if err != nil {
return errors.WithStack(err)
}

formattedOutput, err := formatter.Format(output, options.Format, options.TemplateFile)

if err != nil {
return errors.WithStack(err)
}

_, err = writer.Write(formattedOutput, w)

if err != nil {
return errors.WithStack(err)
}

return nil
}

0 comments on commit 0bb75dd

Please sign in to comment.