Skip to content

Writing a Plugin: Casketfile Loader

Jason Chu edited this page Oct 25, 2020 · 1 revision

This page is obsolete.

It is about Casket v1.


Join the Casket Community Forum to chat with other Casket developers!

You can customize how Casket loads the Casketfile. This is useful if you manage your server configuration with external tools or services and you want to get the Casketfile from them dynamically. If you play your cards right, you can even have Casket reload the Casketfile when it changes upstream.

All you have to do is implement the casket.Loader interface and register your plugin with a unique name.

Here's a skeleton:

import "github.com/mholt/casket"

func init() {
	casket.RegisterCasketfileLoader("myloader", casket.LoaderFunc(myLoader))
}

func myLoader(serverType string) (casket.Input, error) {
	return nil, nil
}

If your loader needs more state, you can implement the casket.Loader interface directly using a struct, instead of using the wrapper type casket.LoaderFunc like we did above. Simple loaders need only be standalone functions.

Your loader should return an error only if it is worth aborting and reporting to the user. In other words, do not return an error merely because there is no Casketfile for your loader to load.

How Loaders are Used

When Casket starts, it iterates the different loaders, asking them for a Casketfile. It always invokes all the loaders, but only one at most should return a value. This means that your loader must not return a casket.Input value unless conditions are just right. For example: if certain environment variables are set, or a command line flag was used to fill in a value, those are specific conditions to agree on how the Casketfile should be loaded.

Invoking Casket with multiple ways to load the Casketfile is confusing and will be treated as an error.

So if you want to write a Casketfile loader to hook into etcd, for instance, make sure it only returns a Casketfile if, say, environment variables to access etcd are set.

Setting the Default Casketfile Loader

You can set a default Casketfile loader for times when no other loader returns a Casketfile. To do this, simply call casket.SetDefaultCasketfileLoader() instead of casket.RegisterCasketfileLoader(). Note that this will overwrite any previous, default loader.

If the default loader does not return any Casketfile input, Casket will ask the server type for a default input. If the server type does not specify a default input, a blank Casketfile will be assumed.

Dynamic Reloads

A Casketfile loader may be written so that Casket reloads and applies the new Casketfile when it changes. The implementation details vary widely, but you can cause Casket to reload with the new Casketfile like so:

(TODO)