Remove cmd input

Signed-off-by: Joel Longtine <joel@dagger.io>
This commit is contained in:
Joel Longtine 2022-02-18 14:46:10 -07:00
parent cfbd70a823
commit 16733fa9eb
12 changed files with 0 additions and 676 deletions

View File

@ -1,38 +0,0 @@
package input
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
)
var boolCmd = &cobra.Command{
Use: "bool <TARGET> <true|false>",
Short: "Add a boolean input",
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
updateEnvironmentInput(
ctx,
cmd,
args[0],
state.BoolInput(readInput(ctx, args[1])),
)
},
}
func init() {
if err := viper.BindPFlags(boolCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -1,69 +0,0 @@
package input
import (
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
)
var dirCmd = &cobra.Command{
Use: "dir TARGET PATH",
Short: "Add a local directory as input artifact",
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
p, err := filepath.Abs(args[1])
if err != nil {
lg.Fatal().Err(err).Str("path", args[1]).Msg("unable to resolve path")
}
// Check that directory exists
if _, err := os.Stat(p); os.IsNotExist(err) {
lg.Fatal().Err(err).Str("path", args[1]).Msg("dir doesn't exist")
}
project := common.CurrentProject(ctx)
if !strings.HasPrefix(p, project.Path) {
lg.Fatal().Err(err).Str("path", args[1]).Msg("dir is outside the project")
}
p, err = filepath.Rel(project.Path, p)
if err != nil {
lg.Fatal().Err(err).Str("path", args[1]).Msg("unable to resolve path")
}
if !strings.HasPrefix(p, ".") {
p = "./" + p
}
updateEnvironmentInput(ctx, cmd, args[0],
state.DirInput(
p,
viper.GetStringSlice("include"),
viper.GetStringSlice("exclude"),
),
)
},
}
func init() {
dirCmd.Flags().StringSlice("include", []string{}, "Include pattern")
dirCmd.Flags().StringSlice("exclude", []string{}, "Exclude pattern")
if err := viper.BindPFlags(dirCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -1,43 +0,0 @@
package input
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
)
var gitCmd = &cobra.Command{
Use: "git TARGET REMOTE [REF] [SUBDIR]",
Short: "Add a git repository as input artifact",
Args: cobra.RangeArgs(2, 4),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
ref := "HEAD"
if len(args) > 2 {
ref = args[2]
}
subDir := ""
if len(args) > 3 {
subDir = args[3]
}
updateEnvironmentInput(ctx, cmd, args[0], state.GitInput(args[1], ref, subDir))
},
}
func init() {
if err := viper.BindPFlags(gitCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -1,40 +0,0 @@
package input
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
)
var jsonCmd = &cobra.Command{
Use: "json <TARGET> [-f] <VALUE|PATH>",
Short: "Add a JSON input",
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
updateEnvironmentInput(
ctx,
cmd,
args[0],
state.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)
}
}

View File

@ -1,131 +0,0 @@
package input
import (
"context"
"fmt"
"os"
"text/tabwriter"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var listCmd = &cobra.Command{
Use: "list [TARGET] [flags]",
Short: "List the inputs of an environment",
Args: cobra.MaximumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
project := common.CurrentProject(ctx)
st := common.CurrentEnvironmentState(ctx, project)
lg = lg.With().
Str("environment", st.Name).
Logger()
doneCh := common.TrackProjectCommand(ctx, cmd, project, st)
env, err := environment.New(st)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create environment")
}
cl := common.NewClient(ctx)
err = cl.Do(ctx, env.Context(), func(ctx context.Context, s solver.Solver) error {
inputs, err := env.ScanInputs(ctx, false)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "Input\tValue\tSet by user\tDescription")
for _, inp := range inputs {
isConcrete := (inp.IsConcreteR() == nil)
_, hasDefault := inp.Default()
switch {
case plancontext.IsSecretValue(inp):
if _, err := env.Context().Secrets.FromValue(inp); err != nil {
isConcrete = false
}
case plancontext.IsFSValue(inp):
if _, err := env.Context().FS.FromValue(inp); err != nil {
isConcrete = false
}
case plancontext.IsServiceValue(inp):
if _, err := env.Context().Services.FromValue(inp); err != nil {
isConcrete = false
}
}
if !viper.GetBool("all") {
// skip input that is not overridable
if !hasDefault && isConcrete {
continue
}
}
if !viper.GetBool("show-optional") && !viper.GetBool("all") {
// skip input if there is already a default value
if hasDefault {
continue
}
}
fmt.Fprintf(w, "%s\t%s\t%t\t%s\n",
inp.Path(),
common.FormatValue(inp),
isUserSet(st, inp),
common.ValueDocOneLine(inp),
)
}
w.Flush()
return nil
})
<-doneCh
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}
},
}
func isUserSet(env *state.State, val *compiler.Value) bool {
for key := range env.Inputs {
if val.Path().String() == key {
return true
}
}
return false
}
func init() {
listCmd.Flags().BoolP("all", "a", false, "List all inputs (include non-overridable)")
listCmd.Flags().Bool("show-optional", false, "List optional inputs (those with default values)")
if err := viper.BindPFlags(listCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -1,112 +0,0 @@
package input
import (
"context"
"io"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"go.dagger.io/dagger/telemetry"
)
// Cmd exposes the top-level command
var Cmd = &cobra.Command{
Use: "input",
Short: "Manage an environment's inputs",
}
func init() {
Cmd.AddCommand(
dirCmd,
gitCmd,
secretCmd,
textCmd,
jsonCmd,
yamlCmd,
listCmd,
boolCmd,
socketCmd,
unsetCmd,
)
}
func updateEnvironmentInput(ctx context.Context, cmd *cobra.Command, target string, input state.Input) {
lg := *log.Ctx(ctx)
project := common.CurrentProject(ctx)
st := common.CurrentEnvironmentState(ctx, project)
lg = lg.With().
Str("environment", st.Name).
Logger()
doneCh := common.TrackProjectCommand(ctx, cmd, project, st, &telemetry.Property{
Name: "input_target",
Value: target,
})
st.SetInput(target, input)
env, err := environment.New(st)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create environment")
}
cl := common.NewClient(ctx)
err = cl.Do(ctx, env.Context(), func(ctx context.Context, s solver.Solver) error {
// the inputs are set, check for cue errors by scanning all the inputs
_, err := env.ScanInputs(ctx, true)
if err != nil {
return err
}
return nil
})
<-doneCh
if err != nil {
lg.Fatal().Err(err).Msg("invalid input")
}
if err := project.Save(ctx, st); err != nil {
lg.Fatal().Err(err).Msg("cannot update environment")
}
}
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)
}

View File

@ -1,59 +0,0 @@
package input
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
"golang.org/x/term"
)
var secretCmd = &cobra.Command{
Use: "secret <TARGET> [-f] [<VALUE|PATH>]",
Short: "Add an encrypted input secret",
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
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
var secret string
if len(args) == 1 {
// No value specified: prompt terminal
fmt.Print("Secret: ")
data, err := term.ReadPassword(int(os.Stdin.Fd()))
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,
cmd,
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)
}
}

View File

@ -1,68 +0,0 @@
package input
import (
"context"
"os"
"runtime"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
)
var socketCmd = &cobra.Command{
Use: "socket <TARGET> <UNIX path>",
Short: "Add a socket input",
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
updateEnvironmentInput(
ctx,
cmd,
args[0],
state.SocketInput(detectStreamType(ctx, args[1])),
)
},
}
func detectStreamType(ctx context.Context, path string) (string, string) {
lg := log.Ctx(ctx)
if runtime.GOOS == "windows" {
// support the unix format for convenience
if path == "/var/run/docker.sock" || path == "\\var\\run\\docker.sock" {
path = "\\\\.\\pipe\\docker_engine"
lg.Info().Str("path", path).Msg("Windows detected, override unix socket path")
}
return path, "npipe"
}
st, err := os.Stat(path)
if err != nil {
lg.Fatal().Err(err).Str("path", path).Msg("invalid unix socket")
}
if st.Mode()&os.ModeSocket == 0 {
lg.Fatal().Str("path", path).Msg("not a unix socket")
}
return path, "unix"
}
func init() {
if err := viper.BindPFlags(boolCmd.Flags()); err != nil {
panic(err)
}
}

View File

@ -1,40 +0,0 @@
package input
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
)
var textCmd = &cobra.Command{
Use: "text <TARGET> [-f] <VALUE|PATH>",
Short: "Add a text input",
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
updateEnvironmentInput(
ctx,
cmd,
args[0],
state.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)
}
}

View File

@ -1,34 +0,0 @@
package input
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
)
var unsetCmd = &cobra.Command{
Use: "unset [TARGET]",
Short: "Remove input of an environment",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
project := common.CurrentProject(ctx)
st := common.CurrentEnvironmentState(ctx, project)
st.RemoveInputs(args[0])
if err := project.Save(ctx, st); err != nil {
lg.Fatal().Err(err).Str("environment", st.Name).Msg("cannot update environment")
}
lg.Info().Str("environment", st.Name).Msg("updated environment")
},
}

View File

@ -1,40 +0,0 @@
package input
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state"
)
var yamlCmd = &cobra.Command{
Use: "yaml <TARGET> [-f] <VALUE|PATH>",
Short: "Add a YAML input",
Args: cobra.ExactArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233
if err := viper.BindPFlags(cmd.Flags()); err != nil {
panic(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
updateEnvironmentInput(
ctx,
cmd,
args[0],
state.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)
}
}

View File

@ -7,7 +7,6 @@ import (
"github.com/moby/buildkit/util/appcontext"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/input"
"go.dagger.io/dagger/cmd/dagger/cmd/mod"
"go.dagger.io/dagger/cmd/dagger/cmd/output"
"go.dagger.io/dagger/cmd/dagger/logger"
@ -64,7 +63,6 @@ func init() {
historyCmd,
loginCmd,
logoutCmd,
input.Cmd,
output.Cmd,
versionCmd,
docCmd,