Skip to content

Commit

Permalink
adding BeforeFlagParseHook() hook (#18)
Browse files Browse the repository at this point in the history
* adding BeforeFlagParseHook() hook so programs can call ChangeFlagsDefault() to change some of the default for flags created by cli/scli

* adding checks

* linter fixes
  • Loading branch information
ldemailly authored Aug 14, 2023
1 parent afbb3ca commit 817b712
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/gochecks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: go-checks

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]

jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master
- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: '1.20'
check-latest: true
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
11 changes: 2 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Config for golanglint-ci
# Config for golangci-lint

# output configuration options

Expand Down Expand Up @@ -45,14 +45,6 @@ linters-settings:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).FErrf
enable-all: true
disable-all: false
depguard:
list-type: blacklist
include-go-root: false
packages:
- github.com/sirupsen/logrus
packages-with-error-message:
# specify an error message to output when a blacklisted package is used
- github.com/sirupsen/logrus: "logging is allowed only by fortio.log"
lll:
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
Expand Down Expand Up @@ -115,6 +107,7 @@ linters:
- cyclop
- forcetypeassert
- ireturn
- depguard
enable-all: true
disable-all: false
# Must not use fast: true in newer golangci-lint or it'll just skip a bunch of linter instead of doing caching like before (!)
Expand Down
24 changes: 23 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,30 @@ var (
ServerMode = false
// Override this to change the exit function (for testing), will be applied to log.Fatalf too.
ExitFunction = os.Exit
baseExe string
// Hook to call before flag.Parse() - for instance to use ChangeFlagDefaults for logger flags etc.
BeforeFlagParseHook = func() {}
// Calculated base exe name from args (will be used if ProgramName if not set).
baseExe string
)

// ChangeFlagsDefault sets some flags to a different default.
// Will return error if the flag is not found and value can't be set
// (caller should likely log.Fatalf if that happens as it's a typo/bug).
func ChangeFlagsDefault(newDefault string, flagNames ...string) error {
for _, flagName := range flagNames {
f := flag.Lookup(flagName)
if f == nil {
return fmt.Errorf("flag %q not found", flagName)
}
f.DefValue = newDefault
err := f.Value.Set(newDefault)
if err != nil {
return err
}
}
return nil
}

func colorJoin(color string, args ...string) string {
return color + strings.Join(args, log.Colors.Reset+"|"+color) + log.Colors.Reset
}
Expand Down Expand Up @@ -149,6 +170,7 @@ func Main() {
}
// In case of a bad flag, we want it in red when on console:
os.Stderr.WriteString(log.Colors.BrightRed)
BeforeFlagParseHook()
flag.Parse()
os.Stderr.WriteString(log.Colors.Reset)
log.Config.ConsoleColor = !*nocolor
Expand Down

0 comments on commit 817b712

Please sign in to comment.