Dependencies

The power of Foxy Contexts comes from being based on DI concept facilitated by fx library. This allows to easily extract common parts of your server into separate packages and reuse them in different tools, prompts and other parts of your server.

You can learn more about fx in official documentation, here there are just some common patterns that you might find useful.

Extracting dependency

When you have identified that there is some bit that you need to repeat across tools, and maybe also for performance reasons you want to extract it and share, you can start with creating a function that creates that shared part.

Following is example for extracting k8s configuration:

// NewK8sClientConfig creates a new k8s client config
// , which we define separately in order to give it to
// fx to later inject it into potentially several tools as a dependency
func NewK8sClientConfig() clientcmd.ClientConfig {
	loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
	return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, nil)
}

With this in place, you can then define how the dependency would be injected into the tool you are creating:

func NewListK8sContextsTool(kubeConfig clientcmd.ClientConfig) fxctx.Tool { 

The final step would be to provide everything to your app.Builder:

	app.
		NewBuilder().
		WithFxOptions(
			fx.Provide(NewK8sClientConfig),
		).
		WithTool(NewListK8sContextsTool).