2018-09-24 18:11:50 +03:00
package commands
import (
2019-08-13 20:51:14 +03:00
"context"
"fmt"
"os"
"sync"
2019-05-03 01:02:50 +03:00
"time"
2019-11-03 18:46:51 +03:00
"github.com/araddon/dateparse"
"github.com/pkg/errors"
2019-06-09 00:14:35 +03:00
"github.com/spf13/cobra"
2018-09-24 18:11:50 +03:00
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/cache"
2018-10-25 00:36:39 +03:00
"github.com/MichaelMure/git-bug/util/interrupt"
2018-09-24 18:11:50 +03:00
)
2019-10-25 17:42:48 +03:00
var (
2019-11-03 18:46:51 +03:00
bridgePullImportSince string
bridgePullNoResume bool
2019-10-25 17:42:48 +03:00
)
2018-09-24 18:11:50 +03:00
func runBridgePull ( cmd * cobra . Command , args [ ] string ) error {
2019-11-03 18:46:51 +03:00
if bridgePullNoResume && bridgePullImportSince != "" {
2019-10-25 17:42:48 +03:00
return fmt . Errorf ( "only one of --no-resume and --since flags should be used" )
}
2018-09-24 18:11:50 +03:00
backend , err := cache . NewRepoCache ( repo )
if err != nil {
return err
}
defer backend . Close ( )
2018-10-25 00:36:39 +03:00
interrupt . RegisterCleaner ( backend . Close )
2018-09-24 18:11:50 +03:00
var b * core . Bridge
if len ( args ) == 0 {
b , err = bridge . DefaultBridge ( backend )
} else {
2019-06-15 03:33:06 +03:00
b , err = bridge . LoadBridge ( backend , args [ 0 ] )
2018-09-24 18:11:50 +03:00
}
if err != nil {
return err
}
2019-08-13 20:51:14 +03:00
parentCtx := context . Background ( )
ctx , cancel := context . WithCancel ( parentCtx )
defer cancel ( )
// buffered channel to avoid send block at the end
done := make ( chan struct { } , 1 )
var mu sync . Mutex
interruptCount := 0
interrupt . RegisterCleaner ( func ( ) error {
mu . Lock ( )
if interruptCount > 0 {
fmt . Println ( "Received another interrupt before graceful stop, terminating..." )
os . Exit ( 0 )
}
interruptCount ++
mu . Unlock ( )
fmt . 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
} )
2019-10-25 17:42:48 +03:00
var events <- chan core . ImportResult
2019-11-03 18:46:51 +03:00
switch {
case bridgePullNoResume :
events , err = b . ImportAllSince ( ctx , time . Time { } )
case bridgePullImportSince != "" :
since , err2 := parseSince ( bridgePullImportSince )
if err2 != nil {
return errors . Wrap ( err2 , "import time parsing" )
2019-10-25 17:42:48 +03:00
}
events , err = b . ImportAllSince ( ctx , since )
2019-11-03 18:46:51 +03:00
default :
events , err = b . ImportAll ( ctx )
}
if err != nil {
return err
2018-09-24 18:11:50 +03:00
}
2019-08-13 20:51:14 +03:00
importedIssues := 0
importedIdentities := 0
for result := range events {
2019-08-19 01:19:58 +03:00
if result . Event != core . ImportEventNothing {
fmt . Println ( result . String ( ) )
}
2019-08-13 20:51:14 +03:00
switch result . Event {
case core . ImportEventBug :
importedIssues ++
case core . ImportEventIdentity :
importedIdentities ++
}
}
2019-10-25 17:42:48 +03:00
fmt . Printf ( "imported %d issues and %d identities with %s bridge\n" , importedIssues , importedIdentities , b . Name )
2019-08-13 20:51:14 +03:00
// send done signal
close ( done )
2019-11-03 18:46:51 +03:00
return nil
}
func parseSince ( since string ) ( time . Time , error ) {
duration , err := time . ParseDuration ( since )
if err == nil {
return time . Now ( ) . Add ( - duration ) , nil
}
return dateparse . ParseLocal ( since )
2018-09-24 18:11:50 +03:00
}
var bridgePullCmd = & cobra . Command {
2018-10-17 21:38:10 +03:00
Use : "pull [<name>]" ,
2019-02-24 16:46:08 +03:00
Short : "Pull updates." ,
2018-10-17 21:38:10 +03:00
PreRunE : loadRepo ,
RunE : runBridgePull ,
2019-06-09 00:14:35 +03:00
Args : cobra . MaximumNArgs ( 1 ) ,
2018-09-24 18:11:50 +03:00
}
func init ( ) {
bridgeCmd . AddCommand ( bridgePullCmd )
2019-11-03 18:46:51 +03:00
bridgePullCmd . Flags ( ) . BoolVarP ( & bridgePullNoResume , "no-resume" , "n" , false , "force importing all bugs" )
bridgePullCmd . Flags ( ) . StringVarP ( & bridgePullImportSince , "since" , "s" , "" , "import only bugs updated after the given date (ex: \"200h\" or \"june 2 2019\")" )
2018-09-24 18:11:50 +03:00
}