46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package go_environment
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
)
|
|
|
|
// EnvironmentString describes a (readonly) string which is
|
|
// defined as an environment variable.
|
|
type EnvironmentString struct {
|
|
Variable string
|
|
}
|
|
|
|
// MarshalJSON returns the JSON encoding of es.
|
|
func (es EnvironmentString) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(es.Variable)
|
|
}
|
|
|
|
// UnmarshalJSON parses the JSON-encoded data and stores the
|
|
// result in es.
|
|
func (es *EnvironmentString) UnmarshalJSON(data []byte) error {
|
|
return json.Unmarshal(data, &es.Variable)
|
|
}
|
|
|
|
// String returns the string representation of es.
|
|
func (es EnvironmentString) String() (value string) {
|
|
return os.Getenv(es.Variable)
|
|
}
|
|
|
|
// Key returns the key of the underlying environment
|
|
// variable of es.
|
|
func (es EnvironmentString) Key() (value string) {
|
|
return es.Variable
|
|
}
|
|
|
|
// String determines the validity of the underlying
|
|
// environment variable of es.
|
|
func (es EnvironmentString) Valid() (valid bool) {
|
|
if es.Variable == "" {
|
|
return false
|
|
}
|
|
var value string
|
|
value, valid = os.LookupEnv(es.Variable)
|
|
return valid && (value != "")
|
|
}
|