-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add new notes output to get command #153
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,6 +13,32 @@ import ( | |||||||||||||||||||||||
"github.com/spf13/cobra" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
type outputEnum string | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const ( | ||||||||||||||||||||||||
outputStandard outputEnum = "standard" | ||||||||||||||||||||||||
outputNotes outputEnum = "notes" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (e *outputEnum) String() string { | ||||||||||||||||||||||||
return string(*e) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (e *outputEnum) Set(v string) error { | ||||||||||||||||||||||||
switch v { | ||||||||||||||||||||||||
case string(outputStandard), string(outputNotes): | ||||||||||||||||||||||||
*e = outputEnum(v) | ||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
default: | ||||||||||||||||||||||||
return fmt.Errorf(`must be one of %s or %s`, outputStandard, outputNotes) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (e *outputEnum) Type() string { | ||||||||||||||||||||||||
return "outputEnum" | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
var outputTemplate = outputStandard | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
var printLatest bool | ||||||||||||||||||||||||
var printVersion string | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -36,23 +62,33 @@ This command is useful for creating and updating Release notes in GitHub. | |||||||||||||||||||||||
RunE: func(command *cobra.Command, args []string) error { | ||||||||||||||||||||||||
fileName := configuration.Config.FileName | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
var tmplSrc string | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
var changelog changelog.Changelog | ||||||||||||||||||||||||
var err error | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if printLatest { | ||||||||||||||||||||||||
changelog, err = get.GetLatest(fileName) | ||||||||||||||||||||||||
} else if printVersion != "" { | ||||||||||||||||||||||||
changelog, err = get.GetVersion(fileName, printVersion) | ||||||||||||||||||||||||
} else if outputTemplate == outputNotes { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this comparison, we can just do a simple string one:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. outputType makes sense, but comparing to a string does not. All of this ties back to command flag validation and using constants to expose issues early, at compile time. |
||||||||||||||||||||||||
err = fmt.Errorf("notes output only supported with latest or version") | ||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
changelog, err = get.GetAll(fileName) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
switch outputTemplate { | ||||||||||||||||||||||||
case outputStandard: | ||||||||||||||||||||||||
tmplSrc = writer.TmplSrcStandard | ||||||||||||||||||||||||
case outputNotes: | ||||||||||||||||||||||||
tmplSrc = writer.TmplSrcNotes | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Comment on lines
+79
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove l79 - l85 |
||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
var buf bytes.Buffer | ||||||||||||||||||||||||
if err := writer.Write(&buf, changelog); err != nil { | ||||||||||||||||||||||||
if err := writer.Write(&buf, tmplSrc, changelog); err != nil { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -77,5 +113,11 @@ func init() { | |||||||||||||||||||||||
"Prints a specific version from the changelog to stdout.", | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
getCmd.Flags().Var( | ||||||||||||||||||||||||
&outputTemplate, | ||||||||||||||||||||||||
"output", | ||||||||||||||||||||||||
fmt.Sprintf(`Output template. allowed: "%s" or "%s"`, outputStandard, outputNotes), | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
Comment on lines
+116
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the flag definition, we can use
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does flag value validation happen? |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
getCmd.Flags().SortFlags = false | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -45,7 +45,7 @@ var newCmd = &cobra.Command{ | |||||
return err | ||||||
} | ||||||
|
||||||
if err := writer.Write(f, changelog); err != nil { | ||||||
if err := writer.Write(f, writer.TmplSrcStandard, changelog); err != nil { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simple change here -
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this trade compile time for run-time errors and then it needs more tests to prevent invalid values when it's implicit using types and constants? |
||||||
return err | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import ( | |
"github.com/chelnak/gh-changelog/pkg/changelog" | ||
) | ||
|
||
var tmplSrc = `<!-- markdownlint-disable MD024 --> | ||
const tmplStandard = `<!-- markdownlint-disable MD024 --> | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
@@ -79,7 +79,57 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a | |
{{- end}} | ||
` | ||
|
||
func Write(writer io.Writer, changelog changelog.Changelog) error { | ||
const tmplNotes = `{{range .GetEntries }} | ||
{{- if .Security }} | ||
### Security | ||
{{range .Security}} | ||
- {{.}} | ||
{{- end}} | ||
{{end}} | ||
{{- if .Changed }} | ||
### Changed | ||
{{range .Changed}} | ||
- {{.}} | ||
{{- end}} | ||
{{end}} | ||
{{- if .Removed }} | ||
### Removed | ||
{{range .Removed}} | ||
- {{.}} | ||
{{- end}} | ||
{{end}} | ||
{{- if .Deprecated }} | ||
### Deprecated | ||
{{range .Deprecated}} | ||
- {{.}} | ||
{{- end}} | ||
{{end}} | ||
{{- if .Added }} | ||
### Added | ||
{{range .Added}} | ||
- {{.}} | ||
{{- end}} | ||
{{end}} | ||
{{- if .Fixed }} | ||
### Fixed | ||
{{range .Fixed}} | ||
- {{.}} | ||
{{- end}} | ||
{{end}} | ||
{{- if .Other }} | ||
### Other | ||
{{range .Other}} | ||
- {{.}} | ||
{{- end}} | ||
{{end}} | ||
{{- end}}` | ||
|
||
const ( | ||
TmplSrcStandard = tmplStandard | ||
TmplSrcNotes = tmplNotes | ||
) | ||
Comment on lines
+127
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would no longer need to expose these two consts |
||
|
||
func Write(writer io.Writer, tmplSrc string, changelog changelog.Changelog) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And Write would become:
|
||
tmpl, err := template.New("changelog").Funcs(template.FuncMap{ | ||
"getFirstCommit": func() string { | ||
git := gitclient.NewGitClient(exec.Command) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we keep the implementation inside the writer package?
I'll explain what I mean over the next few comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only for handling the command line arguments. Open to suggestions on how to remove some of the duplication.