Skip to content

Commit

Permalink
Remove deps from go.mod for knative.dev/hack
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Sep 22, 2023
1 parent da6e74c commit b99242b
Show file tree
Hide file tree
Showing 29 changed files with 639 additions and 760 deletions.
10 changes: 2 additions & 8 deletions cmd/script/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/*
Copyright 2022 The Knative 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
https://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.
Expand All @@ -16,13 +13,10 @@ limitations under the License.

package main

import (
"github.com/wavesoftware/go-commandline"
"knative.dev/hack/pkg/inflator/cli"
)
import "knative.dev/hack/pkg/inflator/cli"

func main() {
commandline.New(new(cli.App)).ExecuteOrDie(cli.Options...)
cli.ExecuteOrDie(cli.Options...)
}

// RunMain is used by tests to run the main function.
Expand Down
25 changes: 12 additions & 13 deletions cmd/script/main_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/*
Copyright 2022 The Knative 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
https://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.
Expand All @@ -20,10 +17,9 @@ import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/wavesoftware/go-commandline"
main "knative.dev/hack/cmd/script"
"knative.dev/hack/pkg/inflator/cli"
"knative.dev/hack/pkg/utest/assert"
)

func TestMainFn(t *testing.T) {
Expand All @@ -33,20 +29,23 @@ func TestMainFn(t *testing.T) {
func() {
main.RunMain()
},
commandline.WithArgs("--help"),
commandline.WithOutput(&buf),
commandline.WithExit(func(c int) {
retcode = &c
}),
func(ex *cli.Execution) {
ex.Stdout = &buf
ex.Stderr = &buf
ex.Args = []string{"--help"}
ex.Exit = func(c int) {
retcode = &c
}
},
)
assert.Nil(t, retcode)
assert.Contains(t, buf.String(), "Script will extract Hack scripts")
assert.ContainsSubstring(t, buf.String(), "Script will extract Hack scripts")
}

func withOptions(fn func(), options ...commandline.Option) {
func withOptions(fn func(), options ...cli.Option) {
prev := cli.Options
cli.Options = options
defer func(p []commandline.Option) {
defer func(p []cli.Option) {
cli.Options = p
}(prev)
fn()
Expand Down
4 changes: 3 additions & 1 deletion embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

package hack

import "embed"
import (
"embed"
)

//go:embed *.sh
var Scripts embed.FS
4 changes: 2 additions & 2 deletions embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package hack_test
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"knative.dev/hack"
"knative.dev/hack/pkg/utest/assert"
"knative.dev/hack/pkg/utest/require"
)

func TestScriptsAreEmbedded(t *testing.T) {
Expand Down
20 changes: 0 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
module knative.dev/hack

go 1.18

require (
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.1
github.com/wavesoftware/go-commandline v1.0.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/wavesoftware/go-retcode v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
44 changes: 0 additions & 44 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,44 +0,0 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/wavesoftware/go-commandline v1.0.0 h1:n7nrFr1unfiUcF7shA1rYf+YhXB12pY8uNYqPgFsHio=
github.com/wavesoftware/go-commandline v1.0.0/go.mod h1:C9yRtwZxJSck99kk6SRRkOtC2ppQF/KDRy0yrzWJuHU=
github.com/wavesoftware/go-retcode v1.0.0 h1:Z53+VpIHMvRMtjS6jPScdihbAN1ks3lIJ5Mj32gCpno=
github.com/wavesoftware/go-retcode v1.0.0/go.mod h1:BLqIIXhB/PQ+izkkRGfSQgu95BDtMmUBuvTJ/gkSWVM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo=
48 changes: 48 additions & 0 deletions pkg/constraints/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// See: https://cs.opensource.google/go/x/exp/+/92128663:constraints/constraints.go

// Package constraints defines a set of useful constraints to be used
// with type parameters.
package constraints

// Signed is a constraint that permits any signed integer type.
// If future releases of Go add new predeclared signed integer types,
// this constraint will be modified to include them.
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}

// Unsigned is a constraint that permits any unsigned integer type.
// If future releases of Go add new predeclared unsigned integer types,
// this constraint will be modified to include them.
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

// Integer is a constraint that permits any integer type.
// If future releases of Go add new predeclared integer types,
// this constraint will be modified to include them.
type Integer interface {
Signed | Unsigned
}

// Float is a constraint that permits any floating-point type.
// If future releases of Go add new predeclared floating-point types,
// this constraint will be modified to include them.
type Float interface {
~float32 | ~float64
}

// Complex is a constraint that permits any complex numeric type.
// If future releases of Go add new predeclared complex numeric types,
// this constraint will be modified to include them.
type Complex interface {
~complex64 | ~complex128
}

// Ordered is a constraint that permits any ordered type: any type
// that supports the operators < <= >= >.
// If future releases of Go add new ordered types,
// this constraint will be modified to include them.
type Ordered interface {
Integer | Float | ~string
}
66 changes: 42 additions & 24 deletions pkg/inflator/cli/app.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
package cli

import (
"os"
"fmt"

"github.com/spf13/cobra"
"github.com/wavesoftware/go-commandline"
"knative.dev/hack/pkg/inflator/extract"
"knative.dev/hack/pkg/retcode"
)

// Options to override the commandline for testing purposes.
var Options []commandline.Option //nolint:gochecknoglobals
// Execute will execute the application.
func Execute(opts []Option) Result {
ex := Execution{}.Default().Configure(opts)
fl, err := parseArgs(&ex)
if err != nil {
return Result{
Execution: ex,
Err: err,
}
}
op := createOperation(fl, ex.Args)
return Result{
Execution: ex,
Err: op.Extract(ex),
}
}

// ExecuteOrDie will execute the application or perform os.Exit in case of error.
func ExecuteOrDie(opts ...Option) {
if r := Execute(opts); r.Err != nil {
r.PrintErrln(fmt.Sprintf("%v", r.Err))
r.Exit(retcode.Calc(r.Err))
}
}

type App struct{}
type usageErr struct{}

func (u usageErr) Error() string {
return `Hacks as Go self-extracting binary
Will extract Hack scripts to a temporary directory, and provide a source

Check failure on line 40 in pkg/inflator/cli/app.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

[trailing whitespace] reported by reviewdog 🐶 Will extract Hack scripts to a temporary directory, and provide a source Raw Output: pkg/inflator/cli/app.go:40:Will extract Hack scripts to a temporary directory, and provide a source
file path to requested shell script.
func (a App) Command() *cobra.Command {
fl := &flags{}
c := &cobra.Command{
Use: "script library.sh",
Short: "Script is a tool for running Hack scripts",
Long: "Script will extract Hack scripts to a temporary directory, " +
"and provide a source file path to requested script",
Example: `
# In Bash script
source "$(go run knative.dev/hack/cmd/script@latest library.sh)"`,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, argv []string) error {
op := createOperation(fl, argv)
return op.Extract(cmd)
},
}
c.SetOut(os.Stdout)
return fl.withFlags(c)
source "$(go run knative.dev/hack/cmd/script@latest library.sh)"
Usage:
script [flags] library.sh
Flags:
-h, --help help
-v, --verbose verbose output
`
}

func createOperation(fl *flags, argv []string) extract.Operation {
Expand Down
19 changes: 10 additions & 9 deletions pkg/inflator/cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"knative.dev/hack/pkg/inflator/cli"
"knative.dev/hack/pkg/inflator/extract"
"knative.dev/hack/pkg/utest/assert"
"knative.dev/hack/pkg/utest/require"
)

func TestApp(t *testing.T) {
func TestExecute(t *testing.T) {
tmpdir := t.TempDir()
t.Setenv(extract.HackScriptsDirEnvVar, tmpdir)
t.Setenv(cli.ManualVerboseEnvVar, "true")
c := cli.App{}.Command()
var (
outb bytes.Buffer
errb bytes.Buffer
)
c.SetOut(&outb)
c.SetErr(&errb)
c.SetArgs([]string{"e2e-tests.sh"})
err := c.Execute()

require.NoError(t, err)
r := cli.Execute([]cli.Option{func(ex *cli.Execution) {
ex.Args = []string{"e2e-tests.sh"}
ex.Stdout = &outb
ex.Stderr = &errb
}})

require.NoError(t, r.Err)
assert.Equal(t, outb.String(), tmpdir+"/e2e-tests.sh\n")
assert.Equal(t, errb.String(), "")
}
51 changes: 51 additions & 0 deletions pkg/inflator/cli/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cli

import (
"io"
"os"
)

// Execution is used to execute a command.
type Execution struct {
Args []string
Stdout io.Writer
Stderr io.Writer
Exit func(code int)
}

// Default will set default values for the execution.
func (e Execution) Default() Execution {
if e.Stdout == nil {
e.Stdout = os.Stdout
}
if e.Stderr == nil {
e.Stderr = os.Stderr
}
if e.Exit == nil {
e.Exit = os.Exit
}
if len(e.Args) == 0 {
e.Args = os.Args[1:]
}
return e
}

// Configure will configure the execution.
func (e Execution) Configure(opts []Option) Execution {
for _, opt := range opts {
opt(&e)
}
return e
}

// Option is used to configure an App.
type Option func(*Execution)

// Options to override the commandline for testing purposes.
var Options []Option //nolint:gochecknoglobals

// Result is a result of execution.
type Result struct {
Execution
Err error
}
Loading

0 comments on commit b99242b

Please sign in to comment.