pin.go 7.68 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2
package options

Michael Muré's avatar
Michael Muré committed
3 4
import "fmt"

5
// PinAddSettings represent the settings for PinAPI.Add
Łukasz Magiera's avatar
Łukasz Magiera committed
6 7 8 9
type PinAddSettings struct {
	Recursive bool
}

10
// PinLsSettings represent the settings for PinAPI.Ls
Łukasz Magiera's avatar
Łukasz Magiera committed
11 12 13 14
type PinLsSettings struct {
	Type string
}

15
// PinIsPinnedSettings represent the settings for PinAPI.IsPinned
Michael Muré's avatar
Michael Muré committed
16 17 18 19
type PinIsPinnedSettings struct {
	WithType string
}

20
// PinRmSettings represents the settings for PinAPI.Rm
Overbool's avatar
Overbool committed
21 22 23 24
type PinRmSettings struct {
	Recursive bool
}

25
// PinUpdateSettings represent the settings for PinAPI.Update
Łukasz Magiera's avatar
Łukasz Magiera committed
26 27 28 29
type PinUpdateSettings struct {
	Unpin bool
}

30
// PinAddOption is the signature of an option for PinAPI.Add
Łukasz Magiera's avatar
Łukasz Magiera committed
31
type PinAddOption func(*PinAddSettings) error
Overbool's avatar
Overbool committed
32

33
// PinLsOption is the signature of an option for PinAPI.Ls
Michael Muré's avatar
Michael Muré committed
34 35
type PinLsOption func(*PinLsSettings) error

36
// PinIsPinnedOption is the signature of an option for PinAPI.IsPinned
Michael Muré's avatar
Michael Muré committed
37 38
type PinIsPinnedOption func(*PinIsPinnedSettings) error

39
// PinRmOption is the signature of an option for PinAPI.Rm
Overbool's avatar
Overbool committed
40 41
type PinRmOption func(*PinRmSettings) error

42
// PinUpdateOption is the signature of an option for PinAPI.Update
Łukasz Magiera's avatar
Łukasz Magiera committed
43
type PinUpdateOption func(*PinUpdateSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
44

45 46
// PinAddOptions compile a series of PinAddOption into a ready to use
// PinAddSettings and set the default values.
Łukasz Magiera's avatar
Łukasz Magiera committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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
}

62 63
// PinLsOptions compile a series of PinLsOption into a ready to use
// PinLsSettings and set the default values.
Michael Muré's avatar
Michael Muré committed
64 65 66
func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
	options := &PinLsSettings{
		Type: "all",
Overbool's avatar
Overbool committed
67 68 69
	}

	for _, opt := range opts {
Michael Muré's avatar
Michael Muré committed
70 71
		err := opt(options)
		if err != nil {
Overbool's avatar
Overbool committed
72 73 74 75 76 77 78
			return nil, err
		}
	}

	return options, nil
}

79 80
// PinIsPinnedOptions compile a series of PinIsPinnedOption into a ready to use
// PinIsPinnedSettings and set the default values.
Michael Muré's avatar
Michael Muré committed
81 82 83
func PinIsPinnedOptions(opts ...PinIsPinnedOption) (*PinIsPinnedSettings, error) {
	options := &PinIsPinnedSettings{
		WithType: "all",
Łukasz Magiera's avatar
Łukasz Magiera committed
84 85 86 87 88 89 90 91 92 93 94 95
	}

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

	return options, nil
}

96 97
// PinRmOptions compile a series of PinRmOption into a ready to use
// PinRmSettings and set the default values.
Michael Muré's avatar
Michael Muré committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
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
}

112 113
// PinUpdateOptions compile a series of PinUpdateOption into a ready to use
// PinUpdateSettings and set the default values.
Łukasz Magiera's avatar
Łukasz Magiera committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
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
}

129
type pinOpts struct {
Michael Muré's avatar
Michael Muré committed
130 131
	Ls       pinLsOpts
	IsPinned pinIsPinnedOpts
132
}
Łukasz Magiera's avatar
Łukasz Magiera committed
133

134
// Pin provide an access to all the options for the Pin API.
135 136
var Pin pinOpts

Michael Muré's avatar
Michael Muré committed
137 138
type pinLsOpts struct{}

139 140
// All is an option for Pin.Ls which will make it return all pins. It is
// the default
Michael Muré's avatar
Michael Muré committed
141 142
func (pinLsOpts) All() PinLsOption {
	return Pin.Ls.pinType("all")
143 144 145 146
}

// Recursive is an option for Pin.Ls which will make it only return recursive
// pins
Michael Muré's avatar
Michael Muré committed
147 148
func (pinLsOpts) Recursive() PinLsOption {
	return Pin.Ls.pinType("recursive")
149 150 151 152
}

// Direct is an option for Pin.Ls which will make it only return direct (non
// recursive) pins
Michael Muré's avatar
Michael Muré committed
153 154
func (pinLsOpts) Direct() PinLsOption {
	return Pin.Ls.pinType("direct")
155 156 157 158
}

// Indirect is an option for Pin.Ls which will make it only return indirect pins
// (objects referenced by other recursively pinned objects)
Michael Muré's avatar
Michael Muré committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
func (pinLsOpts) Indirect() PinLsOption {
	return Pin.Ls.pinType("indirect")
}

// Type is an option for Pin.Ls which will make it only return pins of the given
// type.
//
// 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)
func (pinLsOpts) Type(typeStr string) (PinLsOption, error) {
	switch typeStr {
	case "all", "direct", "indirect", "recursive":
		return Pin.Ls.pinType(typeStr), nil
	default:
		return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
	}
}

// pinType 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)
func (pinLsOpts) pinType(t string) PinLsOption {
	return func(settings *PinLsSettings) error {
		settings.Type = t
		return nil
	}
}

type pinIsPinnedOpts struct{}

// All is an option for Pin.IsPinned which will make it search in all type of pins.
// It is the default
func (pinIsPinnedOpts) All() PinIsPinnedOption {
	return Pin.IsPinned.pinType("all")
}

// Recursive is an option for Pin.IsPinned which will make it only search in
// recursive pins
func (pinIsPinnedOpts) Recursive() PinIsPinnedOption {
	return Pin.IsPinned.pinType("recursive")
}

// Direct is an option for Pin.IsPinned which will make it only search in direct
// (non recursive) pins
func (pinIsPinnedOpts) Direct() PinIsPinnedOption {
	return Pin.IsPinned.pinType("direct")
}

// Indirect is an option for Pin.IsPinned which will make it only search indirect
// pins (objects referenced by other recursively pinned objects)
func (pinIsPinnedOpts) Indirect() PinIsPinnedOption {
	return Pin.IsPinned.pinType("indirect")
}

// Type is an option for Pin.IsPinned which will make it only search pins of the given
// type.
//
// 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)
func (pinIsPinnedOpts) Type(typeStr string) (PinIsPinnedOption, error) {
	switch typeStr {
	case "all", "direct", "indirect", "recursive":
		return Pin.IsPinned.pinType(typeStr), nil
	default:
		return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", typeStr)
	}
}

// pinType is an option for Pin.IsPinned which allows to specify which pin type the given
// pin is expected to be, speeding up the research.
//
// 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)
func (pinIsPinnedOpts) pinType(t string) PinIsPinnedOption {
	return func(settings *PinIsPinnedSettings) error {
		settings.WithType = t
		return nil
	}
255 256
}

257 258
// 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
259
func (pinOpts) Recursive(recursive bool) PinAddOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
260
	return func(settings *PinAddSettings) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
261
		settings.Recursive = recursive
Łukasz Magiera's avatar
Łukasz Magiera committed
262 263 264 265
		return nil
	}
}

266 267 268
// RmRecursive is an option for Pin.Rm which specifies whether to recursively
// unpin the object linked to by the specified object(s). This does not remove
// indirect pins referenced by other recursive pins.
Overbool's avatar
Overbool committed
269 270 271 272 273 274 275
func (pinOpts) RmRecursive(recursive bool) PinRmOption {
	return func(settings *PinRmSettings) error {
		settings.Recursive = recursive
		return nil
	}
}

276 277
// Unpin is an option for Pin.Update which specifies whether to remove the old pin.
// Default is true.
278
func (pinOpts) Unpin(unpin bool) PinUpdateOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
279 280 281 282 283
	return func(settings *PinUpdateSettings) error {
		settings.Unpin = unpin
		return nil
	}
}