-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddf66ea
commit 6649530
Showing
2 changed files
with
707 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/ava-labs/avalanchego/wallet/subnet/primary" | ||
) | ||
|
||
const ( | ||
URIsKey = "uris" | ||
|
||
FlagsName = "workload" | ||
EnvPrefix = "avawl" | ||
) | ||
|
||
var ( | ||
errNoURIs = errors.New("at least one URI must be provided") | ||
errNoArguments = errors.New("no arguments") | ||
) | ||
|
||
type Config struct { | ||
URIs []string | ||
} | ||
|
||
func NewConfig(arguments []string) (*Config, error) { | ||
v, err := parseFlags(arguments) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &Config{ | ||
URIs: v.GetStringSlice(URIsKey), | ||
} | ||
return c, c.Verify() | ||
} | ||
|
||
func (c *Config) Verify() error { | ||
if len(c.URIs) == 0 { | ||
return errNoURIs | ||
} | ||
return nil | ||
} | ||
|
||
func parseFlags(arguments []string) (*viper.Viper, error) { | ||
if len(arguments) == 0 { | ||
return nil, errNoArguments | ||
} | ||
|
||
fs := pflag.NewFlagSet(FlagsName, pflag.ContinueOnError) | ||
fs.StringSlice(URIsKey, []string{primary.LocalAPIURI}, "URIs of nodes that the workload can communicate with") | ||
if err := fs.Parse(arguments[1:]); err != nil { | ||
return nil, fmt.Errorf("failed parsing CLI flags: %w", err) | ||
} | ||
|
||
v := viper.New() | ||
v.AutomaticEnv() | ||
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
v.SetEnvPrefix(EnvPrefix) | ||
if err := v.BindPFlags(fs); err != nil { | ||
return nil, fmt.Errorf("failed binding pflags: %w", err) | ||
} | ||
return v, nil | ||
} |
Oops, something went wrong.