Files
tools/internal/gok/edit.go
Michael Stapelberg 2c805ed001 refactor cobra command initialization to avoid stale state
Before this commit, we held onto *cobra.Command objects,
but that is not actually supported: after the first Execute(),
commands like updateCmd are stuck on the first-ever provided ctx.

Instead, turn command initialization into functions.

I only noticed this when trying to do two 'gok update'
from within the same test, where the fake build timestamp
is injected via the context (the timestamp was always the same).
2025-11-29 10:47:48 +01:00

57 lines
1.3 KiB
Go

package gok
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"syscall"
"github.com/gokrazy/internal/instanceflag"
"github.com/spf13/cobra"
)
// editCmd is gok edit.
func editCmd() *cobra.Command {
cmd := &cobra.Command{
GroupID: "edit",
Use: "edit",
Short: "Edit a gokrazy instance configuration interactively",
Long: `Edit a gokrazy instance configuration interactively.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if cmd.Flags().NArg() > 0 {
fmt.Fprint(os.Stderr, `positional arguments are not supported
`)
return cmd.Usage()
}
return editImpl.run(cmd.Context(), args, cmd.OutOrStdout(), cmd.OutOrStderr())
},
}
instanceflag.RegisterPflags(cmd.Flags())
return cmd
}
type editImplConfig struct{}
var editImpl editImplConfig
func (r *editImplConfig) run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
parentDir := instanceflag.ParentDir()
instance := instanceflag.Instance()
configJSON := filepath.Join(parentDir, instance, "config.json")
editor := os.Getenv("VISUAL")
if editor == "" {
editor = os.Getenv("EDITOR")
}
if editor == "" {
editor = "vi" // most likely available
}
shellCmd := fmt.Sprintf("%s %q", editor, configJSON)
return syscall.Exec("/bin/sh", []string{"/bin/sh", "-c", shellCmd}, os.Environ())
}