Skip to content

Commit

Permalink
Merge pull request #8 from devops-kung-fu/vaporize
Browse files Browse the repository at this point in the history
fix: Vaporizes tailog and fixes pre-mature exit
  • Loading branch information
djschleen authored Jul 28, 2021
2 parents 24b631f + 87d4838 commit 7b54ffb
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"afero"
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ The current SBoM for ```hinge``` is available [here](hinge-sbom.json).

## Credits

A big thank-you to our friends at [Freepik](https://www.freepik.com) for the ```hinge``` logo.
A big thank-you to our friends at [Freepik](https://www.freepik.com) for the ```hinge``` logo.
14 changes: 3 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"

"github.com/devops-kung-fu/hinge/lib"
"github.com/devops-kung-fu/tailog"
"github.com/gookit/color"
"github.com/spf13/cobra"
)
Expand All @@ -30,22 +29,15 @@ var (
color.Style{color.FgRed, color.OpBold}.Println("Please provide the path to the repository.")
fmt.Println()
_ = cmd.Usage()
os.Exit(1)
} else if len(args) > 1 {
color.Style{color.FgRed, color.OpBold}.Println("Only one path is allowed.")
fmt.Println()
_ = cmd.Usage()
os.Exit(1)
}
os.Exit(1)
},
Run: func(cmd *cobra.Command, args []string) {
tailog := tailog.Tailog(tailog.DEBUG)
switch {
case trace:
_ = tailog.ChangeGlobalLevel(0)
case debug:
_ = tailog.ChangeGlobalLevel(1)
}

repoPath := args[0]
schedule := lib.Schedule{
Interval: "daily",
Expand All @@ -63,7 +55,7 @@ var (
case interval == "monthly":
schedule.Interval = interval
}
lib.Generator(tailog, repoPath, verbose, schedule)
lib.Generator(repoPath, verbose, schedule)

},
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/devops-kung-fu/hinge
go 1.16

require (
github.com/devops-kung-fu/tailog v0.1.7
github.com/gookit/color v1.4.2
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/yaml.v2 v2.4.0
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devops-kung-fu/tailog v0.1.7 h1:N1eM8c9lwBbOpeeFlPewdKjAn8xAHQWT8ymkCSiYhdE=
github.com/devops-kung-fu/tailog v0.1.7/go.mod h1:ll6LcF1jIJv7s22Vub1kd3VhTs7IsWwEMXTkTM+m7SI=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
41 changes: 7 additions & 34 deletions hinge-sbom.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"bomFormat": "CycloneDX",
"specVersion": "1.2",
"serialNumber": "urn:uuid:c9e967ef-9015-4f27-897b-9011b00a203d",
"serialNumber": "urn:uuid:7206b67d-bb64-4fba-bea1-c4fb3c74145c",
"version": 1,
"metadata": {
"timestamp": "2021-07-14T14:32:52-06:00",
"timestamp": "2021-07-22T20:58:27-06:00",
"tools": [
{
"vendor": "CycloneDX",
Expand All @@ -31,11 +31,11 @@
}
],
"component": {
"bom-ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210714142452-6a754f3bc11f",
"bom-ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210714162810-e7d7d897d5dd",
"type": "application",
"name": "github.com/devops-kung-fu/hinge",
"version": "v0.0.0-20210714142452-6a754f3bc11f",
"purl": "pkg:golang/github.com/devops-kung-fu/[email protected]20210714142452-6a754f3bc11f",
"version": "v0.0.0-20210714162810-e7d7d897d5dd",
"purl": "pkg:golang/github.com/devops-kung-fu/[email protected]20210714162810-e7d7d897d5dd",
"externalReferences": [
{
"url": "https://github.com/devops-kung-fu/hinge",
Expand All @@ -45,26 +45,6 @@
}
},
"components": [
{
"bom-ref": "pkg:golang/github.com/devops-kung-fu/[email protected]",
"type": "library",
"name": "github.com/devops-kung-fu/tailog",
"version": "v0.1.7",
"scope": "required",
"hashes": [
{
"alg": "SHA-256",
"content": "37578cf1cf65c016cea5e78594f7b074a8c09fcc401d0593f329a409289885d1"
}
],
"purl": "pkg:golang/github.com/devops-kung-fu/[email protected]",
"externalReferences": [
{
"url": "https://github.com/devops-kung-fu/tailog",
"type": "vcs"
}
]
},
{
"bom-ref": "pkg:golang/github.com/gookit/[email protected]",
"type": "library",
Expand Down Expand Up @@ -258,13 +238,6 @@
}
],
"dependencies": [
{
"ref": "pkg:golang/github.com/devops-kung-fu/[email protected]",
"dependsOn": [
"pkg:golang/github.com/gookit/[email protected]",
"pkg:golang/golang.org/x/[email protected]"
]
},
{
"ref": "pkg:golang/github.com/gookit/[email protected]",
"dependsOn": [
Expand Down Expand Up @@ -305,12 +278,12 @@
"ref": "pkg:golang/gopkg.in/[email protected]"
},
{
"ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210714142452-6a754f3bc11f",
"ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210714162810-e7d7d897d5dd",
"dependsOn": [
"pkg:golang/github.com/devops-kung-fu/[email protected]",
"pkg:golang/github.com/gookit/[email protected]",
"pkg:golang/github.com/spf13/[email protected]",
"pkg:golang/github.com/spf13/[email protected]",
"pkg:golang/golang.org/x/[email protected]",
"pkg:golang/golang.org/x/[email protected]",
"pkg:golang/gopkg.in/[email protected]",
"pkg:golang/[email protected]"
Expand Down
30 changes: 30 additions & 0 deletions lib/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lib

import "log"

//IsError Checks to see if an error exists, and if so
//writes it to the log with the provided prefix
func IsError(err error, prefix string) error {
if err != nil {
log.Printf("%v: %v", prefix, err)
}
return err
}

//IsErrorBool Checks to see if an error exists, and if so
//returns true after writing the error to the log with the provided prefix
func IsErrorBool(err error, prefix string) (b bool) {
if err != nil {
log.Printf("%v: %v", prefix, err)
b = true
}
return
}

//IfErrorLog Checks to see if an error exists, and if so
//simply writes it to the log.
func IfErrorLog(err error, prefix string) {
if err != nil {
log.Printf("%v: %v", prefix, err)
}
}
49 changes: 49 additions & 0 deletions lib/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lib

import (
"bytes"
"errors"
"log"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsError(t *testing.T) {
output := captureOutput(func() {
IsError(errors.New("Test Error"), "[TEST]")
})

if output.Len() == 0 {
assert.GreaterOrEqual(t, output.Len(), 0, "No information logged to STDOUT")
assert.GreaterOrEqual(t, strings.Count(output.String(), "\n"), 1, "Expected only a single line of log output")
}
}

func TestIsErrorBool(t *testing.T) {
output := captureOutput(func() {
IsErrorBool(errors.New("Test Error"), "[TEST]")
})

assert.GreaterOrEqual(t, output.Len(), 0, "No information logged to STDOUT")
assert.GreaterOrEqual(t, strings.Count(output.String(), "\n"), 1, "Expected only a single line of log output")
}

func TestIfErrorLog(t *testing.T) {
output := captureOutput(func() {
IfErrorLog(errors.New("Test Error"), "[TEST]")
})

assert.GreaterOrEqual(t, output.Len(), 0, "No information logged to STDOUT")
assert.GreaterOrEqual(t, strings.Count(output.String(), "\n"), 1, "Expected only a single line of log output")
}

func captureOutput(f func()) bytes.Buffer {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf
}
15 changes: 3 additions & 12 deletions lib/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import (
"path"
"strings"

"github.com/devops-kung-fu/tailog"
"github.com/gookit/color"
"github.com/spf13/afero"
"gopkg.in/yaml.v2"
)

// Generator - generates the dependabot.yml in the specified repo path.
func Generator(logger *tailog.Logger, repoPath string, verbose bool, schedule Schedule) {
logger.Trace("Setting up filesystem.")
func Generator(repoPath string, verbose bool, schedule Schedule) {
fs := afero.NewOsFs()
logger.Debug("Getting platform ecosystems.")
bundler := platform(fs, `Gemfile|Gemfile\.lock`, repoPath, "bundler", schedule)
cargo := platform(fs, `Cargo\.toml`, repoPath, "cargo", schedule)
composer := platform(fs, `composer\.json`, repoPath, "composer", schedule)
Expand All @@ -40,27 +37,21 @@ func Generator(logger *tailog.Logger, repoPath string, verbose bool, schedule Sc
terraform := platform(fs, `(.*)\.tf`, repoPath, "terraform", schedule)
color.Style{color.FgLightBlue}.Print(" ■ ")
fmt.Println("Got platform ecosystems")
// logger.Info("Got platform ecosystems.")
// logger.Debug("Begin joining updates.")
updates := joinUpdates(bundler, cargo, composer, docker, elm, gitsubmodules, githubActual, gomod, gradle, hexmix, maven, npm, nuget, pip, terraform)
color.Style{color.FgLightBlue}.Print(" ■ ")
fmt.Println("Joined all updates")
// logger.Info("Joined all updates.")
logger.Debug("Building configuration.")
config := Configuration{
Version: 2,
Updates: updates,
}
color.Style{color.FgLightBlue}.Print(" ■ ")
fmt.Println("Configuration complete")
//logger.Info("Configuration complete.")
if verbose {
logger.Trace("Output configuration to standard output.")
outputConfig(config)
}
logger.Debug("Writing configuration.")
writeConfig(fs, repoPath, config)
//logger.Info("Done.")
color.Style{color.FgGreen}.Print(" ■ ")
fmt.Println("Updated .github/dependabot.yml")
color.Style{color.FgGreen}.Print(" ■ ")
fmt.Println("Done!")
}
Expand Down

0 comments on commit 7b54ffb

Please sign in to comment.