git-bug/commands/bridge_push.go
Johannes Altmanninger bc6ba02bd8
Add command-specific argument completions
Complete bug IDs, bridges, users, labels where appropriate.

This works in bash and fish. ZSH is not yet supported by Cobra.
In fish, descriptions (the part of a completion after the "\t") are shown
as completion label, and can be searched with Ctrl+S.

Reproduce with

	fish -C 'source misc/fish_completion/git-bug'
	git bug select ^I

(tested with fish version 3.3.1)

Also works with bash, but only for "git-bug" (with the dash)

	bash --rcfile <(echo source misc/bash_completion/git-bug)
	git-bug select ^I

Closes #493
2022-05-01 10:54:13 +02:00

98 lines
1.9 KiB
Go

package commands
import (
"context"
"os"
"sync"
"time"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/util/interrupt"
)
func newBridgePushCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "push [NAME]",
Short: "Push updates.",
PreRunE: loadBackendEnsureUser(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runBridgePush(env, args)
}),
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: completeBridge(env),
}
return cmd
}
func runBridgePush(env *Env, args []string) error {
var b *core.Bridge
var err error
if len(args) == 0 {
b, err = bridge.DefaultBridge(env.backend)
} else {
b, err = bridge.LoadBridge(env.backend, args[0])
}
if err != nil {
return err
}
parentCtx := context.Background()
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
done := make(chan struct{}, 1)
var mu sync.Mutex
interruptCount := 0
interrupt.RegisterCleaner(func() error {
mu.Lock()
if interruptCount > 0 {
env.err.Println("Received another interrupt before graceful stop, terminating...")
os.Exit(0)
}
interruptCount++
mu.Unlock()
env.err.Println("Received interrupt signal, stopping the import...\n(Hit ctrl-c again to kill the process.)")
// send signal to stop the importer
cancel()
// block until importer gracefully shutdown
<-done
return nil
})
events, err := b.ExportAll(ctx, time.Time{})
if err != nil {
return err
}
exportedIssues := 0
for result := range events {
if result.Event != core.ExportEventNothing {
env.out.Println(result.String())
}
switch result.Event {
case core.ExportEventBug:
exportedIssues++
}
}
env.out.Printf("exported %d issues with %s bridge\n", exportedIssues, b.Name)
// send done signal
close(done)
return nil
}