From 523a1481857e4e4e13c03a1d4d7630db73568694 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Fri, 27 May 2022 13:33:44 -0400 Subject: [PATCH] test(778): allow alternate CWD via context --- commands/env.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/commands/env.go b/commands/env.go index 98a7e1a57e043d21679abca371e5b37350b80471..4d90348f1ea02e96c936ebf7170961a59fe0f5ff 100644 --- a/commands/env.go +++ b/commands/env.go @@ -46,12 +46,26 @@ func (o out) Println(a ...interface{}) { _, _ = fmt.Fprintln(o, a...) } +func getCWD(cmd *cobra.Command, args []string) (string, error) { + cwd, ok := cmd.Context().Value("cwd").(string) + if cwd != "" && ok { + return cwd, nil + } + + cwd, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("unable to get the current working directory: %q", err) + } + + return cwd, nil +} + // loadRepo is a pre-run function that load the repository for use in a command func loadRepo(env *Env) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { - cwd, err := os.Getwd() + cwd, err := getCWD(cmd, args) if err != nil { - return fmt.Errorf("unable to get the current working directory: %q", err) + return err } env.repo, err = repository.OpenGoGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader})