input: support file/stdin source for text/json/yaml
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
var jsonCmd = &cobra.Command{
|
||||
Use: "json TARGET VALUE",
|
||||
Use: "json <TARGET> [-f] <VALUE|PATH>",
|
||||
Short: "Add a JSON input",
|
||||
Args: cobra.ExactArgs(2),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
@@ -22,11 +22,17 @@ var jsonCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
updateDeploymentInput(ctx, args[0], dagger.JSONInput(args[1]))
|
||||
updateDeploymentInput(
|
||||
ctx,
|
||||
args[0],
|
||||
dagger.JSONInput(readInput(ctx, args[1])),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
jsonCmd.Flags().BoolP("file", "f", false, "Read value from file")
|
||||
|
||||
if err := viper.BindPFlags(jsonCmd.Flags()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@@ -2,11 +2,14 @@ package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"dagger.io/go/cmd/dagger/cmd/common"
|
||||
"dagger.io/go/dagger"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Cmd exposes the top-level command
|
||||
@@ -43,3 +46,35 @@ func updateDeploymentInput(ctx context.Context, target string, input dagger.Inpu
|
||||
}
|
||||
lg.Info().Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("updated deployment")
|
||||
}
|
||||
|
||||
func readInput(ctx context.Context, source string) string {
|
||||
lg := log.Ctx(ctx)
|
||||
|
||||
if !viper.GetBool("file") {
|
||||
return source
|
||||
}
|
||||
|
||||
if source == "-" {
|
||||
// stdin source
|
||||
data, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
Err(err).
|
||||
Msg("failed to read input from stdin")
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// file source
|
||||
data, err := os.ReadFile(source)
|
||||
if err != nil {
|
||||
lg.
|
||||
Fatal().
|
||||
Err(err).
|
||||
Str("path", source).
|
||||
Msg("failed to read input from file")
|
||||
}
|
||||
|
||||
return string(data)
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
var textCmd = &cobra.Command{
|
||||
Use: "text TARGET VALUE",
|
||||
Use: "text <TARGET> [-f] <VALUE|PATH>",
|
||||
Short: "Add a text input",
|
||||
Args: cobra.ExactArgs(2),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
@@ -22,11 +22,17 @@ var textCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
updateDeploymentInput(ctx, args[0], dagger.TextInput(args[1]))
|
||||
updateDeploymentInput(
|
||||
ctx,
|
||||
args[0],
|
||||
dagger.TextInput(readInput(ctx, args[1])),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
textCmd.Flags().BoolP("file", "f", false, "Read value from file")
|
||||
|
||||
if err := viper.BindPFlags(textCmd.Flags()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
var yamlCmd = &cobra.Command{
|
||||
Use: "yaml TARGET VALUE",
|
||||
Use: "yaml <TARGET> [-f] <VALUE|PATH>",
|
||||
Short: "Add a YAML input",
|
||||
Args: cobra.ExactArgs(2),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
@@ -22,11 +22,17 @@ var yamlCmd = &cobra.Command{
|
||||
lg := logger.New()
|
||||
ctx := lg.WithContext(cmd.Context())
|
||||
|
||||
updateDeploymentInput(ctx, args[0], dagger.YAMLInput(args[1]))
|
||||
updateDeploymentInput(
|
||||
ctx,
|
||||
args[0],
|
||||
dagger.YAMLInput(readInput(ctx, args[1])),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
yamlCmd.Flags().BoolP("file", "f", false, "Read value from file")
|
||||
|
||||
if err := viper.BindPFlags(yamlCmd.Flags()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user