push.go

 1package commands
 2
 3import (
 4	"errors"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/MichaelMure/git-bug/cache"
 9	"github.com/MichaelMure/git-bug/util/interrupt"
10)
11
12func newPushCommand() *cobra.Command {
13	env := newEnv()
14
15	cmd := &cobra.Command{
16		Use:     "push [<remote>]",
17		Short:   "Push bugs update to a git remote.",
18		PreRunE: loadRepo(env),
19		RunE: func(cmd *cobra.Command, args []string) error {
20			return runPush(env, args)
21		},
22	}
23
24	return cmd
25}
26
27func runPush(env *Env, args []string) error {
28	if len(args) > 1 {
29		return errors.New("Only pushing to one remote at a time is supported")
30	}
31
32	remote := "origin"
33	if len(args) == 1 {
34		remote = args[0]
35	}
36
37	backend, err := cache.NewRepoCache(env.repo)
38	if err != nil {
39		return err
40	}
41	defer backend.Close()
42	interrupt.RegisterCleaner(backend.Close)
43
44	stdout, err := backend.Push(remote)
45	if err != nil {
46		return err
47	}
48
49	env.out.Println(stdout)
50
51	return nil
52}