Skip to content

Commit

Permalink
Removes utility code formerly used by Docker (#211)
Browse files Browse the repository at this point in the history
We had a lot of redundant code that was deleted with the extension
subcommand. However, code analysis failed to notice other parts that are
only referenced in their own tests. This completes removal of the extra
code made for the extension experiment.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
  • Loading branch information
codefromthecrypt authored May 11, 2021
1 parent d5b16dc commit 64760a4
Show file tree
Hide file tree
Showing 22 changed files with 34 additions and 898 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/klauspost/compress v1.12.2 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/mattn/go-shellwords v1.0.11
github.com/mholt/archiver/v3 v3.5.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pierrec/lz4/v4 v4.1.6 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-shellwords v1.0.11 h1:vCoR9VPpsk/TZFW2JwK5I9S0xdrtUq2bph6/YjEPnaw=
github.com/mattn/go-shellwords v1.0.11/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mholt/archiver/v3 v3.5.0 h1:nE8gZIrw66cu4osS/U7UW7YDuGMHssxKutU8IfWxwWE=
github.com/mholt/archiver/v3 v3.5.0/go.mod h1:qqTTPUK/HZPFgFQ/TJ3BzvTpF/dPtFVJXdQbCmeMxwc=
Expand Down
4 changes: 2 additions & 2 deletions pkg/binary/envoy/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (r *Runtime) Run(ctx context.Context, args []string) error {
// handlers, and they expect the process to still be running. For example, this allows admin API hooks.
cmd := exec.Command(r.opts.EnvoyPath, args...) // #nosec -> users can run whatever binary they like!
cmd.Dir = r.opts.WorkingDir
cmd.Stdout = r.IO.Out
cmd.Stderr = r.IO.Err
cmd.Stdout = r.Out
cmd.Stderr = r.Err
cmd.SysProcAttr = sysProcAttr()
r.cmd = cmd

Expand Down
5 changes: 3 additions & 2 deletions pkg/binary/envoy/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"

"github.com/tetratelabs/getenvoy/pkg/globals"
ioutil "github.com/tetratelabs/getenvoy/pkg/util/io"
)

// NewRuntime creates a new Runtime that runs envoy in globals.RunOpts WorkingDir
Expand All @@ -37,7 +37,8 @@ type Runtime struct {
opts *globals.RunOpts

cmd *exec.Cmd
IO ioutil.StdStreams
Out io.Writer
Err io.Writer

adminAddress, adminAddressPath string

Expand Down
14 changes: 4 additions & 10 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/spf13/cobra"

"github.com/tetratelabs/getenvoy/pkg/globals"
"github.com/tetratelabs/getenvoy/pkg/util/exec"
"github.com/tetratelabs/getenvoy/pkg/version"
)

Expand Down Expand Up @@ -106,15 +105,10 @@ func handleFlagOverrides(o *globals.GlobalOpts, homeDirFlag, manifestURLFlag str
func Execute(cmd *cobra.Command) error {
actualCmd, err := cmd.ExecuteC()
if actualCmd != nil && err != nil { // both are always true on error
var serr exec.ShutdownError
if errors.As(err, &serr) { // in case of ShutdownError, we want to avoid any wrapper messages
cmd.PrintErrln("NOTE:", serr.Error())
} else {
cmd.PrintErrln("Error:", err.Error())
// actualCmd ensures command path includes the subcommand (ex "extension run")
cmd.PrintErrf("\nRun '%v --help' for usage.\n", actualCmd.CommandPath())
return err
}
cmd.PrintErrln("Error:", err.Error())
// actualCmd ensures command path includes the subcommand (ex "extension run")
cmd.PrintErrf("\nRun '%v --help' for usage.\n", actualCmd.CommandPath())
return err
}
return nil
}
19 changes: 15 additions & 4 deletions pkg/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
package cmd_test

import (
"bytes"
"fmt"
"os"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

rootcmd "github.com/tetratelabs/getenvoy/pkg/cmd"
"github.com/tetratelabs/getenvoy/pkg/globals"
cmdtest "github.com/tetratelabs/getenvoy/pkg/test/cmd"
)

func TestGetEnvoyValidateArgs(t *testing.T) {
Expand Down Expand Up @@ -55,7 +56,7 @@ func TestGetEnvoyValidateArgs(t *testing.T) {
test := test // pin! see https://github.com/kyoh86/scopelint for why

t.Run(test.name, func(t *testing.T) {
c, stdout, stderr := cmdtest.NewRootCommand(o)
c, stdout, stderr := newRootCommand(o)
c.SetArgs(test.args)
err := rootcmd.Execute(c)

Expand Down Expand Up @@ -116,7 +117,7 @@ func TestGetEnvoyHomeDir(t *testing.T) {
}

o := &globals.GlobalOpts{}
c, stdout, stderr := cmdtest.NewRootCommand(o)
c, stdout, stderr := newRootCommand(o)
c.SetArgs(test.args)
err := rootcmd.Execute(c)

Expand Down Expand Up @@ -175,7 +176,7 @@ func TestGetEnvoyManifest(t *testing.T) {
}

o := &globals.GlobalOpts{}
c, stdout, stderr := cmdtest.NewRootCommand(o)
c, stdout, stderr := newRootCommand(o)
c.SetArgs(append(test.args, "help"))
err := rootcmd.Execute(c)

Expand All @@ -198,3 +199,13 @@ func requireSetenv(t *testing.T, key, value string) func() {
require.NoError(t, e, `error reverting env variable %s=%s`, key, previous)
}
}

// newRootCommand initializes a command with buffers for stdout and stderr.
func newRootCommand(o *globals.GlobalOpts) (c *cobra.Command, stdout, stderr *bytes.Buffer) {
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)
c = rootcmd.NewRoot(o)
c.SetOut(stdout)
c.SetErr(stderr)
return c, stdout, stderr
}
8 changes: 2 additions & 6 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/tetratelabs/getenvoy/pkg/binary/envoy"
"github.com/tetratelabs/getenvoy/pkg/binary/envoy/debug"
"github.com/tetratelabs/getenvoy/pkg/globals"
ioutil "github.com/tetratelabs/getenvoy/pkg/util/io"
)

// NewRunCmd create a command responsible for starting an Envoy process
Expand Down Expand Up @@ -95,11 +94,8 @@ func InitializeRunOpts(o *globals.GlobalOpts, reference string) error {
// This is exposed for re-use in "getenvoy extension run"
func Run(o *globals.GlobalOpts, cmd *cobra.Command, args []string) error {
r := envoy.NewRuntime(&o.RunOpts)
r.IO = ioutil.StdStreams{
In: cmd.InOrStdin(),
Out: cmd.OutOrStdout(),
Err: cmd.ErrOrStderr(),
}
r.Out = cmd.OutOrStdout()
r.Err = cmd.ErrOrStderr()
debug.EnableAll(r)
return r.Run(cmd.Context(), args)
}
7 changes: 3 additions & 4 deletions pkg/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
rootcmd "github.com/tetratelabs/getenvoy/pkg/cmd"
"github.com/tetratelabs/getenvoy/pkg/globals"
"github.com/tetratelabs/getenvoy/pkg/manifest"
"github.com/tetratelabs/getenvoy/pkg/test/cmd"
manifesttest "github.com/tetratelabs/getenvoy/pkg/test/manifest"
"github.com/tetratelabs/getenvoy/pkg/test/morerequire"
)
Expand Down Expand Up @@ -54,7 +53,7 @@ func TestGetEnvoyRunValidateFlag(t *testing.T) {

t.Run(test.name, func(t *testing.T) {
// Run "getenvoy run"
c, stdout, stderr := cmd.NewRootCommand(&globals.GlobalOpts{})
c, stdout, stderr := newRootCommand(&globals.GlobalOpts{})
c.SetArgs(test.args)
err := rootcmd.Execute(c)

Expand All @@ -72,7 +71,7 @@ func TestGetEnvoyRun(t *testing.T) {
defer cleanup()

// Run "getenvoy run standard:1.17.1 -- -c envoy.yaml"
c, stdout, stderr := cmd.NewRootCommand(&o.GlobalOpts)
c, stdout, stderr := newRootCommand(&o.GlobalOpts)
c.SetArgs([]string{"run", reference.Latest, "--", "-c", "envoy.yaml"})
err := rootcmd.Execute(c)

Expand All @@ -92,7 +91,7 @@ func TestGetEnvoyRunFailWithUnknownVersion(t *testing.T) {
defer cleanup()

o.EnvoyPath = "" // force lookup of version flag
c, _, stderr := cmd.NewRootCommand(&o.GlobalOpts)
c, _, stderr := newRootCommand(&o.GlobalOpts)

// Run "getenvoy run unknown"
version := "unknown"
Expand Down
34 changes: 0 additions & 34 deletions pkg/test/cmd/command.go

This file was deleted.

7 changes: 0 additions & 7 deletions pkg/test/morerequire/morerequire.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,3 @@ func RequireCaptureScript(t *testing.T, name string) (string, func()) {
}
return path, cleanup
}

// RequireAbs runs filepath.Abs and ensures there are no errors.
func RequireAbs(t *testing.T, f string) string {
f, err := filepath.Abs(f)
require.NoError(t, err, `error determining absolute path: %v`, f)
return f
}
34 changes: 0 additions & 34 deletions pkg/util/args/args.go

This file was deleted.

73 changes: 0 additions & 73 deletions pkg/util/args/args_test.go

This file was deleted.

Loading

0 comments on commit 64760a4

Please sign in to comment.