pin.go 3.39 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4 5 6 7 8 9 10
package options

type PinAddSettings struct {
	Recursive bool
}

type PinLsSettings struct {
	Type string
}

Overbool's avatar
Overbool committed
11 12 13 14 15
// PinRmSettings represents the settings of pin rm command
type PinRmSettings struct {
	Recursive bool
}

Łukasz Magiera's avatar
Łukasz Magiera committed
16 17 18 19
type PinUpdateSettings struct {
	Unpin bool
}

Łukasz Magiera's avatar
Łukasz Magiera committed
20
type PinAddOption func(*PinAddSettings) error
Overbool's avatar
Overbool committed
21 22 23 24 25 26

// PinRmOption pin rm option func
type PinRmOption func(*PinRmSettings) error

// PinLsOption pin ls option func
type PinLsOption func(*PinLsSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
27
type PinUpdateOption func(*PinUpdateSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
	options := &PinAddSettings{
		Recursive: true,
	}

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

	return options, nil
}

Overbool's avatar
Overbool committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// PinRmOptions pin rm options
func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) {
	options := &PinRmSettings{
		Recursive: true,
	}

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

	return options, nil
}

Łukasz Magiera's avatar
Łukasz Magiera committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
	options := &PinLsSettings{
		Type: "all",
	}

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

	return options, nil
}

Łukasz Magiera's avatar
Łukasz Magiera committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
	options := &PinUpdateSettings{
		Unpin: true,
	}

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

	return options, nil
}

89 90 91 92 93
type pinType struct{}

type pinOpts struct {
	Type pinType
}
Łukasz Magiera's avatar
Łukasz Magiera committed
94

95 96
var Pin pinOpts

97 98
// All is an option for Pin.Ls which will make it return all pins. It is
// the default
99
func (pinType) All() PinLsOption {
100 101 102 103 104
	return Pin.pinType("all")
}

// Recursive is an option for Pin.Ls which will make it only return recursive
// pins
105
func (pinType) Recursive() PinLsOption {
106 107 108 109 110
	return Pin.pinType("recursive")
}

// Direct is an option for Pin.Ls which will make it only return direct (non
// recursive) pins
111
func (pinType) Direct() PinLsOption {
112 113 114 115 116
	return Pin.pinType("direct")
}

// Indirect is an option for Pin.Ls which will make it only return indirect pins
// (objects referenced by other recursively pinned objects)
117
func (pinType) Indirect() PinLsOption {
118 119 120
	return Pin.pinType("indirect")
}

121 122
// Recursive is an option for Pin.Add which specifies whether to pin an entire
// object tree or just one object. Default: true
Łukasz Magiera's avatar
Łukasz Magiera committed
123
func (pinOpts) Recursive(recursive bool) PinAddOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
124
	return func(settings *PinAddSettings) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
125
		settings.Recursive = recursive
Łukasz Magiera's avatar
Łukasz Magiera committed
126 127 128 129
		return nil
	}
}

Overbool's avatar
Overbool committed
130 131 132 133 134 135 136 137
// RmRecursive is an option for Pin.Rm
func (pinOpts) RmRecursive(recursive bool) PinRmOption {
	return func(settings *PinRmSettings) error {
		settings.Recursive = recursive
		return nil
	}
}

138 139 140 141 142 143 144 145 146
// Type is an option for Pin.Ls which allows to specify which pin types should
// be returned
//
// Supported values:
// * "direct" - directly pinned objects
// * "recursive" - roots of recursive pins
// * "indirect" - indirectly pinned objects (referenced by recursively pinned
//    objects)
// * "all" - all pinned objects (default)
147
func (pinOpts) pinType(t string) PinLsOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
148 149 150 151 152
	return func(settings *PinLsSettings) error {
		settings.Type = t
		return nil
	}
}
Łukasz Magiera's avatar
Łukasz Magiera committed
153

154 155
// Unpin is an option for Pin.Update which specifies whether to remove the old pin.
// Default is true.
156
func (pinOpts) Unpin(unpin bool) PinUpdateOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
157 158 159 160 161
	return func(settings *PinUpdateSettings) error {
		settings.Unpin = unpin
		return nil
	}
}