mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 04:17:44 +08:00
go-ipfs-config: fix: panic on invalid priority/flag values
This commit is contained in:
parent
38ad98d84d
commit
44e005e577
@ -56,6 +56,8 @@ const (
|
||||
)
|
||||
|
||||
// WithDefault resolves the value of the flag given the provided default value.
|
||||
//
|
||||
// Panics if Flag is an invalid value.
|
||||
func (f Flag) WithDefault(defaultValue bool) bool {
|
||||
switch f {
|
||||
case False:
|
||||
@ -126,17 +128,30 @@ const (
|
||||
|
||||
// WithDefault resolves the priority with the given default.
|
||||
//
|
||||
// If `defaultPriority` is Default/0, this function will return 0.
|
||||
// If defaultPriority is Default/0, this function will return 0.
|
||||
//
|
||||
// Panics if the priority has an invalid value (e.g., not DefaultPriority,
|
||||
// Disabled, or > 0).
|
||||
func (p Priority) WithDefault(defaultPriority Priority) (priority int64, enabled bool) {
|
||||
switch p {
|
||||
case Disabled:
|
||||
return 0, false
|
||||
case DefaultPriority:
|
||||
if defaultPriority < 0 {
|
||||
switch defaultPriority {
|
||||
case Disabled:
|
||||
return 0, false
|
||||
case DefaultPriority:
|
||||
return 0, true
|
||||
default:
|
||||
if defaultPriority <= 0 {
|
||||
panic(fmt.Sprintf("invalid priority %d < 0", int64(defaultPriority)))
|
||||
}
|
||||
return int64(defaultPriority), true
|
||||
}
|
||||
return int64(defaultPriority), true
|
||||
default:
|
||||
if p <= 0 {
|
||||
panic(fmt.Sprintf("invalid priority %d < 0", int64(p)))
|
||||
}
|
||||
return int64(p), true
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user