index.go 1.85 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
package options

// Index option constants
const (
	RepoWindowStart = iota
)

// IndexMakeSettings mkidx option settings
type IndexMakeSettings struct {
	Events   chan<- interface{}
	Quiet    bool
	Progress bool
}

// IndexListSettings ls option settings
type IndexListSettings struct {
	Meta string
}

// IndexMakeOption make options type
type IndexMakeOption func(*IndexMakeSettings) error

// IndexListOption ls options type
type IndexListOption func(*IndexListSettings) error

// IndexMakeOptions make options function
func IndexMakeOptions(opts ...IndexMakeOption) (*IndexMakeSettings, error) {
	options := &IndexMakeSettings{
		Events:   nil,
		Quiet:    false,
		Progress: false,
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}

	return options, nil
}

type indexMakeOpts struct{}

// IndexMake options
var IndexMake indexMakeOpts

// Events specifies channel which will be used to report events about ongoing
// Make operation.
//
// Note that if this channel blocks it may slowdown the make
func (indexMakeOpts) Events(sink chan<- interface{}) IndexMakeOption {
	return func(settings *IndexMakeSettings) error {
		settings.Events = sink
		return nil
	}
}

// Quiet reduces event output
func (indexMakeOpts) Quiet(quiet bool) IndexMakeOption {
	return func(settings *IndexMakeSettings) error {
		settings.Quiet = quiet
		return nil
	}
}

// Progress tells make whether to enable progress events
func (indexMakeOpts) Progress(enable bool) IndexMakeOption {
	return func(settings *IndexMakeSettings) error {
		settings.Progress = enable
		return nil
	}
}

// IndexListOptions ls options function
func IndexListOptions(opts ...IndexListOption) (*IndexListSettings, error) {
	options := &IndexListSettings{
		Meta: "",
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}

	return options, nil
}