Skip to content

Commit

Permalink
refactor: moved github output variable handling with one level deeper
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Nov 25, 2024
1 parent c437762 commit 339a20e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 176 deletions.
64 changes: 0 additions & 64 deletions action.yml

This file was deleted.

33 changes: 10 additions & 23 deletions cmd/k6registry/action.go → cmd/action.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand All @@ -9,27 +9,16 @@ import (
"net/http"
"os"
"path/filepath"
"time"
)

//nolint:forbidigo
func emitOutput() error {
out := getenv("INPUT_OUT", "")
if len(out) == 0 {
api := getenv("INPUT_API", "")
if len(api) == 0 {
return nil
}

out = filepath.Join(api, "registry.json")
}

ref := getenv("INPUT_REF", "")
if len(ref) == 0 {
return nil
}
func isGitHubAction() bool {
return os.Getenv("GITHUB_ACTIONS") == "true"
}

ghOutput := getenv("GITHUB_OUTPUT", "")
//nolint:forbidigo
func emitOutput(ctx context.Context, out string, ref string) error {
ghOutput := os.Getenv("GITHUB_OUTPUT")
if len(ghOutput) == 0 {
return nil
}
Expand All @@ -39,7 +28,7 @@ func emitOutput() error {
return err
}

changed := isChanged(ref, out)
changed := isChanged(ctx, ref, out)

slog.Debug("Detect change", "changed", changed, "ref", ref)

Expand All @@ -52,10 +41,10 @@ func emitOutput() error {
}

//nolint:forbidigo
func isChanged(refURL string, localFile string) bool {
func isChanged(ctx context.Context, refURL string, localFile string) bool {
client := &http.Client{Timeout: httpTimeout}

req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, refURL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, refURL, nil)
if err != nil {
return true
}
Expand All @@ -79,5 +68,3 @@ func isChanged(refURL string, localFile string) bool {

return !bytes.Equal(refData, localData)
}

const httpTimeout = 10 * time.Second
20 changes: 16 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type options struct {
api string
test []string
origin string
ref string
}

// New creates new cobra command for exec command.
Expand Down Expand Up @@ -73,6 +74,7 @@ func New(levelVar *slog.LevelVar) (*cobra.Command, error) {
flags.StringVarP(&opts.out, "out", "o", "", "write output to file instead of stdout")
flags.StringVar(&opts.api, "api", "", "write outputs to directory instead of stdout")
flags.StringVar(&opts.origin, "origin", "", "external registry URL for default values")
flags.StringVar(&opts.ref, "ref", "", "reference output URL for change detection")
flags.StringSliceVar(&opts.test, "test", []string{}, "test api path(s) (example: /registry.json,/catalog.json)")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "no output, only validation")
flags.BoolVar(&opts.loose, "loose", false, "skip JSON schema validation")
Expand Down Expand Up @@ -146,8 +148,12 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
return err
}

return postRun(ctx, registry, output, opts)
}

func postRun(ctx context.Context, registry k6registry.Registry, output io.Writer, opts *options) error {
if len(opts.api) != 0 {
if err = writeAPI(registry, opts.api); err != nil {
if err := writeAPI(registry, opts.api); err != nil {
return err
}

Expand All @@ -160,11 +166,17 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
}
}

if opts.quiet {
return nil
if !opts.quiet {
if err := writeOutput(registry, output, opts.compact, false); err != nil {
return err
}
}

if isGitHubAction() && len(opts.out) > 0 && len(opts.ref) > 0 {
return emitOutput(ctx, opts.out, opts.ref)
}

return writeOutput(registry, output, opts.compact, false)
return nil
}

//nolint:forbidigo
Expand Down
84 changes: 2 additions & 82 deletions cmd/k6registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"log"
"log/slog"
"os"
"strings"

"github.com/google/shlex"
"github.com/grafana/k6registry/cmd"
sloglogrus "github.com/samber/slog-logrus/v2"
"github.com/sirupsen/logrus"
Expand All @@ -28,11 +26,12 @@ func initLogging() *slog.LevelVar {
return levelVar
}

//nolint:forbidigo
func main() {
log.SetFlags(0)
log.Writer()

runCmd(newCmd(getArgs(), initLogging()))
runCmd(newCmd(os.Args[1:], initLogging()))
}

func newCmd(args []string, levelVar *slog.LevelVar) *cobra.Command {
Expand All @@ -51,83 +50,4 @@ func runCmd(cmd *cobra.Command) {
if err := cmd.Execute(); err != nil {
log.Fatal(formatError(err))
}

if isGitHubAction() {
if err := emitOutput(); err != nil {
log.Fatal(formatError(err))
}
}
}

//nolint:forbidigo
func isGitHubAction() bool {
return os.Getenv("GITHUB_ACTIONS") == "true"
}

//nolint:forbidigo
func getArgs() []string {
if !isGitHubAction() {
return os.Args[1:]
}

var args []string

if getenv("INPUT_QUIET", "false") == "true" {
args = append(args, "--quiet")
}

if getenv("INPUT_VERBOSE", "false") == "true" {
args = append(args, "--verbose")
}

if getenv("INPUT_LOOSE", "false") == "true" {
args = append(args, "--loose")
}

if getenv("INPUT_LINT", "false") == "true" {
args = append(args, "--lint")
}

if getenv("INPUT_COMPACT", "false") == "true" {
args = append(args, "--compact")
}

if catalog := getenv("INPUT_CATALOG", ""); len(catalog) != 0 {
args = append(args, "--catalog", catalog)
}

if api := getenv("INPUT_API", ""); len(api) != 0 {
args = append(args, "--api", api)
}

if out := getenv("INPUT_OUT", ""); len(out) != 0 {
args = append(args, "--out", out)
}

if paths := getenv("INPUT_TEST", ""); len(paths) != 0 {
parts, err := shlex.Split(paths)
if err == nil {
paths = strings.Join(parts, ",")
}

args = append(args, "--test", paths)
}

if origin := getenv("INPUT_ORIGIN", ""); len(origin) != 0 {
args = append(args, "--origin", origin)
}

args = append(args, getenv("INPUT_IN", ""))

return args
}

//nolint:forbidigo
func getenv(name string, defval string) string {
value, found := os.LookupEnv(name)
if found {
return value
}

return defval
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/cli/go-gh/v2 v2.11.0
github.com/go-git/go-git/v5 v5.12.0
github.com/google/go-github/v62 v62.0.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/grafana/clireadme v0.1.0
github.com/grafana/k6lint v0.3.1
github.com/narqo/go-badge v0.0.0-20230821190521-c9a75c019a59
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwM
github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/grafana/clireadme v0.1.0 h1:KYEYSnYdSzmHf3bufaK6fQZ5j4dzvM/T+G6Ba+qNnAM=
github.com/grafana/clireadme v0.1.0/go.mod h1:Wy4KIG2ZBGMYAYyF9l7qAy+yoJVasqk/txsRgoRI3gc=
github.com/grafana/k6foundry v0.3.0 h1:C+6dPbsOv7Uq4hEhBFNuYqmTdE9jQ0VqhXqBDtMkVTE=
Expand Down

0 comments on commit 339a20e

Please sign in to comment.