2020-02-18 13:51:49 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-07 04:13:14 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/term"
|
2020-02-18 13:51:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type secrets map[string]string
|
|
|
|
|
|
|
|
func newSecrets(secretList []string) secrets {
|
|
|
|
s := make(map[string]string)
|
|
|
|
for _, secretPair := range secretList {
|
2020-03-03 00:11:46 +08:00
|
|
|
secretPairParts := strings.SplitN(secretPair, "=", 2)
|
2021-01-13 01:54:53 +08:00
|
|
|
secretPairParts[0] = strings.ToUpper(secretPairParts[0])
|
|
|
|
if strings.ToUpper(s[secretPairParts[0]]) == secretPairParts[0] {
|
2021-05-07 04:13:14 +08:00
|
|
|
log.Errorf("Secret %s is already defined (secrets are case insensitive)", secretPairParts[0])
|
2021-01-13 01:54:53 +08:00
|
|
|
}
|
2020-02-18 13:51:49 +08:00
|
|
|
if len(secretPairParts) == 2 {
|
|
|
|
s[secretPairParts[0]] = secretPairParts[1]
|
|
|
|
} else if env, ok := os.LookupEnv(secretPairParts[0]); ok && env != "" {
|
|
|
|
s[secretPairParts[0]] = env
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Provide value for '%s': ", secretPairParts[0])
|
2021-05-07 04:13:14 +08:00
|
|
|
val, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2022-02-26 00:47:49 +08:00
|
|
|
fmt.Println()
|
2020-02-18 13:51:49 +08:00
|
|
|
if err != nil {
|
2021-05-07 04:13:14 +08:00
|
|
|
log.Errorf("failed to read input: %v", err)
|
|
|
|
os.Exit(1)
|
2020-02-18 13:51:49 +08:00
|
|
|
}
|
|
|
|
s[secretPairParts[0]] = string(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s secrets) AsMap() map[string]string {
|
|
|
|
return s
|
|
|
|
}
|