diff --git a/cmd/dagger/cmd/input/bool.go b/cmd/dagger/cmd/input/bool.go deleted file mode 100644 index 0dd1a691..00000000 --- a/cmd/dagger/cmd/input/bool.go +++ /dev/null @@ -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 ", - 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) - } -} diff --git a/cmd/dagger/cmd/input/dir.go b/cmd/dagger/cmd/input/dir.go deleted file mode 100644 index 7af9cbb7..00000000 --- a/cmd/dagger/cmd/input/dir.go +++ /dev/null @@ -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) - } -} diff --git a/cmd/dagger/cmd/input/git.go b/cmd/dagger/cmd/input/git.go deleted file mode 100644 index 5cddefa0..00000000 --- a/cmd/dagger/cmd/input/git.go +++ /dev/null @@ -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) - } -} diff --git a/cmd/dagger/cmd/input/json.go b/cmd/dagger/cmd/input/json.go deleted file mode 100644 index f5c47325..00000000 --- a/cmd/dagger/cmd/input/json.go +++ /dev/null @@ -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 [-f] ", - 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) - } -} diff --git a/cmd/dagger/cmd/input/list.go b/cmd/dagger/cmd/input/list.go deleted file mode 100644 index 8589db02..00000000 --- a/cmd/dagger/cmd/input/list.go +++ /dev/null @@ -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) - } -} diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go deleted file mode 100644 index 57b1a785..00000000 --- a/cmd/dagger/cmd/input/root.go +++ /dev/null @@ -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) -} diff --git a/cmd/dagger/cmd/input/secret.go b/cmd/dagger/cmd/input/secret.go deleted file mode 100644 index b33a4230..00000000 --- a/cmd/dagger/cmd/input/secret.go +++ /dev/null @@ -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 [-f] []", - 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) - } -} diff --git a/cmd/dagger/cmd/input/socket.go b/cmd/dagger/cmd/input/socket.go deleted file mode 100644 index 3a6d6801..00000000 --- a/cmd/dagger/cmd/input/socket.go +++ /dev/null @@ -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 ", - 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) - } -} diff --git a/cmd/dagger/cmd/input/text.go b/cmd/dagger/cmd/input/text.go deleted file mode 100644 index bfe63cd8..00000000 --- a/cmd/dagger/cmd/input/text.go +++ /dev/null @@ -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 [-f] ", - 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) - } -} diff --git a/cmd/dagger/cmd/input/unset.go b/cmd/dagger/cmd/input/unset.go deleted file mode 100644 index 26287ba5..00000000 --- a/cmd/dagger/cmd/input/unset.go +++ /dev/null @@ -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") - }, -} diff --git a/cmd/dagger/cmd/input/yaml.go b/cmd/dagger/cmd/input/yaml.go deleted file mode 100644 index a4204eaa..00000000 --- a/cmd/dagger/cmd/input/yaml.go +++ /dev/null @@ -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 [-f] ", - 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) - } -} diff --git a/cmd/dagger/cmd/root.go b/cmd/dagger/cmd/root.go index 13933ea0..7f3edecd 100644 --- a/cmd/dagger/cmd/root.go +++ b/cmd/dagger/cmd/root.go @@ -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,