secret input type, simplify state format

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-05-17 11:12:46 -07:00
parent 5442d6b261
commit 9d416d65f7
5 changed files with 83 additions and 91 deletions

View File

@@ -10,6 +10,7 @@ import (
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger"
"dagger.io/go/dagger/compiler"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -84,9 +85,9 @@ var listCmd = &cobra.Command{
},
}
func isUserSet(env *dagger.EnvironmentState, val *compiler.Value) bool {
for _, i := range env.Inputs {
if val.Path().String() == i.Key {
func isUserSet(env *state.State, val *compiler.Value) bool {
for key := range env.Inputs {
if val.Path().String() == key {
return true
}
}

View File

@@ -1,14 +1,20 @@
package input
import (
"fmt"
"syscall"
"dagger.io/go/cmd/dagger/logger"
"dagger.io/go/dagger/state"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
)
var secretCmd = &cobra.Command{
Use: "secret TARGET VALUE",
Use: "secret <TARGET> [-f] [<VALUE|PATH>]",
Short: "Add an encrypted input secret",
Args: cobra.ExactArgs(2),
Args: cobra.RangeArgs(1, 2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
@@ -17,14 +23,35 @@ var secretCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
// lg := logger.New()
// ctx := lg.WithContext(cmd.Context())
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
panic("not implemented")
var secret string
if len(args) == 1 {
// No value specified: prompt terminal
fmt.Print("Secret: ")
data, err := term.ReadPassword(syscall.Stdin)
if err != nil {
lg.Fatal().Err(err).Msg("unable to read secret from terminal")
}
fmt.Println("")
secret = string(data)
} else {
// value specified: read it
secret = readInput(ctx, args[1])
}
updateEnvironmentInput(
ctx,
args[0],
state.SecretInput(secret),
)
},
}
func init() {
secretCmd.Flags().BoolP("file", "f", false, "Read value from file")
if err := viper.BindPFlags(secretCmd.Flags()); err != nil {
panic(err)
}