Skip to content

Commit

Permalink
main: add support to different output formats (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
atosatto authored May 3, 2020
1 parent 7530de4 commit f35a4b9
Show file tree
Hide file tree
Showing 45 changed files with 3,226 additions and 337 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
needs: test
steps:
- name: Checkout
uses: actions/checkout@v1 # using v1 here because with v2
uses: actions/checkout@v1 # using v1 here because with v2 goreleaser
# computes the whole git history as changelog
- name: Set up Go
uses: actions/setup-go@v1
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ WARN: atosatto.prometheus: role not at the latest version, upgrade from v1.0.1 t
WARN: atosatto.grafana: role not at the latest version, upgrade from v1.0.0 to v1.1.0.
```


In addition to requirements files, `ansible-requirements-lint` can parse role dependencies
declared in the `meta/main.yml` file in your role directory

Expand Down
82 changes: 67 additions & 15 deletions cmd/ansible-requirements-lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@ import (
"fmt"
"os"
"os/signal"
"sync"

"github.com/atosatto/ansible-requirements-lint/linter"
"github.com/atosatto/ansible-requirements-lint/provider"
"github.com/atosatto/ansible-requirements-lint/requirements"
"github.com/fatih/color"
"github.com/atosatto/ansible-requirements-lint/pkg/linter"
"github.com/atosatto/ansible-requirements-lint/pkg/parser"
"github.com/atosatto/ansible-requirements-lint/pkg/provider"
"github.com/atosatto/ansible-requirements-lint/pkg/writer"
)

var (
verbose = flag.Bool("v", false, "")
galaxyURL = flag.String("galaxy", provider.DefaultAnsibleGalaxyURL, "")
noColor = flag.Bool("no-color", false, "")
outFormat = flag.String("o", "text", "")
printVersion = flag.Bool("V", false, "")
printHelp = flag.Bool("h", false, "")
)

// version will be set at compilation time
var version string

var usage = fmt.Sprintf(`Usage: ansible-requirements-lint [options...] <requirements-file>
Options:
-v Enable verbose output.
-galaxy <url> Set the Ansible Galaxy URL (default: %s).
-o <format> Format of the output, allowed values are text,table (default: text).
-no-color Disable color output.
-V Print the version number and exit.
-h Show this help message and exit.
Expand All @@ -49,9 +53,17 @@ func main() {
usageAndExit("")
}

// disables colorized output
if *noColor {
color.NoColor = true
var out writer.Writer
switch *outFormat {
case "table":
out = writer.TableWriter{
Verbose: *verbose,
}
default:
out = writer.TextWriter{
Verbose: *verbose,
NoColor: *noColor,
}
}

var requirementsFile = flag.Arg(0)
Expand All @@ -64,7 +76,8 @@ func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

// call cancel on the context
// call cancel on the context in
// order to prevent go routines from leaking
defer func() {
signal.Stop(c)
cancel()
Expand All @@ -78,7 +91,7 @@ func main() {
}()

// parse the requirements file
r, err := requirements.UnmarshalFromFile(requirementsFile)
requirements, err := parser.UnmarshalFromFile(requirementsFile)
if err != nil {
switch {
case os.IsNotExist(err):
Expand All @@ -90,13 +103,52 @@ func main() {
}
}

// run the updates linter
updatesLinterOpts := linter.UpdatesLinterOptions{
AnsibleGalaxyURL: *galaxyURL,
}
numUpdatesOrErrs := runUpdatesLinter(ctx, r, updatesLinterOpts)
// create a WaitGroup to make sure
// to wait for the Linters to finish before
// exiting the program
var wg sync.WaitGroup

// whether to exit with an error code
// or not
var exitWithError = false

// run the Updates Linter
wg.Add(2)
updatesLinterResults := make(chan linter.Result)
updatesLinterOutput := make(chan linter.Result)
go func() {
updatesLinter := linter.NewUpdatesLinter()
updatesLinter.WithAnsibleGalaxyURL(*galaxyURL)
updatesLinter.Lint(ctx, requirements, updatesLinterResults)
defer wg.Done()
}()
go func() {
out.WriteUpdates(ctx, os.Stdout, updatesLinterOutput)
defer wg.Done()
}()

// check wether the Updates Linter has reported
// any Error or Warning and copy back the results
// to the output channel
func() {
for update := range updatesLinterResults {
select {
case <-ctx.Done():
return
default:
if update.Level != linter.LevelInfo {
exitWithError = true
}
updatesLinterOutput <- update
}
}
}()
close(updatesLinterOutput)

// wait for the Linters to be done
wg.Wait()

if numUpdatesOrErrs > 0 {
if exitWithError {
os.Exit(1)
}
}
Expand Down
45 changes: 0 additions & 45 deletions cmd/ansible-requirements-lint/updates.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/fatih/color v1.9.0
github.com/hashicorp/go-version v1.2.0
github.com/olekukonko/tablewriter v0.0.4
gopkg.in/src-d/go-billy.v4 v4.3.2
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
Loading

0 comments on commit f35a4b9

Please sign in to comment.