Skip to content

Commit

Permalink
Merge pull request #42 from rcmachado/refactor-commands
Browse files Browse the repository at this point in the history
Refactor commands to simplify usageinput/output handling
  • Loading branch information
rcmachado committed Jan 6, 2020
2 parents 5e1b661 + 7deea51 commit 613b137
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 83 deletions.
10 changes: 5 additions & 5 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ var bundleCmd = &cobra.Command{
Short: "Bundles files containing unrelased changelog entries",
Long: "Bundles multiple files that follows the changetype/file.md structure into the Unreleased version",
Run: func(cmd *cobra.Command, args []string) {
input := readChangelog()
var bi bytes.Buffer
bi.ReadFrom(inputFile)

changelog := parser.Parse(input)
bundleFiles(directory, input, args, changelog)
changelog := parser.Parse(bi.Bytes())
bundleFiles(directory, bi.Bytes(), args, changelog)

var buf bytes.Buffer
changelog.Render(&buf)
output := buf.Bytes()

writeChangelog(output)
outputFile.ReadFrom(&buf)
},
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func buildCommands(rootCmd *cobra.Command) {
Short: fmt.Sprintf("Add item under '%s' section", cmdType.String()),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
input := readChangelog()
var bi bytes.Buffer
bi.ReadFrom(inputFile)

changelog := parser.Parse(input)
changelog := parser.Parse(bi.Bytes())
changelog.AddItem(cmdType, strings.Join(args, " "))

var buf bytes.Buffer
changelog.Render(&buf)
output := buf.Bytes()

writeChangelog(output)
outputFile.ReadFrom(&buf)
},
}

Expand Down
11 changes: 5 additions & 6 deletions cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ var fmtCmd = &cobra.Command{
Short: "Reformat the change log file",
Long: "Reformats changelog input following keepachangelog.com spec",
Run: func(cmd *cobra.Command, args []string) {
input := readChangelog()

chg := parser.Parse(input)
var bi bytes.Buffer
bi.ReadFrom(inputFile)
changelog := parser.Parse(bi.Bytes())

var buf bytes.Buffer
chg.Render(&buf)
output := buf.Bytes()
changelog.Render(&buf)

writeChangelog(output)
outputFile.ReadFrom(&buf)
},
}

Expand Down
15 changes: 7 additions & 8 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ It will normalize the output with the new version.
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
input := readChangelog()

var buf bytes.Buffer
release(input, args, &buf)
output := buf.Bytes()

writeChangelog(output)
release(inputFile, args, &buf)
outputFile.ReadFrom(&buf)
},
}

Expand All @@ -42,7 +38,7 @@ func init() {
rootCmd.AddCommand(releaseCmd)
}

func release(input []byte, args []string, w io.Writer) {
func release(input io.Reader, args []string, w io.Writer) {
version := chg.Version{
Name: args[0],
Date: releaseDate,
Expand All @@ -52,7 +48,10 @@ func release(input []byte, args []string, w io.Writer) {
version.Link = compareURL
}

changelog := parser.Parse(input)
var bi bytes.Buffer
bi.ReadFrom(inputFile)
changelog := parser.Parse(bi.Bytes())

_, err := changelog.Release(version)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create release '%s': %s\n", args[0], err)
Expand Down
86 changes: 32 additions & 54 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,62 @@
package cmd

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var inputFile *bufio.Reader
var outputFile *bufio.Writer

var rootCmd = &cobra.Command{
Use: "changelog",
Short: "Manipulate and validate changelog files",
Long: `changelog manipulate and validate markdown changelog files following the keepachangelog.com specification.`,
}

var inputFilename string
var outputFilename string
PersistentPreRun: func(cmd *cobra.Command, args []string) {
fs := cmd.Flags()

func init() {
flags := rootCmd.PersistentFlags()
flags.StringVarP(&inputFilename, "filename", "f", "CHANGELOG.md", "Changelog file or '-' for stdin")
flags.StringVarP(&outputFilename, "output", "o", "-", "Output file or '-' for stdout")
}
fdr := openFileOrExit(fs, "filename", os.O_RDONLY, os.Stdin)
inputFile = bufio.NewReader(fdr)

func readChangelog() []byte {
name := inputFilename
if name == "-" {
content, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(2)
fdw := openFileOrExit(fs, "output", os.O_WRONLY|os.O_CREATE, os.Stdout)
outputFile = bufio.NewWriter(fdw)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if outputFile != nil {
outputFile.Flush()
}
return content
}
},
}

var prefixDir string
if strings.HasPrefix(name, "/") {
prefixDir = ""
} else {
prefixDir = "./"
}
filename, err := filepath.Abs(prefixDir + name)
func openFileOrExit(fs *pflag.FlagSet, option string, flag int, defaultIfDash *os.File) *os.File {
filename, err := fs.GetString(option)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
fmt.Printf("Failed to get option '%s': %s\n", option, err)
os.Exit(2)
}
content, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(2)
}
return content
}

func writeChangelog(content []byte) {
if outputFilename == "-" {
os.Stdout.Write(content)
return
if filename == "-" {
return defaultIfDash
}

var prefixDir string
if strings.HasPrefix(outputFilename, "/") {
prefixDir = ""
} else {
prefixDir = "./"
}

filename, err := filepath.Abs(prefixDir + outputFilename)
file, err := os.OpenFile(filename, flag, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
fmt.Printf("Failed to open file '%s': %s\n", filename, err)
os.Exit(2)
}
return file
}

err = ioutil.WriteFile(filename, content, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(2)
}
func init() {
flags := rootCmd.PersistentFlags()
flags.StringP("filename", "f", "CHANGELOG.md", "Changelog file or '-' for stdin")
rootCmd.MarkFlagFilename("filename")
flags.StringP("output", "o", "-", "Output file or '-' for stdout")
rootCmd.MarkFlagFilename("output")
}

// Execute the program with command-line args
Expand Down
11 changes: 5 additions & 6 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@ var showCmd = &cobra.Command{
Long: `Show changelog section and entries for version [version]`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
input := readChangelog()
var bi bytes.Buffer
bi.ReadFrom(inputFile)
changelog := parser.Parse(bi.Bytes())

chg := parser.Parse(input)

v := chg.Version(args[0])
v := changelog.Version(args[0])
if v == nil {
fmt.Printf("Unknown version: '%s'\n", args[0])
os.Exit(3)
}

var buf bytes.Buffer
v.RenderChanges(&buf)
output := buf.Bytes()

writeChangelog(output)
outputFile.ReadFrom(&buf)
},
}

Expand Down

0 comments on commit 613b137

Please sign in to comment.