mirror of
https://github.com/cooperspencer/gickup.git
synced 2026-10-19 16:16:46 +00:00
added exit code when an error occured (#289)
This commit is contained in:
@@ -55,7 +55,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
|
||||
err := repo.Filter.ParseDuration()
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msg(err.Error())
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -210,7 +210,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
sub = logger.CreateSubLogger("stage", "gitea", "url", repo.URL)
|
||||
err := repo.Filter.ParseDuration()
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msg(err.Error())
|
||||
}
|
||||
ran = true
|
||||
|
||||
+2
-2
@@ -110,7 +110,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
sub = logger.CreateSubLogger("stage", "github", "url", "https://github.com")
|
||||
err := repo.Filter.ParseDuration()
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msg(err.Error())
|
||||
}
|
||||
ran = true
|
||||
@@ -444,7 +444,7 @@ func GetIssues(repo *github.Repository, client *github.Client, conf types.GenRep
|
||||
return issues
|
||||
}
|
||||
if errorcount < 5 {
|
||||
sub.Error().Err(err).Str("repo", *repo.Name).Msg("can't fetch issues")
|
||||
sub.Warn().Err(err).Str("repo", *repo.Name).Msg("can't fetch issues")
|
||||
time.Sleep(5 * time.Second)
|
||||
errorcount++
|
||||
} else {
|
||||
|
||||
+2
-2
@@ -109,7 +109,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
err := repo.Filter.ParseDuration()
|
||||
sub = logger.CreateSubLogger("stage", "gitlab", "url", repo.URL)
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msg(err.Error())
|
||||
}
|
||||
ran = true
|
||||
@@ -563,7 +563,7 @@ func GetIssues(repo *gitlab.Project, client *gitlab.Client, conf types.GenRepo)
|
||||
return issues
|
||||
}
|
||||
if errorcount < 5 {
|
||||
sub.Error().Err(err).Str("repo", repo.Name).Msg("can't fetch issues")
|
||||
sub.Warn().Err(err).Str("repo", repo.Name).Msg("can't fetch issues")
|
||||
time.Sleep(5 * time.Second)
|
||||
errorcount++
|
||||
} else {
|
||||
|
||||
+2
-2
@@ -139,7 +139,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
sub = logger.CreateSubLogger("stage", "gogs", "url", repo.URL)
|
||||
err := repo.Filter.ParseDuration()
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msg(err.Error())
|
||||
}
|
||||
ran = true
|
||||
@@ -401,7 +401,7 @@ func GetIssues(repo *gogs.Repository, client *gogs.Client, conf types.GenRepo) m
|
||||
i, err := client.ListRepoIssues(repo.Owner.UserName, repo.Name, listOptions)
|
||||
if err != nil {
|
||||
if errorcount < 5 {
|
||||
sub.Error().Err(err).Str("repo", repo.Name).Msg("can't fetch issues")
|
||||
sub.Warn().Err(err).Str("repo", repo.Name).Msg("can't fetch issues")
|
||||
time.Sleep(5 * time.Second)
|
||||
errorcount++
|
||||
} else {
|
||||
|
||||
+22
-1
@@ -4,6 +4,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/cooperspencer/gickup/types"
|
||||
"github.com/rs/zerolog"
|
||||
@@ -11,6 +12,10 @@ import (
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
exitcode int32
|
||||
)
|
||||
|
||||
// NewRollingFile TODO.
|
||||
func NewRollingFile(config types.FileLogging) io.Writer {
|
||||
if config.Dir != "" {
|
||||
@@ -44,7 +49,10 @@ func CreateLogger(conf types.Logging) zerolog.Logger {
|
||||
|
||||
mw := io.MultiWriter(writers...)
|
||||
|
||||
return zerolog.New(mw).With().Timestamp().Logger()
|
||||
logger := zerolog.New(mw).With().Timestamp().Logger()
|
||||
|
||||
// Attach the error hook to the main logger
|
||||
return logger.Hook(&ErrorHook{})
|
||||
}
|
||||
|
||||
// CreateSubLogger create a sublogger for modules
|
||||
@@ -59,5 +67,18 @@ func CreateSubLogger(args ...string) zerolog.Logger {
|
||||
sub = sub.Str(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
return sub.Logger()
|
||||
}
|
||||
|
||||
type ErrorHook struct{}
|
||||
|
||||
func GetExitCode() int32 {
|
||||
return exitcode
|
||||
}
|
||||
|
||||
func (h *ErrorHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
|
||||
if level == zerolog.ErrorLevel {
|
||||
atomic.StoreInt32(&exitcode, 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1007,6 +1007,7 @@ func main() {
|
||||
break
|
||||
}
|
||||
}
|
||||
os.Exit(int(logger.GetExitCode()))
|
||||
}
|
||||
|
||||
func logNextRun(conf *types.Conf) {
|
||||
|
||||
+13
-14
@@ -29,7 +29,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
sub = logger.CreateSubLogger("stage", "onedev", "url", repo.URL)
|
||||
err := repo.Filter.ParseDuration()
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msg(err.Error())
|
||||
}
|
||||
include := types.GetMap(repo.Include)
|
||||
@@ -108,7 +108,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
|
||||
defaultbranch, _, err := client.GetDefaultBranch(r.ID)
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msgf("couldn't get default branch for %s", r.Name)
|
||||
defaultbranch = "main"
|
||||
}
|
||||
@@ -118,16 +118,15 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
options := onedev.CommitQueryOptions{Query: fmt.Sprintf("branch(%s)", defaultbranch), Fields: []string{onedev.Committer, onedev.Parents, onedev.Author, onedev.CommitDate, onedev.Body, onedev.FileChanges, onedev.LineChanges, onedev.Subject}}
|
||||
commits, _, err := client.GetCommits(r.ID, &options)
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
Msgf("can't get latest commit for %s", defaultbranch)
|
||||
} else {
|
||||
if len(commits) > 0 {
|
||||
lastactive := time.UnixMicro(commits[0].Author.When)
|
||||
if time.Since(lastactive) > repo.Filter.LastActivityDuration && repo.Filter.LastActivityDuration != 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
sub.Error().
|
||||
Msgf("can't get latest commit for %s", defaultbranch)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(commits) > 0 {
|
||||
lastactive := time.UnixMicro(commits[0].Author.When)
|
||||
if time.Since(lastactive) > repo.Filter.LastActivityDuration && repo.Filter.LastActivityDuration != 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +188,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
|
||||
defaultbranch, _, err := client.GetDefaultBranch(r.ID)
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msgf("couldn't get default branch for %s", r.Name)
|
||||
defaultbranch = "main"
|
||||
}
|
||||
@@ -306,7 +305,7 @@ func GetIssues(repo *onedev.Project, client *onedev.Client, conf types.GenRepo,
|
||||
return issues
|
||||
}
|
||||
if errorcount < 5 {
|
||||
sub.Error().Err(err).Str("repo", repo.Name).Msg("can't fetch issues")
|
||||
sub.Warn().Err(err).Str("repo", repo.Name).Msg("can't fetch issues")
|
||||
time.Sleep(5 * time.Second)
|
||||
errorcount++
|
||||
} else {
|
||||
|
||||
@@ -153,7 +153,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
|
||||
sub = logger.CreateSubLogger("stage", "sourcehut", "url", repo.URL)
|
||||
err := repo.Filter.ParseDuration()
|
||||
if err != nil {
|
||||
sub.Error().
|
||||
sub.Warn().
|
||||
Msg(err.Error())
|
||||
}
|
||||
ran = true
|
||||
|
||||
Reference in New Issue
Block a user