Skip to content

Commit

Permalink
Do not output messages if we are being quiet! (#246)
Browse files Browse the repository at this point in the history
* Squelch these messages if output format isn't text, and if someone wants things to be quiet

* Rudimentary test for alpine

Co-authored-by: Dan Rollo <danrollo@gmail.com>
  • Loading branch information
DarthHater and bhamail authored Sep 16, 2021
1 parent 5712bb4 commit f9fa6c1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:latest

RUN apk add --no-cache ca-certificates \
RUN apk add --no-cache ca-certificates jq \
&& update-ca-certificates 2>/dev/null || true \
&& rm -rf /var/cache/apk/*

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,16 @@ docker-alpine-integration-test: build-linux
# in ci its likely you have the codebase (thus .nancy-ignore) in the same location you run nancy sleuth
go list -json -deps > dist/deps.out
echo "cd /tmp && cat /tmp/dist/deps.out | nancy sleuth" > dist/ci.sh
echo "cd /tmp && cat /tmp/dist/deps.out | nancy sleuth --output=json && > nancy-result.json && cat nancy-result.json | jq '.'" > dist/ci-json.sh
chmod +x dist/ci.sh
chmod +x dist/ci-json.sh
# run the container....using cat with no params keeps it running
$(DOCKER_CMD) run --name alpine-integration-test -td sonatypecommunity/nancy:alpine-integration-test cat
# copy the code as if it was actually in the "ci" container.. doing this cause circleci cant actually mount volumes
$(DOCKER_CMD) cp . alpine-integration-test:/tmp
# run nancy against nancy output
$(DOCKER_CMD) exec -it alpine-integration-test /bin/sh /tmp/dist/ci.sh
$(DOCKER_CMD) exec -it alpine-integration-test /bin/sh /tmp/dist/ci-json.sh
$(DOCKER_CMD) stop alpine-integration-test && $(DOCKER_CMD) rm alpine-integration-test


Expand Down
23 changes: 13 additions & 10 deletions internal/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ package cmd

import (
"fmt"
"time"

"github.com/sirupsen/logrus"
"github.com/sonatype-nexus-community/nancy/buildversion"
"github.com/sonatype-nexus-community/nancy/settings"
"github.com/sonatype-nexus-community/nancy/update"
"time"
)

// For use in checking for newer release version during app startup (not during explicit command to check for update)
func checkForUpdates(gitHubAPI string) error {
func checkForUpdates(gitHubAPI string, quiet bool) error {
if configOssi.SkipUpdateCheck {
logLady.Debug("skipping update check")
return nil
Expand All @@ -46,7 +47,7 @@ func checkForUpdates(gitHubAPI string) error {
}).Trace("updateCheck")

if update.ShouldCheckForUpdates(updateCheck) {
logAndShowMessage("Checking for updates...")
logAndShowMessage("Checking for updates...", quiet)

logLady.WithFields(logrus.Fields{
"gitHubAPI": gitHubAPI,
Expand All @@ -62,25 +63,25 @@ func checkForUpdates(gitHubAPI string) error {
}

if !check.Found {
logAndShowMessage("No updates found.")
logAndShowMessage("No updates found.", quiet)

updateCheck.LastUpdateCheck = time.Now()
err = updateCheck.WriteToDisk()
return err
}

if update.IsLatestVersion(check) {
logAndShowMessage("Already up-to-date.")
logAndShowMessage("Already up-to-date.", quiet)

updateCheck.LastUpdateCheck = time.Now()
err = updateCheck.WriteToDisk()
return err
}
logLady.Debug(update.DebugVersion(check))

logAndShowMessage(update.ReportVersion(check))
logAndShowMessage(update.HowToUpdate(check))
logAndShowMessage("\n") // Print a new-line after all of that
logAndShowMessage(update.ReportVersion(check), quiet)
logAndShowMessage(update.HowToUpdate(check), quiet)
logAndShowMessage("\n", quiet) // Print a new-line after all of that

updateCheck.LastUpdateCheck = time.Now()
err = updateCheck.WriteToDisk()
Expand All @@ -92,7 +93,9 @@ func checkForUpdates(gitHubAPI string) error {
return nil
}

func logAndShowMessage(message string) {
func logAndShowMessage(message string, quiet bool) {
logLady.Info(message)
fmt.Println(message)
if !quiet {
fmt.Println(message)
}
}
5 changes: 3 additions & 2 deletions internal/cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package cmd

import (
"testing"

"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCheckForUpdates(t *testing.T) {
Expand All @@ -28,5 +29,5 @@ func TestCheckForUpdates(t *testing.T) {
// NOTE: will not actually run check unless last_update_check is old or file is removed.
// Still, just having this test helped me find a slug bug. Hey, that rhymes.
// Can add real harness setup/teardown later if desired.
assert.Nil(t, checkForUpdates(""))
assert.Nil(t, checkForUpdates("", false))
}
10 changes: 8 additions & 2 deletions internal/cmd/iq.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package cmd
import (
"errors"
"fmt"
"io"
"os"

"github.com/mitchellh/go-homedir"
"github.com/sonatype-nexus-community/go-sona-types/configuration"
"github.com/sonatype-nexus-community/go-sona-types/iq"
Expand All @@ -31,8 +34,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"io"
"os"
)

type iqServerFactory interface {
Expand Down Expand Up @@ -106,6 +107,11 @@ func doIQ(cmd *cobra.Command, args []string) (err error) {
logLady = logger.GetLogger("", configOssi.LogLevel)
logLady.Info("Nancy parsing config for IQ")

err = checkForUpdates("", true)
if err != nil {
return
}

printHeader(!configOssi.Quiet)

var purls []string
Expand Down
12 changes: 10 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"bufio"
"fmt"
"github.com/spf13/pflag"
"os"
"path/filepath"
"reflect"
Expand All @@ -28,6 +27,8 @@ import (
"strings"
"time"

"github.com/spf13/pflag"

"github.com/common-nighthawk/go-figure"
"github.com/golang/dep"
"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -126,7 +127,7 @@ a smooth experience as a Golang developer, using the best tools in the market!`,
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
setupViperAutomaticEnv()
logLady = logger.GetLogger("", configOssi.LogLevel)
return checkForUpdates("")
return nil
},
RunE: doRoot,
}
Expand Down Expand Up @@ -265,6 +266,13 @@ func processConfig() (err error) {
configOssi.Formatter = audit.AuditLogTextFormatter{Quiet: isQuiet, NoColor: configOssi.NoColor}
}

var isTotallyQuiet = isQuiet && outputFormat != "text"

err = checkForUpdates("", isTotallyQuiet)
if err != nil {
return
}

ossIndex := ossiCreator.create()

printHeader(!getIsQuiet() && reflect.TypeOf(configOssi.Formatter).String() == "audit.AuditLogTextFormatter")
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/sleuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package cmd

import (
"fmt"
"os"

"github.com/sonatype-nexus-community/nancy/internal/customerrors"
"github.com/sonatype-nexus-community/nancy/internal/logger"
"github.com/spf13/cobra"
"os"
)

func init() {
Expand Down
18 changes: 9 additions & 9 deletions internal/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func newUpdateCommand() *cobra.Command {
Short: "Check if there are any updates available",
RunE: func(_ *cobra.Command, _ []string) error {
//return updateCLI("", true)
return updateCLI("", false)
return updateCLI("", false, true)
},
}

return updateCmd
}

func updateCLI(gitHubAPI string, performUpdate bool) error {
logAndShowMessage("Checking for updates...")
func updateCLI(gitHubAPI string, performUpdate bool, quiet bool) error {
logAndShowMessage("Checking for updates...", quiet)
latest, found, err := selfupdate.DetectLatest(update.NancySlug)
if err != nil {
return err
Expand All @@ -66,30 +66,30 @@ func updateCLI(gitHubAPI string, performUpdate bool) error {
logLady.WithField("check results", check).Debug("")

if !check.Found {
logAndShowMessage("No updates found.")
logAndShowMessage("No updates found.", quiet)
return nil
}

if update.IsLatestVersion(check) {
logAndShowMessage("Already up-to-date.")
logAndShowMessage("Already up-to-date.", quiet)
return nil
}

logLady.Debug(update.DebugVersion(check))
logAndShowMessage(update.ReportVersion(check))
logAndShowMessage(update.ReportVersion(check), quiet)

if !performUpdate {
logAndShowMessage(update.HowToUpdate(check))
logAndShowMessage(update.HowToUpdate(check), quiet)
return nil
}

logAndShowMessage("Installing update...")
logAndShowMessage("Installing update...", quiet)
message, err := update.InstallLatest(check)
if err != nil {
return err
}

logAndShowMessage(message)
logAndShowMessage(message, quiet)

return nil
}

0 comments on commit f9fa6c1

Please sign in to comment.