Skip to content
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

fix: Improve flag value resets between command executions #3

Merged
merged 2 commits into from
Nov 24, 2023
Merged
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
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.2] - 2023-11-24

### Fixed
* fixed flag reset behaviour for slice/array flags by using casting to `SliceValue` and using `SliceValue.Reset()` by @avirtopeanu-ionos in https://github.com/ionoscloudsdk/comptplus/pull/3
* In v1.0.1 and before, including in the original cobra-prompt repository, the defaults would be appended to the values of the previous execution

## [1.0.1] - 2023-11-23

### Added
* added the option to set custom flag reset behaviours by @avirtopeanu-ionos in https://github.com/ionoscloudsdk/comptplus/pull/2

## [1.0.0] - 2023-11-22

### Added
* added completions for flag values. by @avirtopeanu-ionos in https://github.com/ionoscloudsdk/comptplus/pull/1
* default cache duration for responses set to 500ms - to prevent laggy user interaction
* support flag descriptions by splitting on `\t`
* added `HookBefore` and `HookAfter` for additional actions before and after command execution.

### Changed
* `PersistFlagValues` behavior:
* instead of adding a flag, setting PersistFlagValues to true will directly influence persistance throughout the entire shell session.
* instead of resetting flags to their default value every time a new character is typed, flag defaults are set after a command execution.

## [0.5.0] - 2023-01-28

### Added
Expand Down
22 changes: 21 additions & 1 deletion comptplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,27 @@ func (co *CobraPrompt) RunContext(ctx context.Context) {

if co.CustomFlagResetBehaviour == nil {
co.CustomFlagResetBehaviour = func(flag *pflag.Flag) {
flag.Value.Set(flag.DefValue)
sliceValue, ok := flag.Value.(pflag.SliceValue)
if !ok {
// For non-slice flags, just set to the default value
flag.Value.Set(flag.DefValue)
return
}

defValue := strings.Trim(flag.DefValue, "[]")
defaultSlice := strings.Split(defValue, ",")
err := sliceValue.Replace(defaultSlice)

if err != nil {
// If there's an error parsing defaultSlice as a slice, try this workaround
errShouldNeverHappenButWeAreProfessionals := sliceValue.Replace([]string{})
if errShouldNeverHappenButWeAreProfessionals == nil {
// If this check wouldn't exist and we would have some error parsing the nil value,
// it would actually append the default value to the previous user's value
flag.Value.Set(flag.DefValue)
}
return
}
}
}

Expand Down