extra.go 1.61 KB
Newer Older
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
package commands

import cmds "github.com/ipfs/go-ipfs-cmds"

func CreateCmdExtras(opts ...func(e *cmds.Extra)) *cmds.Extra {
	e := new(cmds.Extra)
	for _, o := range opts {
		o(e)
	}
	return e
}

type doesNotUseRepo struct{}

func SetDoesNotUseRepo(val bool) func(e *cmds.Extra) {
	return func(e *cmds.Extra) {
		e.SetValue(doesNotUseRepo{}, val)
	}
}

func GetDoesNotUseRepo(e *cmds.Extra) (val bool, found bool) {
	return getBoolFlag(e, doesNotUseRepo{})
}

// doesNotUseConfigAsInput describes commands that do not use the config as
// input. These commands either initialize the config or perform operations
// that don't require access to the config.
//
// pre-command hooks that require configs must not be run before these
// commands.
type doesNotUseConfigAsInput struct{}

func SetDoesNotUseConfigAsInput(val bool) func(e *cmds.Extra) {
	return func(e *cmds.Extra) {
		e.SetValue(doesNotUseConfigAsInput{}, val)
	}
}

func GetDoesNotUseConfigAsInput(e *cmds.Extra) (val bool, found bool) {
	return getBoolFlag(e, doesNotUseConfigAsInput{})
}

// preemptsAutoUpdate describes commands that must be executed without the
// auto-update pre-command hook
type preemptsAutoUpdate struct{}

func SetPreemptsAutoUpdate(val bool) func(e *cmds.Extra) {
	return func(e *cmds.Extra) {
		e.SetValue(preemptsAutoUpdate{}, val)
	}
}

func GetPreemptsAutoUpdate(e *cmds.Extra) (val bool, found bool) {
	return getBoolFlag(e, preemptsAutoUpdate{})
}

func getBoolFlag(e *cmds.Extra, key interface{}) (val bool, found bool) {
	var ival interface{}
	ival, found = e.GetValue(key)
	if !found {
		return false, false
	}
	val = ival.(bool)
	return val, found
}