aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/commandfactory.go
blob: ac571a21ae1df22240d4b9a6db1b4f2216d8ae6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package caddycmd

import (
	"github.com/spf13/cobra"
)

type rootCommandFactory struct {
	constructor func() *cobra.Command
	options     []func(*cobra.Command)
}

func newRootCommandFactory(fn func() *cobra.Command) *rootCommandFactory {
	return &rootCommandFactory{
		constructor: fn,
	}
}

func (f *rootCommandFactory) Use(fn func(cmd *cobra.Command)) {
	f.options = append(f.options, fn)
}

func (f *rootCommandFactory) Build() *cobra.Command {
	o := f.constructor()
	for _, v := range f.options {
		v(o)
	}
	return o
}