diff --git a/cmd/aws/main.go b/cmd/aws/main.go index 6953413..93fe6d8 100644 --- a/cmd/aws/main.go +++ b/cmd/aws/main.go @@ -1,44 +1,69 @@ package main import ( + "context" "fmt" "io" "os" + "runtime/debug" "get.porter.sh/mixin/aws/pkg/aws" + "get.porter.sh/porter/pkg/cli" "github.com/spf13/cobra" + "go.opentelemetry.io/otel/attribute" ) func main() { - cmd, err := buildRootCommand(os.Stdin) - if err != nil { - fmt.Printf("err: %s\n", err) - os.Exit(1) - } - if err := cmd.Execute(); err != nil { - fmt.Printf("err: %s\n", err) - os.Exit(1) + run := func() int { + ctx := context.Background() + m := aws.New() + if err := m.ConfigureLogging(ctx); err != nil { + fmt.Println(err) + os.Exit(cli.ExitCodeErr) + } + cmd := buildRootCommand(m, os.Stdin) + + // We don't have tracing working inside a bundle working currently. + // We are using StartRootSpan anyway because it creates a TraceLogger and sets it + // on the context, so we can grab it later + ctx, log := m.StartRootSpan(ctx, "aws") + defer func() { + // Capture panics and trace them + if panicErr := recover(); panicErr != nil { + log.Error(fmt.Errorf("%s", panicErr), + attribute.Bool("panic", true), + attribute.String("stackTrace", string(debug.Stack()))) + log.EndSpan() + m.Close() + os.Exit(cli.ExitCodeErr) + } else { + log.Close() + m.Close() + } + }() + + if err := cmd.ExecuteContext(ctx); err != nil { + return cli.ExitCodeErr + } + return cli.ExitCodeSuccess } + os.Exit(run()) } -func buildRootCommand(in io.Reader) (*cobra.Command, error) { - m, err := aws.New() - if err != nil { - return nil, err - } - m.Config.In = in +func buildRootCommand(m *aws.Mixin, in io.Reader) *cobra.Command { cmd := &cobra.Command{ Use: "aws", Long: "A skeleton mixin to use for building other mixins for porter 👩🏽‍✈️", PersistentPreRun: func(cmd *cobra.Command, args []string) { // Enable swapping out stdout/stderr for testing - m.Config.Out = cmd.OutOrStdout() - m.Config.Err = cmd.OutOrStderr() + m.In = in + m.Out = cmd.OutOrStdout() + m.Err = cmd.OutOrStderr() }, SilenceUsage: true, } - cmd.PersistentFlags().BoolVar(&m.Config.DebugMode, "debug", false, "Enable debug logging") + cmd.PersistentFlags().BoolVar(&m.DebugMode, "debug", false, "Enable debug logging") cmd.AddCommand(buildVersionCommand(m)) cmd.AddCommand(buildSchemaCommand(m)) @@ -48,5 +73,5 @@ func buildRootCommand(in io.Reader) (*cobra.Command, error) { cmd.AddCommand(buildUpgradeCommand(m)) cmd.AddCommand(buildUninstallCommand(m)) - return cmd, nil + return cmd } diff --git a/go.mod b/go.mod index 6ce64ad..77be281 100644 --- a/go.mod +++ b/go.mod @@ -10,11 +10,12 @@ replace github.com/spf13/viper => github.com/getporter/viper v1.7.1-porter.2.0.2 require ( get.porter.sh/magefiles v0.3.2 - get.porter.sh/porter v1.0.0-rc.1 + get.porter.sh/porter v1.0.0-rc.2 github.com/ghodss/yaml v1.0.0 github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 github.com/xeipuuv/gojsonschema v1.2.0 + go.opentelemetry.io/otel v1.9.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -112,7 +113,6 @@ require ( github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect - go.opentelemetry.io/otel v1.9.0 // indirect go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.9.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.9.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.9.0 // indirect @@ -124,12 +124,12 @@ require ( go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.8.0 // indirect go.uber.org/zap v1.23.0 // indirect - golang.org/x/net v0.0.0-20220907135653-1e95f45603a7 // indirect + golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect golang.org/x/sync v0.0.0-20220907140024-f12130a52804 // indirect golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect - google.golang.org/genproto v0.0.0-20220908141613-51c1cc9bc6d0 // indirect + google.golang.org/genproto v0.0.0-20220915135415-7fd63a7952de // indirect google.golang.org/grpc v1.49.0 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index f4c5cd4..29a74b7 100644 --- a/go.sum +++ b/go.sum @@ -45,8 +45,8 @@ contrib.go.opencensus.io/exporter/stackdriver v0.13.4/go.mod h1:aXENhDJ1Y4lIg4EU dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= get.porter.sh/magefiles v0.3.2 h1:RDJini5LBQ8pYnc0r8M6cX2Pk+CPsCtyFwO3fy8Jb5g= get.porter.sh/magefiles v0.3.2/go.mod h1:w8ikniAFHO7AVvWayyWF7ViHoh4tNKwTxtKpO1FyGkU= -get.porter.sh/porter v1.0.0-rc.1 h1:8sYXXW2dwp5V5Llk19f4o/KAkj7xwZo1HNRl49m0r4w= -get.porter.sh/porter v1.0.0-rc.1/go.mod h1:ZDh+OB2lJJL5aA7zBLhWRE9m9DhRxH0iVW88OoifL0c= +get.porter.sh/porter v1.0.0-rc.2 h1:xclJNxXqlGaozFnSb82JM+B5I7cnJHY3q/W7w+FDONE= +get.porter.sh/porter v1.0.0-rc.2/go.mod h1:EGPOxhMu44dRnwKdcedUtbMaFBrFrYzG1ybnHVYTrDs= github.com/Antonboom/errname v0.1.5/go.mod h1:DugbBstvPFQbv/5uLcRRzfrNqKE9tVdVCqWCLp6Cifo= github.com/Antonboom/nilnil v0.1.0/go.mod h1:PhHLvRPSghY5Y7mX4TW+BHZQYo1A8flE5H20D3IPZBo= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= @@ -1060,8 +1060,8 @@ golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220907135653-1e95f45603a7 h1:1WGATo9HAhkWMbfyuVU0tEFP88OIkUvwaHFveQPvzCQ= -golang.org/x/net v0.0.0-20220907135653-1e95f45603a7/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI= +golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -1373,8 +1373,8 @@ google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6D google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= -google.golang.org/genproto v0.0.0-20220908141613-51c1cc9bc6d0 h1:bMz0aY2wd9TwUp9M7QfjBWuQqaFD/ZaTtvDpPDCo2Ow= -google.golang.org/genproto v0.0.0-20220908141613-51c1cc9bc6d0/go.mod h1:rQWNQYp1kbHR3+n5cARSTCF5rlJOttUn8yIhRklGAWQ= +google.golang.org/genproto v0.0.0-20220915135415-7fd63a7952de h1:5ANeKFmGdtiputJJYeUVg8nTGA/1bEirx4CgzcnPSx8= +google.golang.org/genproto v0.0.0-20220915135415-7fd63a7952de/go.mod h1:0Nb8Qy+Sk5eDzHnzlStwW3itdNaWoZA5XeSG+R3JHSo= google.golang.org/grpc v1.8.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= diff --git a/pkg/aws/aws.go b/pkg/aws/aws.go index 6244147..3a70b67 100644 --- a/pkg/aws/aws.go +++ b/pkg/aws/aws.go @@ -6,15 +6,15 @@ import ( type Mixin struct { // Config is a specialized context with additional runtime settings. - Config runtime.RuntimeConfig + runtime.RuntimeConfig } // New aws mixin client, initialized with useful defaults. -func New() (*Mixin, error) { +func New() *Mixin { m := &Mixin{ - Config: runtime.NewConfig(), + RuntimeConfig: runtime.NewConfig(), } m.SetUserAgent() - return m, nil + return m } diff --git a/pkg/aws/build.go b/pkg/aws/build.go index 6f51546..19ff2e0 100644 --- a/pkg/aws/build.go +++ b/pkg/aws/build.go @@ -6,10 +6,10 @@ import "fmt" // for an invocation image using this mixin func (m *Mixin) Build() error { // TODO: This gets whatever the latest version of the cli is, there isn't a way for us to say what version we are using - fmt.Fprintln(m.Config.Out, `RUN apt-get update && apt-get install -y --no-install-recommends curl unzip libc6 less groff`) - fmt.Fprintln(m.Config.Out, `RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m).zip" -o "awscliv2.zip"`) - fmt.Fprintln(m.Config.Out, `RUN unzip awscliv2.zip`) - fmt.Fprintln(m.Config.Out, `RUN ./aws/install`) - fmt.Fprintln(m.Config.Out, `RUN rm -fr awscliv2.zip ./aws`) + fmt.Fprintln(m.Out, `RUN apt-get update && apt-get install -y --no-install-recommends curl unzip libc6 less groff`) + fmt.Fprintln(m.Out, `RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m).zip" -o "awscliv2.zip"`) + fmt.Fprintln(m.Out, `RUN unzip awscliv2.zip`) + fmt.Fprintln(m.Out, `RUN ./aws/install`) + fmt.Fprintln(m.Out, `RUN rm -fr awscliv2.zip ./aws`) return nil } diff --git a/pkg/aws/config.go b/pkg/aws/config.go index afddae0..ada3d09 100644 --- a/pkg/aws/config.go +++ b/pkg/aws/config.go @@ -13,11 +13,11 @@ const AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV" func (m *Mixin) SetUserAgent() { value := []string{pkg.UserAgent(), m.UserAgent()} - if agentStr, ok := m.Config.LookupEnv(AWS_EXECUTION_ENV); ok { + if agentStr, ok := m.LookupEnv(AWS_EXECUTION_ENV); ok { value = append(value, agentStr) } - m.Config.Setenv(AWS_EXECUTION_ENV, strings.Join(value, " ")) + m.Setenv(AWS_EXECUTION_ENV, strings.Join(value, " ")) } func (m *Mixin) UserAgent() string { diff --git a/pkg/aws/config_test.go b/pkg/aws/config_test.go index 06f6b3c..5c687f5 100644 --- a/pkg/aws/config_test.go +++ b/pkg/aws/config_test.go @@ -15,5 +15,5 @@ func TestSetUserAgent(t *testing.T) { m.SetUserAgent() expected := "getporter/aws/" + pkg.Version - require.Contains(t, m.Config.Getenv(AWS_EXECUTION_ENV), expected) + require.Contains(t, m.Getenv(AWS_EXECUTION_ENV), expected) } diff --git a/pkg/aws/execute.go b/pkg/aws/execute.go index e1504c6..7b9fa55 100644 --- a/pkg/aws/execute.go +++ b/pkg/aws/execute.go @@ -9,7 +9,7 @@ import ( func (m *Mixin) loadAction(ctx context.Context) (*Action, error) { var action Action - err := builder.LoadAction(ctx, m.Config, "", func(contents []byte) (interface{}, error) { + err := builder.LoadAction(ctx, m.RuntimeConfig, "", func(contents []byte) (interface{}, error) { err := yaml.Unmarshal(contents, &action) return &action, err }) @@ -22,6 +22,6 @@ func (m *Mixin) Execute(ctx context.Context) error { return err } - _, err = builder.ExecuteSingleStepAction(ctx, m.Config, action) + _, err = builder.ExecuteSingleStepAction(ctx, m.RuntimeConfig, action) return err } diff --git a/pkg/aws/execute_test.go b/pkg/aws/execute_test.go index c33ef6f..b3f0beb 100644 --- a/pkg/aws/execute_test.go +++ b/pkg/aws/execute_test.go @@ -41,21 +41,21 @@ func TestMixin_Execute(t *testing.T) { ctx := context.Background() m := NewTestMixin(t) - m.Config.Setenv(test.ExpectedCommandEnv, tc.wantCommand) + m.Setenv(test.ExpectedCommandEnv, tc.wantCommand) mixinInputB, err := ioutil.ReadFile(tc.file) require.NoError(t, err) - m.Config.In = bytes.NewBuffer(mixinInputB) + m.In = bytes.NewBuffer(mixinInputB) err = m.Execute(ctx) require.NoError(t, err, "execute failed") if tc.wantOutput == "" { - outputs, _ := m.Config.FileSystem.ReadDir("/cnab/app/porter/outputs") + outputs, _ := m.FileSystem.ReadDir("/cnab/app/porter/outputs") assert.Empty(t, outputs, "expected no outputs to be created") } else { wantPath := path.Join("/cnab/app/porter/outputs", tc.wantOutput) - exists, _ := m.Config.FileSystem.Exists(wantPath) + exists, _ := m.FileSystem.Exists(wantPath) assert.True(t, exists, "output file was not created %s", wantPath) } }) diff --git a/pkg/aws/helpers.go b/pkg/aws/helpers.go index 7c8ec9a..b82c838 100644 --- a/pkg/aws/helpers.go +++ b/pkg/aws/helpers.go @@ -17,7 +17,7 @@ func NewTestMixin(t *testing.T) *TestMixin { c := portercontext.NewTestContext(t) m := &TestMixin{ Mixin: &Mixin{ - Config: runtime.NewConfigFor(c.Context), + RuntimeConfig: runtime.NewConfigFor(c.Context), }, TestContext: c, } diff --git a/pkg/aws/schema.go b/pkg/aws/schema.go index 9e3d5f3..598f24f 100644 --- a/pkg/aws/schema.go +++ b/pkg/aws/schema.go @@ -9,5 +9,5 @@ import ( var schema string func (m *Mixin) PrintSchema() { - fmt.Fprintf(m.Config.Out, schema) + fmt.Fprintf(m.Out, schema) } diff --git a/pkg/aws/version.go b/pkg/aws/version.go index 13da7b4..af64cdc 100644 --- a/pkg/aws/version.go +++ b/pkg/aws/version.go @@ -8,7 +8,7 @@ import ( ) func (m *Mixin) PrintVersion(opts version.Options) error { - return version.PrintVersion(m.Config.Context, opts, m.Version()) + return version.PrintVersion(m.Context, opts, m.Version()) } func (m *Mixin) Version() mixin.Metadata {