Skip to content

Commit

Permalink
errs
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 16, 2017
1 parent 3b0be9e commit eadaf6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: go
go: 1.8
go: 1.9
install: make setup
script: make ci
after_success:
Expand Down
21 changes: 4 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
SOURCE_FILES?=$$(go list ./... | grep -v /vendor/)
SOURCE_FILES?=./...
TEST_PATTERN?=.
TEST_OPTIONS?=

setup: ## Install all the build and lint dependencies
go get -u github.com/alecthomas/gometalinter
go get -u github.com/golang/dep/...
go get -u github.com/pierrre/gotestcover
go get -u golang.org/x/tools/cmd/cover
# go get -u github.com/pierrre/gotestcover
# go get -u golang.org/x/tools/cmd/cover
dep ensure
gometalinter --install --update

Expand All @@ -20,20 +20,7 @@ fmt: ## gofmt and goimports all go files
find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done

lint: ## Run all the linters
gometalinter --vendor --disable-all \
--enable=deadcode \
--enable=ineffassign \
--enable=gosimple \
--enable=staticcheck \
--enable=gofmt \
--enable=goimports \
--enable=dupl \
--enable=misspell \
--enable=errcheck \
--enable=vet \
--enable=vetshadow \
--deadline=10m \
./...
gometalinter --vendor ./...

ci: lint test ## Run all the tests and code checks

Expand Down
35 changes: 26 additions & 9 deletions cmd/github-vacations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,27 @@ func main() {
func readNotification(path string) {
log.Info("checking your notifications...")
db, err := bolt.Open(os.ExpandEnv(path), 0600, nil)
kingpin.FatalIfError(err, "%v")
if err != nil {
log.WithError(err).Fatal("failed to open database")
}
defer closeDB(db)
if err := db.View(func(tx *bolt.Tx) error {
var bucket = tx.Bucket([]byte("notifications"))
if bucket == nil {
log.Warn("0 notifications to read")
return nil
}
var notifications = map[string][]lib.Notification{}
bucket.ForEach(func(url, encoded []byte) error {
if err := bucket.ForEach(func(url, encoded []byte) error {
var notification lib.Notification
if err := json.Unmarshal(encoded, &notification); err != nil {
return err
}
notifications[notification.Repo] = append(notifications[notification.Repo], notification)
return nil
})
}); err != nil {
log.WithError(err).Fatal("failed to read bucket")
}
for repo, repoNotifications := range notifications {
fmt.Printf("\n")
cli.Default.Padding = 3
Expand All @@ -79,18 +84,22 @@ func readNotification(path string) {
}
return nil
}); err != nil {
kingpin.FatalIfError(err, "%v")
log.WithError(err).Fatal("failed to read database")
}
}

func checkNotifications(path, org, token string) {
log.Info("helping you not to work...")
db, err := bolt.Open(os.ExpandEnv(path), 0600, nil)
kingpin.FatalIfError(err, "%v")
defer db.Close()
if err != nil {
log.WithError(err).Fatal("failed to open database")
}
defer closeDB(db)

notifications, err := lib.MarkWorkNotificationsAsRead(token, org)
kingpin.FatalIfError(err, "%v")
if err != nil {
log.WithError(err).Fatal("failed check your notifications")
}
log.Infof("%d %s notifications mark as read", len(notifications), org)

if err := db.Update(func(tx *bolt.Tx) error {
Expand All @@ -103,11 +112,19 @@ func checkNotifications(path, org, token string) {
if err != nil {
return err
}
bucket.Put([]byte(notification.URL), encoded)
if err := bucket.Put([]byte(notification.URL), encoded); err != nil {
return err
}
}
return nil
}); err != nil {
kingpin.FatalIfError(err, "%v")
log.WithError(err).Fatal("failed to store your notifications")
}
log.Infof("notifications stored on %s", path)
}

func closeDB(db *bolt.DB) {
if err := db.Close(); err != nil {
log.WithError(err).Error("failed to close db")
}
}

0 comments on commit eadaf6c

Please sign in to comment.