cmd

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Go = &cli.Command{
		Name:            "go",
		Usage:           "Executes standard go commands with automatic instrumentation enabled",
		UsageText:       "orchestrion go [go command arguments...]",
		Args:            true,
		SkipFlagParsing: true,
		Action: func(ctx *cli.Context) error {
			pin.AutoPinOrchestrion(ctx.Context)

			if err := goproxy.Run(ctx.Context, ctx.Args().Slice(), goproxy.WithToolexec(binpath.Orchestrion, "toolexec")); err != nil {
				var exitErr *exec.ExitError
				if errors.As(err, &exitErr) {
					return cli.Exit("", exitErr.ExitCode())
				}
				return cli.Exit(err, -1)
			}
			return nil
		},
	}
)
View Source
var Pin = &cli.Command{
	Name:  "pin",
	Usage: "Registers orchestrion in your project's `go.mod` file",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:  "generate",
			Usage: "Add a //go:generate directive to " + config.FilenameOrchestrionToolGo + " to facilitate automated upkeep of its contents.",
			Value: true,
		},
		&cli.BoolFlag{
			Name:  "prune",
			Usage: "Remove imports from " + config.FilenameOrchestrionToolGo + " that do not contain a valid " + config.FilenameOrchestrionYML + " file declaring integrations.",
			Value: true,
		},
		&cli.BoolFlag{
			Name:  "validate",
			Usage: "Validate all " + config.FilenameOrchestrionYML + " files in the project.",
			Value: false,
		},
	},
	Action: func(ctx *cli.Context) error {
		return pin.PinOrchestrion(ctx.Context, pin.Options{
			Writer:     ctx.App.Writer,
			ErrWriter:  ctx.App.ErrWriter,
			Validate:   ctx.Bool("validate"),
			NoGenerate: !ctx.Bool("generate"),
			NoPrune:    !ctx.Bool("prune"),
		})
	},
}
View Source
var Server = &cli.Command{
	Name:        "server",
	Usage:       "Start an Objectsrion job server.",
	Description: "The job server is used to remove duplicated processing that can occur when instrumenting large applications, due to how Orchestrion injects new dependencies that the go toolchain was initially not aware of.\n\nUsers do not normally need to use this command directly, as Orchestrion automatically manages servers during runtime.",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "url-file",
			Usage: "Write a file containing the ClientURL for this server once it is ready to accept connections. The server automatically shuts down when the URL file is deleted.",
		},
		&cli.IntFlag{
			Name:        "port",
			Usage:       "Choose a port to listen on.",
			Value:       -1,
			DefaultText: "random",
		},
		&cli.DurationFlag{
			Name:  "inactivity-timeout",
			Usage: "Automatically shut down after a period without any connected client.",
			Value: time.Minute,
		},
		&cli.BoolFlag{
			Name:  "nats-logging",
			Usage: "Enable NATS server logging.",
		},
		&cli.StringFlag{
			Name:        "build-flags",
			Usage:       "Specify the 'go build' flags to use when resolving packages. This is specified as a YAML array and must start with a valid go subcommand (e.g, 'build').",
			DefaultText: "Looked up the process hierarchy",
			Action: func(ctx *cli.Context, val string) error {
				var args []string
				if err := yaml.Unmarshal([]byte(val), &args); err != nil {
					return cli.Exit(fmt.Errorf("invalid -build-flags value: %w", err), 2)
				}
				goflags.SetFlags(ctx.Context, ".", args)
				return nil
			},
		},
		&cli.IntFlag{
			Name:        "parent-pid",
			Usage:       "Specify which process created this server. This is useful when the server is started as a daemon, as it needs to be able to resolve the top-level go command line.",
			DefaultText: "This process' parent (may be inaccurate if the process is daemonized)",
			Action: func(ctx *cli.Context, val int) error {
				return goflags.SetFlagsFromPid(ctx.Context, val)
			},
		},
	},
	Hidden: true,
	Action: func(ctx *cli.Context) error {
		opts := jobserver.Options{
			ServerName:        "github.com/DataDog/orchestrion server",
			Port:              ctx.Int("port"),
			InactivityTimeout: ctx.Duration("inactivity-timeout"),
			EnableLogging:     ctx.Bool("nats-logging"),
		}

		if urlFile := ctx.String("url-file"); urlFile != "" {
			return startWithURLFile(ctx.Context, &opts, urlFile)
		}
		_, err := start(ctx.Context, &opts, true)
		return err
	},
}
View Source
var Toolexec = &cli.Command{
	Name:            "toolexec",
	Usage:           "Standard `-toolexec` plugin for the Go toolchain",
	UsageText:       "orchestrion toolexec [tool] [tool args...]",
	Args:            true,
	SkipFlagParsing: true,
	Action: func(ctx *cli.Context) (resErr error) {
		log := zerolog.Ctx(ctx.Context)
		importPath := os.Getenv("TOOLEXEC_IMPORTPATH")

		proxyCmd, err := proxy.ParseCommand(ctx.Context, importPath, ctx.Args().Slice())
		if err != nil || proxyCmd == nil {

			return err
		}
		defer func() { proxyCmd.Close(ctx.Context, resErr) }()

		if proxyCmd.Type() == proxy.CommandTypeOther {

			err := proxy.RunCommand(proxyCmd)
			var event *zerolog.Event
			if err != nil {
				event = log.Error().Err(err)
			} else {
				event = log.Trace()
			}
			event.Strs("command", proxyCmd.Args()).Msg("Toolexec fast-forward command")
			return err
		}

		pin.AutoPinOrchestrion(ctx.Context)

		if proxyCmd.ShowVersion() {
			log.Trace().Strs("command", proxyCmd.Args()).Msg("Toolexec version command")
			fullVersion, err := toolexec.ComputeVersion(ctx.Context, proxyCmd)
			if err != nil {
				return err
			}
			log.Trace().Str("version", fullVersion).Msg("Complete version output")
			_, err = fmt.Println(fullVersion)
			return err
		}

		log.Info().Strs("command", proxyCmd.Args()).Msg("Toolexec original command")
		weaver := aspect.Weaver{ImportPath: importPath}

		if err := proxy.ProcessCommand(ctx.Context, proxyCmd, weaver.OnCompile); errors.Is(err, proxy.ErrSkipCommand) {
			log.Trace().Msg("OnCompile processor requested command skipping...")
			return nil
		} else if err != nil {
			return err
		}
		if err := proxy.ProcessCommand(ctx.Context, proxyCmd, weaver.OnCompileMain); errors.Is(err, proxy.ErrSkipCommand) {
			log.Trace().Msg("OnCompileMain processor requested command skipping...")
			return nil
		} else if err != nil {
			return err
		}
		if err := proxy.ProcessCommand(ctx.Context, proxyCmd, weaver.OnLink); errors.Is(err, proxy.ErrSkipCommand) {
			log.Trace().Msg("OnLink processor requested command skipping...")
			return nil
		} else if err != nil {
			return err
		}

		log.Debug().Strs("command", proxyCmd.Args()).Msg("Toolexec final command")
		if err := proxy.RunCommand(proxyCmd); err != nil {

			log.Error().Strs("command", proxyCmd.Args()).Err(err).Msg("Proxied command failed")
			return err
		}
		return nil
	},
}
View Source
var Version = &cli.Command{
	Name:  "version",
	Usage: "Displays this command's version information",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "verbose",
			Aliases: []string{"v"},
			Usage:   "display the version of the orchestrion binary that started this command (if different from the current)",
			Hidden:  true,
		},
	},
	Action: func(c *cli.Context) error {
		if _, err := fmt.Fprintf(c.App.Writer, "orchestrion %s", version.Tag); err != nil {
			return err
		}

		if c.Bool("verbose") {
			if startupVersion := ensure.StartupVersion(); startupVersion != version.Tag {
				if _, err := fmt.Fprintf(c.App.Writer, " (started as %s)", startupVersion); err != nil {
					return err
				}
			}
			if _, err := fmt.Fprintf(c.App.Writer, " built with %s (%s/%s)", runtime.Version(), runtime.GOOS, runtime.GOARCH); err != nil {
				return err
			}
		}

		_, err := fmt.Fprintln(c.App.Writer)
		return err
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL