Skip to content

Commit

Permalink
Add init command
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmachado committed Jan 7, 2020
1 parent 6796070 commit 252723b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rcmachado/changelog/chg"
"github.com/spf13/cobra"
)

func newInitCmd(iostreams *IOStreams) *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "creates a new changelog file",
Run: func(cmd *cobra.Command, args []string) {
compareURL := "https://github.com/rcmachado/changelog/compare/abcdef...HEAD"
c := chg.NewEmptyChangelog(compareURL)
c.Render(iostreams.Out)
},
}
}
36 changes: 36 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestInitCmd(t *testing.T) {
expected := `# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- First commit
[Unreleased]: https://github.com/rcmachado/changelog/compare/abcdef...HEAD
`

out := new(bytes.Buffer)
iostreams := &IOStreams{
In: nil,
Out: out,
}

cmd := newInitCmd(iostreams)
_, err := cmd.ExecuteC()

assert.Nil(t, err)
assert.Equal(t, expected, string(out.Bytes()))
}
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func openFileOrExit(fs *pflag.FlagSet, option string, flag int, defaultIfDash *o
func init() {
ioStreams = &IOStreams{}

initCmd := newInitCmd(ioStreams)
rootCmd.AddCommand(initCmd)

fmtCmd := NewFmtCmd(ioStreams)
rootCmd.AddCommand(fmtCmd)

Expand Down

0 comments on commit 252723b

Please sign in to comment.