2020-06-28 19:26:29 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2020-06-28 20:09:32 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2020-06-28 19:26:29 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2020-06-28 20:09:32 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
2020-06-28 19:26:29 +03:00
|
|
|
)
|
|
|
|
|
2022-05-26 20:39:13 +03:00
|
|
|
const gitBugNamespace = "git-bug"
|
2022-05-25 14:55:28 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
// Env is the environment of a command
|
|
|
|
type Env struct {
|
2020-06-28 20:09:32 +03:00
|
|
|
repo repository.ClockedRepo
|
|
|
|
backend *cache.RepoCache
|
|
|
|
out out
|
|
|
|
err out
|
2020-06-28 19:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newEnv() *Env {
|
|
|
|
return &Env{
|
|
|
|
repo: nil,
|
|
|
|
out: out{Writer: os.Stdout},
|
|
|
|
err: out{Writer: os.Stderr},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type out struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o out) Printf(format string, a ...interface{}) {
|
|
|
|
_, _ = fmt.Fprintf(o, format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o out) Print(a ...interface{}) {
|
|
|
|
_, _ = fmt.Fprint(o, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o out) Println(a ...interface{}) {
|
|
|
|
_, _ = fmt.Fprintln(o, a...)
|
|
|
|
}
|
2020-06-28 20:09:32 +03:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the current working directory: %q", err)
|
|
|
|
}
|
|
|
|
|
2022-05-26 20:39:13 +03:00
|
|
|
env.repo, err = repository.OpenGoGitRepo(cwd, gitBugNamespace, []repository.ClockLoader{bug.ClockLoader})
|
2020-06-28 20:09:32 +03:00
|
|
|
if err == repository.ErrNotARepo {
|
|
|
|
return fmt.Errorf("%s must be run from within a git repo", rootCommandName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadRepoEnsureUser is the same as loadRepo, but also ensure that the user has configured
|
|
|
|
// an identity. Use this pre-run function when an error after using the configured user won't
|
|
|
|
// do.
|
|
|
|
func loadRepoEnsureUser(env *Env) func(*cobra.Command, []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
err := loadRepo(env)(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = identity.GetUserIdentity(env.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadBackend is a pre-run function that load the repository and the backend for use in a command
|
|
|
|
// When using this function you also need to use closeBackend as a post-run
|
|
|
|
func loadBackend(env *Env) func(*cobra.Command, []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
err := loadRepo(env)(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
env.backend, err = cache.NewRepoCache(env.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleaner := func(env *Env) interrupt.CleanerFunc {
|
|
|
|
return func() error {
|
|
|
|
if env.backend != nil {
|
|
|
|
err := env.backend.Close()
|
|
|
|
env.backend = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup properly on interrupt
|
|
|
|
interrupt.RegisterCleaner(cleaner(env))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadBackendEnsureUser is the same as loadBackend, but also ensure that the user has configured
|
|
|
|
// an identity. Use this pre-run function when an error after using the configured user won't
|
|
|
|
// do.
|
|
|
|
func loadBackendEnsureUser(env *Env) func(*cobra.Command, []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2020-07-07 21:15:22 +03:00
|
|
|
err := loadBackend(env)(cmd, args)
|
2020-06-28 20:09:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = identity.GetUserIdentity(env.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:33:20 +03:00
|
|
|
// closeBackend is a wrapper for a RunE function that will close the backend properly
|
2020-06-28 20:09:32 +03:00
|
|
|
// if it has been opened.
|
2021-05-09 12:33:20 +03:00
|
|
|
// This wrapper style is necessary because a Cobra PostE function does not run if RunE return an error.
|
|
|
|
func closeBackend(env *Env, runE func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error {
|
2020-06-28 20:09:32 +03:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2021-05-09 12:33:20 +03:00
|
|
|
errRun := runE(cmd, args)
|
|
|
|
|
2020-06-28 20:09:32 +03:00
|
|
|
if env.backend == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := env.backend.Close()
|
|
|
|
env.backend = nil
|
2021-05-09 12:33:20 +03:00
|
|
|
|
|
|
|
// prioritize the RunE error
|
|
|
|
if errRun != nil {
|
|
|
|
return errRun
|
|
|
|
}
|
2020-06-28 20:09:32 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|