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).
33 lines
683 B
Go
33 lines
683 B
Go
package gok
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/gokrazy/tools/internal/version"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// versionCmd is gok version.
|
|
func versionCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "version",
|
|
Short: "Print gok version",
|
|
Long: `Print gok version`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return versionImpl.run(cmd.Context(), args, cmd.OutOrStdout(), cmd.OutOrStderr())
|
|
},
|
|
}
|
|
return cmd
|
|
}
|
|
|
|
type versionImplConfig struct{}
|
|
|
|
var versionImpl versionImplConfig
|
|
|
|
func (r *versionImplConfig) run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
|
|
fmt.Fprintf(stdout, "%s\n", version.Read())
|
|
return nil
|
|
}
|