Pass global flags down to subcommands (#54)

Fix #51

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/changelog/pulls/54
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Co-authored-by: Andrew Thornton <art27@cantab.net>
Co-committed-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
Andrew Thornton 2022-09-16 17:31:37 +08:00 committed by Norwin
parent 37ed264d72
commit 3b83ddcefb
3 changed files with 41 additions and 8 deletions

5
.gitignore vendored
View File

@ -3,4 +3,7 @@
# Binaries # Binaries
/changelog /changelog
/changelog.exe /changelog.exe
# Go
/vendor/

View File

@ -1,4 +1,5 @@
# Changelog # Changelog
A changelog generator for Gitea A changelog generator for Gitea
[![Build Status](https://drone.gitea.com/api/badges/gitea/changelog/status.svg)](https://drone.gitea.com/gitea/changelog) [![Build Status](https://drone.gitea.com/api/badges/gitea/changelog/status.svg)](https://drone.gitea.com/gitea/changelog)
@ -19,18 +20,21 @@ See the [changelog.example.yml](config/changelog.example.yml) example file.
## Usage ## Usage
#### Changelog Entries ### Changelog Entries
```
changelog -m=1.11.0 -c=/path/to/my_config_file generate ```sh
changelog generate -m=1.11.0 -c=/path/to/my_config_file
``` ```
#### Contributors List ### Contributors List
```
changelog -m=1.11.0 -c=/path/to/my_config_file contributors ```sh
changelog contributors -m=1.11.0 -c=/path/to/my_config_file
``` ```
## Building ## Building
```
```sh
go build go build
``` ```

View File

@ -80,6 +80,7 @@ func New() *cli.App {
Init, Init,
}, },
} }
copyGlobalFlags(app)
return app return app
} }
@ -95,3 +96,28 @@ func getDefaultConfigFile() string {
} }
return "" return ""
} }
func copyGlobalFlags(app *cli.App) {
for _, command := range app.Commands {
originalFlagNames := make([]string, 0, len(command.Flags))
for _, flag := range command.Flags {
originalFlagNames = append(originalFlagNames, flag.Names()...)
}
for _, flag := range app.Flags {
found := false
flagNameLoop:
for _, name := range flag.Names() {
for _, originalName := range originalFlagNames {
if name == originalName {
found = true
break flagNameLoop
}
}
}
if !found {
command.Flags = append(command.Flags, flag)
}
}
}
}