get.porter.sh/porter@v1.3.0/cmd/exec/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "os" 8 "runtime/debug" 9 10 "get.porter.sh/porter/pkg/cli" 11 "get.porter.sh/porter/pkg/exec" 12 "github.com/spf13/cobra" 13 "go.opentelemetry.io/otel/attribute" 14 ) 15 16 func main() { 17 run := func() int { 18 ctx := context.Background() 19 m := exec.New() 20 ctx, err := m.Config.ConfigureLogging(ctx) 21 if err != nil { 22 fmt.Println(err) 23 os.Exit(cli.ExitCodeErr) 24 } 25 cmd := buildRootCommand(m, os.Stdin) 26 27 // We don't have tracing working inside a bundle working currently. 28 // We are using StartRootSpan anyway because it creates a TraceLogger and sets it 29 // on the context, so we can grab it later 30 ctx, log := m.Config.StartRootSpan(ctx, "exec") 31 defer func() { 32 // Capture panics and trace them 33 if panicErr := recover(); panicErr != nil { 34 _ = log.Error(fmt.Errorf("%s", panicErr), 35 attribute.Bool("panic", true), 36 attribute.String("stackTrace", string(debug.Stack()))) 37 log.EndSpan() 38 m.Close() 39 os.Exit(cli.ExitCodeErr) 40 } else { 41 log.Close() 42 m.Close() 43 } 44 }() 45 46 if err := cmd.ExecuteContext(ctx); err != nil { 47 return cli.ExitCodeErr 48 } 49 return cli.ExitCodeSuccess 50 } 51 os.Exit(run()) 52 } 53 54 func buildRootCommand(m *exec.Mixin, in io.Reader) *cobra.Command { 55 cmd := &cobra.Command{ 56 Use: "exec", 57 Long: "exec is a porter 👩🏽✈️ mixin that you can you can use to execute arbitrary commands", 58 PersistentPreRun: func(cmd *cobra.Command, args []string) { 59 // Enable swapping out stdout/stderr/stdin for testing 60 m.Config.In = in 61 m.Config.Out = cmd.OutOrStdout() 62 m.Config.Err = cmd.OutOrStderr() 63 }, 64 SilenceUsage: true, 65 } 66 67 cmd.PersistentFlags().BoolVar(&m.Debug, "debug", false, "Enable debug mode") 68 69 cmd.AddCommand(buildVersionCommand(m)) 70 cmd.AddCommand(buildSchemaCommand(m)) 71 cmd.AddCommand(buildBuildCommand(m)) 72 cmd.AddCommand(buildLintCommand(m)) 73 cmd.AddCommand(buildInstallCommand(m)) 74 cmd.AddCommand(buildUpgradeCommand(m)) 75 cmd.AddCommand(buildInvokeCommand(m)) 76 cmd.AddCommand(buildUninstallCommand(m)) 77 78 return cmd 79 }