Skip to content

Commit

Permalink
feat: add writer step
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 23, 2020
1 parent f8df0ba commit 441ad13
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
43 changes: 43 additions & 0 deletions 6_writer/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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))

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

return n, nil
}
6 changes: 5 additions & 1 deletion HOW_IT_WORKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ Up to this point, the role of generator is to generate templates and which templ

### 5. formatter

In this step we will format the output. For example, format markdown/json
In this step we will format the output. For example, format markdown/json

### 6. writer

Output content at this stage, it determines whether to output a file or stdout
25 changes: 3 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
"os"
"path"

parser "github.com/axetroy/changelog/1_parser"
extractor "github.com/axetroy/changelog/2_extractor"
transform "github.com/axetroy/changelog/3_transform"
generator "github.com/axetroy/changelog/4_generator"
formatter "github.com/axetroy/changelog/5_formatter"
writer "github.com/axetroy/changelog/6_writer"
"github.com/axetroy/changelog/internal/client"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -154,30 +152,13 @@ func run() error {
return errors.WithStack(err)
}

var writer io.Writer

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

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

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

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

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

_, err = io.Copy(writer, bytes.NewBuffer(output))
_, err = writer.Write(formattedOutput, outputFile)

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

0 comments on commit 441ad13

Please sign in to comment.