push.go

 1package commands
 2
 3import (
 4	"errors"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/git-bug/git-bug/commands/completion"
 9	"github.com/git-bug/git-bug/commands/execenv"
10	"github.com/git-bug/git-bug/repository"
11)
12
13func newPushCommand(env *execenv.Env) *cobra.Command {
14	cmd := &cobra.Command{
15		Use:     "push [REMOTE]",
16		Short:   "Push updates to a git remote",
17		PreRunE: execenv.LoadBackend(env),
18		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
19			return runPush(env, args)
20		}),
21		ValidArgsFunction: completion.GitRemote(env),
22	}
23
24	return cmd
25}
26
27func runPush(env *execenv.Env, args []string) error {
28	var remote string
29	switch {
30	case len(args) > 1:
31		return errors.New("Only pushing to one remote at a time is supported")
32	case len(args) == 1:
33		remote = args[0]
34	default:
35		v, err := repository.GetDefaultString("git-bug.remote", env.Repo.AnyConfig(), "origin")
36		if err != nil {
37			return err
38		}
39		remote = v
40	}
41
42	stdout, err := env.Backend.Push(remote)
43	if err != nil {
44		return err
45	}
46
47	env.Out.Println(stdout)
48
49	return nil
50}