Skip to content

Commit f8613f7

Browse files
committed
Added Viper
1 parent 3d6235b commit f8613f7

14 files changed

+287
-96
lines changed

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ gh extension install .
2525

2626
## Usage
2727

28-
### `changelog`
28+
### `gh itkdev changelog`
2929

3030
Manage changelog based on [keep a changelog](https://keepachangelog.com/en/1.1.0/):
3131

@@ -36,21 +36,43 @@ gh itkdev changelog --help
3636
Create changelog:
3737

3838
```shell
39-
gh itkdev changelog --create
39+
gh itkdev changelog create
4040
```
4141

42-
Update changelog for a pull request:
42+
Add pull request to changelog:
4343

4444
```shell
45-
gh itkdev changelog --fucking-changelog
45+
gh itkdev changelog add-pull-request
4646
```
4747

48-
Update changelog for a release (`«tag»`):
48+
Prepare a new release:
4949

5050
```shell
51-
gh itkdev changelog --release «tag»
51+
gh itkdev changelog add-release «tag»
5252
```
5353

54+
## Configuration
55+
56+
Default option values can be set in `.gh-itkdev.yaml` in the project folder or in `$HOME`.
57+
58+
``` yaml
59+
changelog:
60+
«sub command»:
61+
«option»: «value»
62+
```
63+
64+
### Example
65+
66+
Set the default branch for `add-release` to `main`:
67+
68+
``` yaml
69+
changelog:
70+
add-release:
71+
base: main
72+
```
73+
74+
Use `gh itkdev config` to dump the current config.
75+
5476
## Development
5577

5678
``` shell

changelog/fuckingchangelog.go renamed to changelog/add_pull_request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func addPullRequest(changelog string, pr pullRequest, itemTemplate string) (stri
5858
return strings.Join(lines, "\n") + "\n", nil
5959
}
6060

61-
func FuckingChangelog(name string, itemTemplate string) {
61+
func AddPullRequest(name string, entryTemplate string) {
6262
b, err := os.ReadFile(name)
6363
if err != nil {
6464
log.Fatal(err)
@@ -74,7 +74,7 @@ func FuckingChangelog(name string, itemTemplate string) {
7474
log.Fatalf("error getting pull request: %s\n", err)
7575
}
7676

77-
updatedChangelog, err := addPullRequest(changelog, pr, itemTemplate)
77+
updatedChangelog, err := addPullRequest(changelog, pr, entryTemplate)
7878
if err != nil {
7979
log.Fatalf("error adding pull request: %s\n", err)
8080
}

changelog/fuckingchangelog_test.go renamed to changelog/add_pull_request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
func TestFuckingChangelog(t *testing.T) {
9+
func TestAddPullRequest(t *testing.T) {
1010
defaultItemTemplate := `* [PR-{{ .Number }}]({{ .Url }})
1111
{{ .Title }}`
1212
testCases := []struct {

changelog/release.go renamed to changelog/add_release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func updateReleaseChangelog(changelog string, release string) (string, error) {
111111
return strings.Join(lines, "\n") + "\n", nil
112112
}
113113

114-
func Release(release string, base string, name string, commit bool) {
114+
func AddRelease(release string, base string, name string, commit bool) {
115115
b, err := os.ReadFile(name)
116116
if err != nil {
117117
log.Fatal(err)

changelog/release_test.go renamed to changelog/add_release_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
func TestRelease(t *testing.T) {
11+
func TestAddRelease(t *testing.T) {
1212
testCases := []struct {
1313
changelog string
1414
release string

cmd/changelog.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

cmd/changelog/add_pull_request.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package changelog
5+
6+
import (
7+
"github.com/rimi-itk/gh-itkdev/changelog"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
var (
13+
entryTemplate = `* [PR-{{ .Number }}]({{ .Url }})
14+
{{ .Title }}`
15+
addPullRequestCmd = &cobra.Command{
16+
Use: "add-pull-request",
17+
Short: "Add missing pull request entry to changelog",
18+
Run: func(cmd *cobra.Command, args []string) {
19+
changelog.AddPullRequest(changelogName, entryTemplate)
20+
},
21+
}
22+
)
23+
24+
func init() {
25+
viperPrefix := "changelog.add-pull-request."
26+
flagName := "entry-template"
27+
addPullRequestCmd.Flags().StringVarP(&entryTemplate, flagName, "", entryTemplate, "pull request entry template")
28+
viper.BindPFlag(viperPrefix+flagName, createCmd.Flags().Lookup(flagName))
29+
30+
ChangelogCmd.AddCommand(addPullRequestCmd)
31+
}

cmd/changelog/add_release.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package changelog
5+
6+
import (
7+
"github.com/rimi-itk/gh-itkdev/changelog"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
// createCmd represents the create command
13+
var (
14+
base = "develop"
15+
commit = false
16+
17+
addReleaseCmd = &cobra.Command{
18+
Use: "add-release",
19+
Short: "Create a release branch and update the changelog",
20+
Long: `Create a release branch named release/«release» and update the changelog as per https://keepachangelog.com/en/1.1.0/
21+
22+
Examples:
23+
24+
gh itkdev changelog add-release 0.1.0
25+
gh itkdev changelog add-release v0.1.0
26+
`,
27+
Args: cobra.ExactArgs(1),
28+
Run: func(cmd *cobra.Command, args []string) {
29+
release := args[0]
30+
31+
changelog.AddRelease(release, base, changelogName, commit)
32+
},
33+
}
34+
)
35+
36+
func init() {
37+
viperPrefix := "changelog.add-release."
38+
flagName := "base"
39+
addPullRequestCmd.Flags().StringVarP(&base, flagName, "", base, "base branch for release")
40+
viper.BindPFlag(viperPrefix+flagName, createCmd.Flags().Lookup(flagName))
41+
42+
flagName = "commit"
43+
addPullRequestCmd.Flags().BoolVarP(&commit, flagName, "", commit, "commit changes")
44+
viper.BindPFlag(viperPrefix+flagName, createCmd.Flags().Lookup(flagName))
45+
46+
ChangelogCmd.AddCommand(addReleaseCmd)
47+
}

cmd/changelog/changelog.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package changelog
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/spf13/viper"
6+
)
7+
8+
var (
9+
changelogName = "CHANGELOG.md"
10+
ChangelogCmd = &cobra.Command{
11+
Use: "changelog",
12+
Short: "Update changelog",
13+
}
14+
)
15+
16+
func init() {
17+
ChangelogCmd.Flags().StringVarP(&changelogName, "changelog", "", changelogName, "changelog name")
18+
viper.BindPFlag("changelog.changelog", ChangelogCmd.Flags().Lookup("changelog"))
19+
}

cmd/changelog/create.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package changelog
5+
6+
import (
7+
"github.com/rimi-itk/gh-itkdev/changelog"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// createCmd represents the create command
12+
var createCmd = &cobra.Command{
13+
Use: "create",
14+
Short: "Create a changelog if it does not exist",
15+
Long: `Create a changelog if it does not exist. The changelog will used the format defined by https://keepachangelog.com/en/1.1.0/.`,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
changelog.Create(changelogName)
18+
},
19+
}
20+
21+
func init() {
22+
ChangelogCmd.AddCommand(createCmd)
23+
}

0 commit comments

Comments
 (0)