Skip to content

Add Viper and clean up #8

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* [PR-8](https://github.com/rimi-itk/gh-itkdev/pull/8)
Added Viper
* [PR-7](https://github.com/rimi-itk/gh-itkdev/pull/7)
Fixed URL parsing

Expand Down
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ gh extension install .

## Usage

### `changelog`
### `gh itkdev changelog`

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

Expand All @@ -36,21 +36,43 @@ gh itkdev changelog --help
Create changelog:

```shell
gh itkdev changelog --create
gh itkdev changelog create
```

Update changelog for a pull request:
Add pull request to changelog:

```shell
gh itkdev changelog --fucking-changelog
gh itkdev changelog add-pull-request
```

Update changelog for a release (`«tag»`):
Prepare a new release:

```shell
gh itkdev changelog --release «tag»
gh itkdev changelog add-release «tag»
```

## Configuration

Default option values can be set in `.gh-itkdev.yaml` in the project folder or in `$HOME`.

``` yaml
changelog:
«sub command»:
«option»: «value»
```

### Example

Set the default branch for `add-release` to `main`:

``` yaml
changelog:
add-release:
base: main
```

Use `gh itkdev config` to dump the current config.

## Development

``` shell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func addPullRequest(changelog string, pr pullRequest, itemTemplate string) (stri
return strings.Join(lines, "\n") + "\n", nil
}

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

updatedChangelog, err := addPullRequest(changelog, pr, itemTemplate)
updatedChangelog, err := addPullRequest(changelog, pr, entryTemplate)
if err != nil {
log.Fatalf("error adding pull request: %s\n", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestFuckingChangelog(t *testing.T) {
func TestAddPullRequest(t *testing.T) {
defaultItemTemplate := `* [PR-{{ .Number }}]({{ .Url }})
{{ .Title }}`
testCases := []struct {
Expand Down
2 changes: 1 addition & 1 deletion changelog/release.go → changelog/add_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func updateReleaseChangelog(changelog string, release string) (string, error) {
return strings.Join(lines, "\n") + "\n", nil
}

func Release(release string, base string, name string, commit bool) {
func AddRelease(release string, base string, name string, commit bool) {
b, err := os.ReadFile(name)
if err != nil {
log.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRelease(t *testing.T) {
func TestAddRelease(t *testing.T) {
testCases := []struct {
changelog string
release string
Expand Down
54 changes: 0 additions & 54 deletions cmd/changelog.go

This file was deleted.

37 changes: 37 additions & 0 deletions cmd/changelog/add_pull_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package changelog

import (
"log"

"github.com/rimi-itk/gh-itkdev/changelog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
entryTemplate = `* [PR-{{ .Number }}]({{ .Url }})
{{ .Title }}`
addPullRequestCmd = &cobra.Command{
Use: "add-pull-request",
Short: "Add missing pull request entry to changelog",
Run: func(cmd *cobra.Command, args []string) {
log.Printf("\n\nadd-pull-request: viper: %q\nentryTemplate: %q\n\n", viper.AllSettings(), entryTemplate)
log.Println(viper.GetString("changelog.add-pull-request.entry-template"))
changelog.AddPullRequest(changelogName, entryTemplate)
},
}
)

func init() {
log.Println("add-pull-request")

viperPrefix := "changelog.add-pull-request."
flagName := "entry-template"
addPullRequestCmd.Flags().StringVarP(&entryTemplate, flagName, "", entryTemplate, "pull request entry template")
viper.BindPFlag(viperPrefix+flagName, addPullRequestCmd.Flags().Lookup(flagName))

ChangelogCmd.AddCommand(addPullRequestCmd)
}
47 changes: 47 additions & 0 deletions cmd/changelog/add_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package changelog

import (
"github.com/rimi-itk/gh-itkdev/changelog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// createCmd represents the create command
var (
base = "develop"
commit = false

addReleaseCmd = &cobra.Command{
Use: "add-release",
Short: "Create a release branch and update the changelog",
Long: `Create a release branch named release/«release» and update the changelog as per https://keepachangelog.com/en/1.1.0/

Examples:

gh itkdev changelog add-release 0.1.0
gh itkdev changelog add-release v0.1.0
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
release := args[0]

changelog.AddRelease(release, base, changelogName, commit)
},
}
)

func init() {
viperPrefix := "changelog.add-release."
flagName := "base"
addReleaseCmd.Flags().StringVarP(&base, flagName, "", base, "base branch for release")
viper.BindPFlag(viperPrefix+flagName, addReleaseCmd.Flags().Lookup(flagName))

flagName = "commit"
addReleaseCmd.Flags().BoolVarP(&commit, flagName, "", commit, "commit changes")
viper.BindPFlag(viperPrefix+flagName, addReleaseCmd.Flags().Lookup(flagName))

ChangelogCmd.AddCommand(addReleaseCmd)
}
19 changes: 19 additions & 0 deletions cmd/changelog/changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package changelog

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
changelogName = "CHANGELOG.md"
ChangelogCmd = &cobra.Command{
Use: "changelog",
Short: "Update changelog",
}
)

func init() {
ChangelogCmd.PersistentFlags().StringVarP(&changelogName, "changelog", "", changelogName, "changelog name")
viper.BindPFlag("changelog.changelog", ChangelogCmd.PersistentFlags().Lookup("changelog"))
}
23 changes: 23 additions & 0 deletions cmd/changelog/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package changelog

import (
"github.com/rimi-itk/gh-itkdev/changelog"
"github.com/spf13/cobra"
)

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a changelog if it does not exist",
Long: `Create a changelog if it does not exist. The changelog will used the format defined by https://keepachangelog.com/en/1.1.0/.`,
Run: func(cmd *cobra.Command, args []string) {
changelog.Create(changelogName)
},
}

func init() {
ChangelogCmd.AddCommand(createCmd)
}
27 changes: 27 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"gopkg.in/yaml.v2"
)

var configCmd = &cobra.Command{
Use: "config",
Short: "Show active config",
Run: func(cmd *cobra.Command, args []string) {
yaml, err := yaml.Marshal(viper.AllSettings())
if err != nil {
log.Fatal(err)
}

fmt.Println(string(yaml))
},
}
36 changes: 24 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"os"
"fmt"
"log"

"github.com/rimi-itk/gh-itkdev/cmd/changelog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -13,22 +16,31 @@ var rootCmd = &cobra.Command{
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
cobra.CheckErr(err)
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
cobra.OnInitialize(initConfig)

rootCmd.AddCommand(changelog.ChangelogCmd)
rootCmd.AddCommand(configCmd)
}

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gh-itkdev.yaml)")
func initConfig() {
viper.SetConfigName(".gh-itkdev")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("Unable to read config file %s: %s\n", viper.ConfigFileUsed(), err)
}
} else {
fmt.Printf("Using config file %s\n", viper.ConfigFileUsed())
}
}
Loading
Loading