env.go

  1package execenv
  2
  3import (
  4	"fmt"
  5	"io"
  6	"os"
  7
  8	"github.com/spf13/cobra"
  9
 10	"github.com/MichaelMure/git-bug/cache"
 11	"github.com/MichaelMure/git-bug/entities/bug"
 12	"github.com/MichaelMure/git-bug/entities/identity"
 13	"github.com/MichaelMure/git-bug/repository"
 14	"github.com/MichaelMure/git-bug/util/interrupt"
 15)
 16
 17const RootCommandName = "git-bug"
 18
 19const gitBugNamespace = "git-bug"
 20
 21// Env is the environment of a command
 22type Env struct {
 23	Repo    repository.ClockedRepo
 24	Backend *cache.RepoCache
 25	Out     Out
 26	Err     Out
 27}
 28
 29func NewEnv() *Env {
 30	return &Env{
 31		Repo: nil,
 32		Out:  out{Writer: os.Stdout},
 33		Err:  out{Writer: os.Stderr},
 34	}
 35}
 36
 37type Out interface {
 38	io.Writer
 39	Printf(format string, a ...interface{})
 40	Print(a ...interface{})
 41	Println(a ...interface{})
 42
 43	// String returns what have been written in the output before, as a string.
 44	// This only works in test scenario.
 45	String() string
 46	// Bytes returns what have been written in the output before, as []byte.
 47	// This only works in test scenario.
 48	Bytes() []byte
 49	// Reset clear what has been recorded as written in the output before.
 50	// This only works in test scenario.
 51	Reset()
 52}
 53
 54type out struct {
 55	io.Writer
 56}
 57
 58func (o out) Printf(format string, a ...interface{}) {
 59	_, _ = fmt.Fprintf(o, format, a...)
 60}
 61
 62func (o out) Print(a ...interface{}) {
 63	_, _ = fmt.Fprint(o, a...)
 64}
 65
 66func (o out) Println(a ...interface{}) {
 67	_, _ = fmt.Fprintln(o, a...)
 68}
 69
 70func (o out) String() string {
 71	panic("only work with a test env")
 72}
 73
 74func (o out) Bytes() []byte {
 75	panic("only work with a test env")
 76}
 77
 78func (o out) Reset() {
 79	panic("only work with a test env")
 80}
 81
 82// LoadRepo is a pre-run function that load the repository for use in a command
 83func LoadRepo(env *Env) func(*cobra.Command, []string) error {
 84	return func(cmd *cobra.Command, args []string) error {
 85		cwd, err := os.Getwd()
 86		if err != nil {
 87			return fmt.Errorf("unable to get the current working directory: %q", err)
 88		}
 89
 90		env.Repo, err = repository.OpenGoGitRepo(cwd, gitBugNamespace, []repository.ClockLoader{bug.ClockLoader})
 91		if err == repository.ErrNotARepo {
 92			return fmt.Errorf("%s must be run from within a git Repo", RootCommandName)
 93		}
 94
 95		if err != nil {
 96			return err
 97		}
 98
 99		return nil
100	}
101}
102
103// LoadRepoEnsureUser is the same as LoadRepo, but also ensure that the user has configured
104// an identity. Use this pre-run function when an error after using the configured user won't
105// do.
106func LoadRepoEnsureUser(env *Env) func(*cobra.Command, []string) error {
107	return func(cmd *cobra.Command, args []string) error {
108		err := LoadRepo(env)(cmd, args)
109		if err != nil {
110			return err
111		}
112
113		_, err = identity.GetUserIdentity(env.Repo)
114		if err != nil {
115			return err
116		}
117
118		return nil
119	}
120}
121
122// LoadBackend is a pre-run function that load the repository and the Backend for use in a command
123// When using this function you also need to use CloseBackend as a post-run
124func LoadBackend(env *Env) func(*cobra.Command, []string) error {
125	return func(cmd *cobra.Command, args []string) error {
126		err := LoadRepo(env)(cmd, args)
127		if err != nil {
128			return err
129		}
130
131		env.Backend, err = cache.NewRepoCache(env.Repo)
132		if err != nil {
133			return err
134		}
135
136		cleaner := func(env *Env) interrupt.CleanerFunc {
137			return func() error {
138				if env.Backend != nil {
139					err := env.Backend.Close()
140					env.Backend = nil
141					return err
142				}
143				return nil
144			}
145		}
146
147		// Cleanup properly on interrupt
148		interrupt.RegisterCleaner(cleaner(env))
149		return nil
150	}
151}
152
153// LoadBackendEnsureUser is the same as LoadBackend, but also ensure that the user has configured
154// an identity. Use this pre-run function when an error after using the configured user won't
155// do.
156func LoadBackendEnsureUser(env *Env) func(*cobra.Command, []string) error {
157	return func(cmd *cobra.Command, args []string) error {
158		err := LoadBackend(env)(cmd, args)
159		if err != nil {
160			return err
161		}
162
163		_, err = identity.GetUserIdentity(env.Repo)
164		if err != nil {
165			return err
166		}
167
168		return nil
169	}
170}
171
172// CloseBackend is a wrapper for a RunE function that will close the Backend properly
173// if it has been opened.
174// This wrapper style is necessary because a Cobra PostE function does not run if RunE return an error.
175func CloseBackend(env *Env, runE func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error {
176	return func(cmd *cobra.Command, args []string) error {
177		errRun := runE(cmd, args)
178
179		if env.Backend == nil {
180			return nil
181		}
182		err := env.Backend.Close()
183		env.Backend = nil
184
185		// prioritize the RunE error
186		if errRun != nil {
187			return errRun
188		}
189		return err
190	}
191}