djdv / go-filesystem-utils

ISC License
10 stars 2 forks source link

Argument/input handling #7

Closed djdv closed 2 years ago

djdv commented 2 years ago

Counterpart to https://github.com/djdv/go-filesystem-utils/pull/6 This is the arguments portion ("actual parameters") of the cmdslib wrapper.

For now, we accept input arguments from 2 providers. The cmdslib Request object (which itself could be generated from CLI's []argv or an HTTP request). And the process environment variables.

The values from these sources are parsed into native Go struct types. Currently like this:

sources := []argument.SetFunc{
    environment.ValueSource(),
    request.ValueSource(req)
}
settings, err := argument.Parse[*mySettings](ctx, sources)

Other sources could be implemented in similar ways later (if/as needed), such as interfaces for config files, etcd, registry, etc.

We'll likely end up implementing a config file later for fs mount to utilize like fstab (but not tabularized plaintext). E.g. config.ValueSource("/somewhere/to/fstab") or whatever.


There's an implementations of a Parameter with a constructor, but it might be taken out if not needed in this set. It can be added back in when actually utilized by a command. But it currently works like this:

type Settings struct {
    All bool
}
func (*Settings) Parameters(ctx context.Context) parameters.Parameters {
    partialParams := []somepkg.CmdsParameter{
        {
            OptionAliases: []string{"a"},
            HelpText:      "Unmount all mountpoints.",
        },
    }
    return somepkg.MustMakeParameters[*Settings](ctx, partialParams)
}

This structure is compatible with both MakeOptions[*unmount.Settings] to generate "formal parameters" (struct => options), and Parse[*unmount.Settings](ctx, sources) to parse any "actual parameters" (options => struct).

~~Draft: This was extracted from the mount branch, rebased on the staging/api branch, and some stuff was shuffled around. It builds and should work, but needs linting, more tests, consistency checks, etc. As well as better partitioned commits. Right now it's just one big blob, but should be split by pkg or whatever makes sense.~~

Good enough to move forward with for now.

djdv commented 2 years ago

See: https://github.com/djdv/go-filesystem-utils/pull/6#issuecomment-1177034987