2020-02-27 04:40:01 -08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-04-14 08:24:27 -07:00
|
|
|
"fmt"
|
2020-06-22 13:31:33 -07:00
|
|
|
"strings"
|
2020-04-14 08:24:27 -07:00
|
|
|
|
2020-07-05 09:56:17 -07:00
|
|
|
"github.com/spf13/cast"
|
2020-03-20 10:14:14 -07:00
|
|
|
|
2020-07-27 10:57:15 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/server/types"
|
2020-03-20 10:14:14 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
2020-07-27 10:57:15 -07:00
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
2020-02-27 04:40:01 -08:00
|
|
|
)
|
|
|
|
|
2020-06-22 13:31:33 -07:00
|
|
|
// GetPruningOptionsFromFlags parses command flags and returns the correct
|
|
|
|
// PruningOptions. If a pruning strategy is provided, that will be parsed and
|
|
|
|
// returned, otherwise, it is assumed custom pruning options are provided.
|
2020-07-27 10:57:15 -07:00
|
|
|
func GetPruningOptionsFromFlags(appOpts types.AppOptions) (storetypes.PruningOptions, error) {
|
2020-07-05 09:56:17 -07:00
|
|
|
strategy := strings.ToLower(cast.ToString(appOpts.Get(FlagPruning)))
|
2020-06-22 13:31:33 -07:00
|
|
|
|
2020-04-14 08:24:27 -07:00
|
|
|
switch strategy {
|
2020-07-27 10:57:15 -07:00
|
|
|
case storetypes.PruningOptionDefault, storetypes.PruningOptionNothing, storetypes.PruningOptionEverything:
|
|
|
|
return storetypes.NewPruningOptionsFromString(strategy), nil
|
2020-02-27 04:40:01 -08:00
|
|
|
|
2020-07-27 10:57:15 -07:00
|
|
|
case storetypes.PruningOptionCustom:
|
|
|
|
opts := storetypes.NewPruningOptions(
|
2020-07-05 09:56:17 -07:00
|
|
|
cast.ToUint64(appOpts.Get(FlagPruningKeepRecent)),
|
|
|
|
cast.ToUint64(appOpts.Get(FlagPruningKeepEvery)),
|
|
|
|
cast.ToUint64(appOpts.Get(FlagPruningInterval)),
|
2020-06-22 13:31:33 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := opts.Validate(); err != nil {
|
|
|
|
return opts, fmt.Errorf("invalid custom pruning options: %w", err)
|
2020-04-14 08:24:27 -07:00
|
|
|
}
|
2020-06-22 13:31:33 -07:00
|
|
|
|
2020-04-14 08:24:27 -07:00
|
|
|
return opts, nil
|
2020-02-27 04:40:01 -08:00
|
|
|
|
2020-04-14 08:24:27 -07:00
|
|
|
default:
|
|
|
|
return store.PruningOptions{}, fmt.Errorf("unknown pruning strategy %s", strategy)
|
|
|
|
}
|
2020-02-27 04:40:01 -08:00
|
|
|
}
|