MCPcopy Index your code
hub / github.com/docker/cli / buildPlugin

Function buildPlugin

e2e/testutils/plugins.go:64–99  ·  view source on GitHub ↗

buildPlugin uses Go to build a plugin from one of the source files in the plugins directory. It returns the path to the built plugin binary. To allow for multiple plugins to be built in parallel, the plugin is compiled with a unique identifier in the binary. This is done by setting a linker flag wit

(t *testing.T, ctx context.Context)

Source from the content-addressed store, hash-verified

62// To allow for multiple plugins to be built in parallel, the plugin is compiled with a unique
63// identifier in the binary. This is done by setting a linker flag with the -ldflags option.
64func buildPlugin(t *testing.T, ctx context.Context) (string, error) {
65 t.Helper()
66
67 randomName, err := randomString()
68 if err != nil {
69 return "", err
70 }
71
72 goBin, err := exec.LookPath("/usr/local/go/bin/go")
73 if err != nil {
74 return "", err
75 }
76 installPath := filepath.Join(os.Getenv("GOPATH"), "bin", randomName)
77
78 pluginContent, err := plugins.ReadFile("plugins/basic.go")
79 if err != nil {
80 return "", err
81 }
82 dir := fs.NewDir(t, "plugin_build")
83 if err := os.WriteFile(dir.Join("main.go"), pluginContent, 0o644); err != nil {
84 return "", err
85 }
86 defer dir.Remove()
87
88 cmd := exec.CommandContext(ctx, goBin, "build", "-ldflags",
89 fmt.Sprintf("-X 'main.UNIQUEME=%s'", randomName),
90 "-o", installPath, dir.Join("main.go"))
91
92 cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
93
94 if out, err := cmd.CombinedOutput(); err != nil {
95 return "", fmt.Errorf("error building basic plugin bin: %s: %w", string(out), err)
96 }
97
98 return installPath, nil
99}
100
101func randomString() (string, error) {
102 b := make([]byte, 8)

Callers 1

SetupPluginFunction · 0.85

Calls 2

randomStringFunction · 0.85
RemoveMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…