From a3f91023a15128ee5e434df10560b33945485809 Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Fri, 18 Feb 2022 14:53:13 -0700 Subject: [PATCH] Remove cmd edit Signed-off-by: Joel Longtine --- cmd/dagger/cmd/edit.go | 146 ----------------------------------------- cmd/dagger/cmd/root.go | 1 - 2 files changed, 147 deletions(-) delete mode 100644 cmd/dagger/cmd/edit.go diff --git a/cmd/dagger/cmd/edit.go b/cmd/dagger/cmd/edit.go deleted file mode 100644 index a80a2343..00000000 --- a/cmd/dagger/cmd/edit.go +++ /dev/null @@ -1,146 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "os" - "os/exec" - "strings" - - "go.dagger.io/dagger/cmd/dagger/cmd/common" - "go.dagger.io/dagger/cmd/dagger/logger" - "go.dagger.io/dagger/environment" - "go.dagger.io/dagger/solver" - "go.dagger.io/dagger/state" - - "github.com/google/shlex" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "gopkg.in/yaml.v3" -) - -var editCmd = &cobra.Command{ - Use: "edit", - Short: "Interactively edit 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) - - data, err := yaml.Marshal(st) - if err != nil { - lg.Fatal().Err(err).Msg("unable to marshal state") - } - - f, err := os.CreateTemp("", fmt.Sprintf("%s-*.yaml", st.Name)) - if err != nil { - lg.Fatal().Err(err).Msg("failed to create temporary file") - } - tmpPath := f.Name() - defer os.Remove(tmpPath) - - if _, err := f.Write(data); err != nil { - lg.Fatal().Err(err).Msg("unable to write file") - } - f.Close() - - if err := runEditor(ctx, tmpPath); err != nil { - lg.Fatal().Err(err).Msg("failed to start editor") - } - - data, err = os.ReadFile(tmpPath) - if err != nil { - lg.Fatal().Err(err).Msg("failed to read temporary file") - } - var newState state.State - if err := yaml.Unmarshal(data, &newState); err != nil { - lg.Fatal().Err(err).Msg("failed to decode file") - } - st.Name = newState.Name - st.Platform = newState.Platform - st.Plan = newState.Plan - st.Inputs = newState.Inputs - - 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 { - // 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).Str("environment", st.Name).Msg("invalid input") - } - - if err := project.Save(ctx, st); err != nil { - lg.Fatal().Err(err).Msg("failed to save state") - } - }, -} - -func runEditor(ctx context.Context, path string) error { - editor := os.Getenv("EDITOR") - var cmd *exec.Cmd - if editor == "" { - editor, err := lookupAnyEditor("vim", "nano", "vi") - if err != nil { - return err - } - cmd = exec.CommandContext(ctx, editor, path) - } else { - parts, err := shlex.Split(editor) - if err != nil { - return fmt.Errorf("invalid $EDITOR: %s", editor) - } - parts = append(parts, path) - cmd = exec.CommandContext(ctx, parts[0], parts[1:]...) // #nosec - } - - cmd.Env = os.Environ() - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} - -func lookupAnyEditor(editorNames ...string) (editorPath string, err error) { - for _, editorName := range editorNames { - editorPath, err = exec.LookPath(editorName) - if err == nil { - return editorPath, nil - } - } - return "", fmt.Errorf("no editor available: dagger attempts to use the editor defined in the EDITOR environment variable, and if that's not set defaults to any of %s, but none of them could be found", strings.Join(editorNames, ", ")) -} - -func init() { - if err := viper.BindPFlags(editCmd.Flags()); err != nil { - panic(err) - } -} diff --git a/cmd/dagger/cmd/root.go b/cmd/dagger/cmd/root.go index 357a56c0..90d42a57 100644 --- a/cmd/dagger/cmd/root.go +++ b/cmd/dagger/cmd/root.go @@ -56,7 +56,6 @@ func init() { listCmd, upCmd, downCmd, - editCmd, loginCmd, logoutCmd, versionCmd,