Skip to content

Replace flags usage with pflags #1270

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

Merged
merged 8 commits into from
Mar 31, 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
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
)

var (
// Deprecated key --> deprecation message (i.e. which key replaces it)
deprecatedKeys = map[string]string{}

errInvalidStakerWeights = errors.New("staking weights must be positive")
Expand Down
4 changes: 1 addition & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,8 @@ func setupFile(t *testing.T, path string, fileName string, value string) {
func setupViperFlags() *viper.Viper {
v := viper.New()
fs := BuildFlagSet()
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.PanicOnError) // flags are now reset
pflag.CommandLine.AddGoFlagSet(fs)
pflag.Parse()
if err := v.BindPFlags(pflag.CommandLine); err != nil {
if err := v.BindPFlags(fs); err != nil {
log.Fatal(err)
}
return v
Expand Down
21 changes: 14 additions & 7 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
package config

import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"time"

"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/ava-labs/avalanchego/database/leveldb"
Expand Down Expand Up @@ -50,12 +50,21 @@ var (
defaultChainDataDir = filepath.Join(defaultUnexpandedDataDir, "chainData")
)

func addProcessFlags(fs *flag.FlagSet) {
func deprecateFlags(fs *pflag.FlagSet) error {
for key, message := range deprecatedKeys {
if err := fs.MarkDeprecated(key, message); err != nil {
return err
}
}
return nil
}

func addProcessFlags(fs *pflag.FlagSet) {
// If true, print the version and quit.
fs.Bool(VersionKey, false, "If true, print version and quit")
}

func addNodeFlags(fs *flag.FlagSet) {
func addNodeFlags(fs *pflag.FlagSet) {
// Home directory
fs.String(DataDirKey, defaultDataDir, "Sets the base data directory where default sub-directories will be placed unless otherwise specified.")
// System
Expand Down Expand Up @@ -367,10 +376,8 @@ func addNodeFlags(fs *flag.FlagSet) {
}

// BuildFlagSet returns a complete set of flags for avalanchego
func BuildFlagSet() *flag.FlagSet {
// TODO parse directly into a *pflag.FlagSet instead of into a *flag.FlagSet
// and then putting those into a *plag.FlagSet
fs := flag.NewFlagSet(constants.AppName, flag.ContinueOnError)
func BuildFlagSet() *pflag.FlagSet {
fs := pflag.NewFlagSet(constants.AppName, pflag.ContinueOnError)
addProcessFlags(fs)
addNodeFlags(fs)
return fs
Expand Down
31 changes: 0 additions & 31 deletions config/pflags.go

This file was deleted.

14 changes: 7 additions & 7 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ package config
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"io"
"os"
"strings"

"github.com/spf13/pflag"
"github.com/spf13/viper"
)

// BuildViper returns the viper environment from parsing config file from
// default search paths and any parsed command line flags
func BuildViper(fs *flag.FlagSet, args []string) (*viper.Viper, error) {
pfs, err := buildPFlagSet(fs)
if err != nil {
func BuildViper(fs *pflag.FlagSet, args []string) (*viper.Viper, error) {
if err := deprecateFlags(fs); err != nil {
return nil, err
}
if err := pfs.Parse(args); err != nil {
if err := fs.Parse(args); err != nil {
return nil, err
}

v := viper.New()
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.SetEnvPrefix("avago")
if err := v.BindPFlags(pfs); err != nil {
if err := v.BindPFlags(fs); err != nil {
return nil, err
}

Expand All @@ -56,7 +56,7 @@ func BuildViper(fs *flag.FlagSet, args []string) (*viper.Viper, error) {
}

// Config deprecations must be after v.ReadInConfig
deprecateConfigs(v, fs.Output())
deprecateConfigs(v, os.Stdout)
return v, nil
}

Expand Down