Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support json and yaml format outputs #31

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage.out
.DS_Store
dist/
bin/
.idea
1 change: 1 addition & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func New() *cobra.Command {

func AddCommands(topLevel *cobra.Command) {
addValidate(topLevel)
addExport(topLevel)
}

func initLogging(*cobra.Command, []string) error {
Expand Down
183 changes: 183 additions & 0 deletions commands/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commands

import (
"encoding/json"
"fmt"
"os"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"sigs.k8s.io/zeitgeist/dependency"
)

type OutputFormat string

const (
YAML OutputFormat = "yaml"
JSON OutputFormat = "json"
LOG OutputFormat = "log"
)

const defaultOutputFileName = "dependencies_output"

type exportOptions struct {
rootOpts *options
outputFormat string
outputFile string
}

func (exo *exportOptions) setAndValidate() error {
if err := exo.rootOpts.setAndValidate(); err != nil {
return err
}

if exo.rootOpts.localOnly {
logrus.Warn("ignoring flag '--local-only'")
}

switch OutputFormat(exo.outputFormat) {
case YAML:
case JSON:
case LOG:
default:
return fmt.Errorf("unsuported output format")
}

if exo.outputFile != "" && OutputFormat(exo.outputFormat) == LOG {
logrus.Warnf("ignoring --output-file as --output-format is 'log'")
}

return nil
}

var exportOpts = &exportOptions{}

func addExport(topLevel *cobra.Command) {
exo := exportOpts
exo.rootOpts = rootOpts

cmd := &cobra.Command{
Use: "export",
Short: "Export list of 'latest' upstream versions available",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(*cobra.Command, []string) error {
if err := exo.setAndValidate(); err != nil {
return err
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runExport(exo)
},
}

cmd.PersistentFlags().StringVar(
&exportOpts.outputFormat,
"output-format",
"log",
"format of the output. Supported values are 'log', 'json' and 'yaml'. If not provided it will default to printing log.",
)

cmd.PersistentFlags().StringVar(
&exportOpts.outputFile,
"output-file",
"",
"file to write output. Use only if --output-format is 'json' or 'yaml'. If not specified will default to dependency_output.(json|yaml).",
)

topLevel.AddCommand(cmd)
}

// runValidate is the function invoked by 'addValidate', responsible for
// validating dependencies in a specified configuration file.
func runExport(opts *exportOptions) error {
client := dependency.NewClient()

updates, err := client.RemoteExport(opts.rootOpts.configFile)
if err != nil {
return err
}

return output(opts, updates)
}

func output(opts *exportOptions, updates []dependency.VersionUpdate) error {
if OutputFormat(opts.outputFormat) == LOG {
return outputLog(updates)
}
return outputFile(opts, updates)
}

func outputLog(updates []dependency.VersionUpdate) error {
for _, update := range updates {
if update.Version == update.NewVersion {
logrus.Debugf(
"No update available for dependency %v: %v (latest: %v)\n",
update.Name,
update.Version,
update.NewVersion,
)
} else {
fmt.Printf(
"Update available for dependency %v: %v (current: %v)\n",
update.Name,
update.NewVersion,
update.Version,
)
}
}
return nil
}

func outputFile(opts *exportOptions, updates []dependency.VersionUpdate) error {
var outputFile string
if opts.outputFile != "" {
outputFile = opts.outputFile
} else {
outputFile = fmt.Sprintf("%v.%v", defaultOutputFileName, opts.outputFormat)
}

f, err := os.Create(outputFile)
if err != nil {
return errors.Wrapf(err, "failed to open output file")
}
defer f.Close()

var b []byte
switch OutputFormat(opts.outputFormat) {
case YAML:
b, err = yaml.Marshal(updates)
if err != nil {
return err
}
case JSON:
b, err = json.Marshal(updates)
if err != nil {
return err
}
}

if _, err := f.Write(b); err != nil {
return errors.Wrapf(err, "failed to write output")
}
return nil
}
90 changes: 69 additions & 21 deletions dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,69 @@ func (c *Client) RemoteCheck(dependencyFilePath string) ([]string, error) {

updates := make([]string, 0)

for _, dep := range externalDeps.Dependencies {
log.Debugf("Examining dependency: %s", dep.Name)
versionUpdateInfos, err := c.checkUpstreamVersions(externalDeps.Dependencies)
if err != nil {
return nil, err
}

for _, vu := range versionUpdateInfos {
if vu.updateAvailable {
updates = append(
updates,
fmt.Sprintf(
"Update available for dependency %s: %s (current: %s)",
vu.name,
vu.latest.Version,
vu.current.Version,
),
)
} else {
log.Debugf(
"No update available for dependency %s: %s (latest: %s)\n",
vu.name,
vu.current.Version,
vu.latest.Version,
)
}
}

return updates, nil
}

func (c *Client) RemoteExport(dependencyFilePath string) ([]VersionUpdate, error) {
externalDeps, err := fromFile(dependencyFilePath)
if err != nil {
return nil, err
}

versionUpdates := []VersionUpdate{}

versionUpdatesInfos, err := c.checkUpstreamVersions(externalDeps.Dependencies)
if err != nil {
return nil, err
}

for _, vui := range versionUpdatesInfos {
if vui.updateAvailable {
versionUpdates = append(versionUpdates, VersionUpdate{
Name: vui.name,
Version: vui.current.Version,
NewVersion: vui.latest.Version,
})
} else {
versionUpdates = append(versionUpdates, VersionUpdate{
Name: vui.name,
Version: vui.current.Version,
NewVersion: vui.current.Version,
})
}
}
return versionUpdates, nil
}

func (c *Client) checkUpstreamVersions(deps []*Dependency) ([]versionUpdateInfo, error) {
versionUpdates := []versionUpdateInfo{}
for _, dep := range deps {
if dep.Upstream == nil {
continue
}
Expand Down Expand Up @@ -283,25 +343,13 @@ func (c *Client) RemoteCheck(dependencyFilePath string) ([]string, error) {
return nil, err
}

if updateAvailable {
updates = append(
updates,
fmt.Sprintf(
"Update available for dependency %s: %s (current: %s)",
dep.Name,
latestVersion.Version,
currentVersion.Version,
),
)
} else {
log.Debugf(
"No update available for dependency %s: %s (latest: %s)\n",
dep.Name,
currentVersion.Version,
latestVersion.Version,
)
}
versionUpdates = append(versionUpdates, versionUpdateInfo{
name: dep.Name,
current: currentVersion,
latest: latestVersion,
updateAvailable: updateAvailable,
})
}

return updates, nil
return versionUpdates, nil
}
22 changes: 22 additions & 0 deletions dependency/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ func TestDummyRemote(t *testing.T) {
require.Nil(t, err)
}

func TestDummyRemoteExportWithoutUpdate(t *testing.T) {
client := NewClient()

updates, err := client.RemoteExport("../testdata/remote-dummy.yaml")
require.Nil(t, err)
require.NotEmpty(t, updates)
require.Equal(t, updates[0].Name, "example")
require.Equal(t, updates[0].Version, "1.0.0")
require.Equal(t, updates[0].NewVersion, "1.0.0")
}

func TestDummyRemoteExportWithUpdate(t *testing.T) {
client := NewClient()

updates, err := client.RemoteExport("../testdata/remote-dummy-with-update.yaml")
require.Nil(t, err)
require.NotEmpty(t, updates)
require.Equal(t, updates[0].Name, "example")
require.Equal(t, updates[0].Version, "0.0.1")
require.Equal(t, updates[0].NewVersion, "1.0.0")
}

func TestRemoteConstraint(t *testing.T) {
client := NewClient()

Expand Down
15 changes: 15 additions & 0 deletions dependency/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ const (
Random VersionScheme = "random"
)

type versionUpdateInfo struct {
name string
current Version
latest Version
updateAvailable bool
}

// VersionUpdate represents the schema of the output format
// The output format is dictated by exportOptions.outputFormat
type VersionUpdate struct {
Name string `yaml:"name" json:"name"`
Version string `yaml:"version" json:"version"`
NewVersion string `yaml:"new_version" json:"new_version"`
}

// VersionSensitivity informs us on how to compare whether a version is more
// recent than another, for example to only notify on new major versions
// Only applicable to Semver versioning
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
golang.org/x/mod v0.4.1 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/oauth2 v0.0.0-20210112200429-01de73cf58bd
golang.org/x/tools v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
sigs.k8s.io/release-utils v0.2.0
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,9 @@ golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3 h1:kzM6+9dur93BcC2kVlYl34cHU+TYZLanmpSJHVMmL64=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -523,8 +524,9 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20201023174141-c8cfbd0f21e6 h1:rbvTkL9AkFts1cgI78+gG6Yu1pwaqX6hjSJAatB78E4=
golang.org/x/tools v0.0.0-20201023174141-c8cfbd0f21e6/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
6 changes: 6 additions & 0 deletions testdata/remote-dummy-with-update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: example
version: 0.0.1
upstream:
flavour: dummy
url: example/example