2015-11-19 20:43:54 +01:00
|
|
|
// Copyright (c) 2015-2016 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cfgutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-05-05 21:04:36 +02:00
|
|
|
"github.com/roasbeef/btcutil"
|
2015-11-19 20:43:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// AmountFlag embeds a btcutil.Amount and implements the flags.Marshaler and
|
|
|
|
// Unmarshaler interfaces so it can be used as a config struct field.
|
|
|
|
type AmountFlag struct {
|
|
|
|
btcutil.Amount
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:00:53 +02:00
|
|
|
// NewAmountFlag creates an AmountFlag with a default btcutil.Amount.
|
|
|
|
func NewAmountFlag(defaultValue btcutil.Amount) *AmountFlag {
|
|
|
|
return &AmountFlag{defaultValue}
|
|
|
|
}
|
|
|
|
|
2015-11-19 20:43:54 +01:00
|
|
|
// MarshalFlag satisifes the flags.Marshaler interface.
|
|
|
|
func (a *AmountFlag) MarshalFlag() (string, error) {
|
|
|
|
return a.Amount.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalFlag satisifes the flags.Unmarshaler interface.
|
|
|
|
func (a *AmountFlag) UnmarshalFlag(value string) error {
|
|
|
|
value = strings.TrimSuffix(value, " BTC")
|
|
|
|
valueF64, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
amount, err := btcutil.NewAmount(valueF64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Amount = amount
|
|
|
|
return nil
|
|
|
|
}
|