Skip to content

Commit

Permalink
fix persistent flag with generate cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
HandOfGod94 committed Jul 18, 2023
1 parent ff0f2f1 commit 5280c1a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ clean:

install:
go install
gh extension install .
go build -o $(APP_NAME) && gh extension install .

quality-check:
staticcheck ./...
Expand Down
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,22 @@ Feel free to use any of the available methods
gh extension install handofgod94/gh-jira-changelog
```

> :warning: If you are using it as gh cli extension, ensure that you don't have `gh-jira-changelog` binary in PATH var.
> In other words, don't install standalone binary, if you want to use it as `gh` extension.
#### MacOS using `homebrew`
```sh
brew install handofgod94/tap/gh-jira-changelog
```

#### Go Toolchain
```sh
go install github.com/handofgod94/[email protected].3
go install github.com/handofgod94/[email protected].4
```
The go binary will be installed in `$GOPATH/bin`

### Verify installation

`$ gh-jira-changelog version`
```
v0.1.3
v0.1.4
```

### Usage
Expand All @@ -62,8 +59,13 @@ Available Commands:
version Current version of generator
Flags:
--config string config file (default is ./.jira_changelog.yaml)
-h, --help help for gh-jira-changelog
-t, --api_token string API token for jira
-u, --base_url string base url where jira is hosted
--config string config file (default is ./.jira_changelog.yaml)
--email_id string email id of the user
-h, --help help for gh-jira-changelog
-v, --log_level string log level. options: debug, info, warn, error (default "error")
-p, --project_name string Project name in jira. usually the acronym
Use "gh-jira-changelog [command] --help" for more information about a command.
```
Expand Down Expand Up @@ -104,16 +106,16 @@ gh-jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0"
gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0" --to="v0.2.0"
Flags:
--from string Git ref to start from
-h, --help help for generate
--to string Git ref to end at (default "main")
--write_to string File stream to write the changelog (default "/dev/stdout")
Global Flags:
-t, --api_token string API token for jira
-u, --base_url string base url where jira is hosted
--config string config file (default is ./.jira_changelog.yaml)
--email_id string email id of the user
--from string Git ref to start from
-h, --help help for generate
-v, --log_level string log level. options: debug, info, warn, error (default "error")
-p, --project_name string Project name in jira. usually the acronym
--to string Git ref to end at (default "main")
--write_to string File stream to write the changelog (default "/dev/stdout")
Global Flags:
--config string config file (default is ./.jira_changelog.yaml)
```
18 changes: 9 additions & 9 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"io"
"net/url"
"os"
"strings"
"time"

"github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog"
"github.com/handofgod94/gh-jira-changelog/pkg/jira_changelog/jira"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/exp/slog"
Expand All @@ -20,6 +22,7 @@ var (
toRef string
writeTo string
DefaultTimeout = 5 * time.Second
requiredFlags = []string{"base_url", "email_id", "api_token", "project_name"}
)

var generateCmd = &cobra.Command{
Expand Down Expand Up @@ -50,6 +53,12 @@ gh-jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0"
# assuming jira plugin installed
gh jira-changelog generate --config="<path-to-config-file>.yaml" --from="v0.1.0" --to="v0.2.0"`,
PreRunE: func(cmd *cobra.Command, args []string) error {
unsetFlags := lo.Filter(requiredFlags, func(flag string, index int) bool { return !viper.IsSet(flag) })
if len(unsetFlags) > 0 {
unsetFlagsStr := strings.Join(unsetFlags, ", ")
return fmt.Errorf(`required flag "%s" not set`, unsetFlagsStr)
}

apiToken := viper.GetString("api_token")
emailID := viper.GetString("email_id")
if apiToken != "" && emailID == "" {
Expand Down Expand Up @@ -105,16 +114,7 @@ func init() {
generateCmd.Flags().StringVar(&toRef, "to", "main", "Git ref to end at")
generateCmd.Flags().StringVar(&writeTo, "write_to", "/dev/stdout", "File stream to write the changelog")

generateCmd.PersistentFlags().StringP("base_url", "u", "", "base url where jira is hosted")
generateCmd.PersistentFlags().String("email_id", "", "email id of the user")
generateCmd.PersistentFlags().StringP("api_token", "t", "", "API token for jira")
generateCmd.PersistentFlags().StringP("project_name", "p", "", "Project name in jira. usually the acronym")
generateCmd.PersistentFlags().StringP("log_level", "v", "error", "log level. options: debug, info, warn, error")

generateCmd.MarkFlagRequired("from")
generateCmd.MarkPersistentFlagRequired("base_url")
generateCmd.MarkPersistentFlagRequired("project_name")

rootCmd.AddCommand(generateCmd)
viper.BindPFlags(generateCmd.PersistentFlags())
}
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./.jira_changelog.yaml)")
rootCmd.PersistentFlags().StringP("base_url", "u", "", "base url where jira is hosted")
rootCmd.PersistentFlags().String("email_id", "", "email id of the user")
rootCmd.PersistentFlags().StringP("api_token", "t", "", "API token for jira")
rootCmd.PersistentFlags().StringP("project_name", "p", "", "Project name in jira. usually the acronym")
rootCmd.PersistentFlags().StringP("log_level", "v", "error", "log level. options: debug, info, warn, error")

viper.BindPFlags(rootCmd.PersistentFlags())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.1.3"
const version = "v0.1.4"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down

0 comments on commit 5280c1a

Please sign in to comment.