funcs

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2020 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ColorFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "text",
			Type: cty.String,
		},
	},
	VarParam: &function.Parameter{
		Name: "attrs",
		Type: cty.String,
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		text := args[0].AsString()
		var attrs []color.Attribute
		for _, arg := range args[1:] {
			attr := strings.ToLower(arg.AsString())
			switch attr {

			case "black", "FgBlack", "fgblack":
				attrs = append(attrs, color.FgBlack)
			case "red", "FgRed", "fgred":
				attrs = append(attrs, color.FgRed)
			case "green", "FgGreen", "fggreen":
				attrs = append(attrs, color.FgGreen)
			case "yellow", "FgYellow", "fgyellow":
				attrs = append(attrs, color.FgYellow)
			case "blue", "FgBlue", "fgblue":
				attrs = append(attrs, color.FgBlue)
			case "magenta", "FgMagenta", "fgmagenta":
				attrs = append(attrs, color.FgMagenta)
			case "cyan", "FgCyan", "fgcyan":
				attrs = append(attrs, color.FgCyan)
			case "white", "FgWhite", "fgwhite":
				attrs = append(attrs, color.FgWhite)

			case "BgBlack", "bgblack":
				attrs = append(attrs, color.BgBlack)
			case "BgRed", "bgred":
				attrs = append(attrs, color.BgRed)
			case "BgGreen", "bggreen":
				attrs = append(attrs, color.BgGreen)
			case "BgYellow", "bgyellow":
				attrs = append(attrs, color.BgYellow)
			case "BgBlue", "bgblue":
				attrs = append(attrs, color.BgBlue)
			case "BgMagenta", "bgmagenta":
				attrs = append(attrs, color.BgMagenta)
			case "BgCyan", "bgcyan":
				attrs = append(attrs, color.BgCyan)
			case "BgWhite", "bgwhite":
				attrs = append(attrs, color.BgWhite)

			case "FgHiBlack", "fghiblack":
				attrs = append(attrs, color.FgHiBlack)
			case "FgHiRed", "fghired":
				attrs = append(attrs, color.FgHiRed)
			case "FgHiGreen", "fghigreen":
				attrs = append(attrs, color.FgHiGreen)
			case "FgHiYellow", "fghiyellow":
				attrs = append(attrs, color.FgHiYellow)
			case "FgHiBlue", "fghiblue":
				attrs = append(attrs, color.FgHiBlue)
			case "FgHiMagenta", "fghimagenta":
				attrs = append(attrs, color.FgHiMagenta)
			case "FgHiCyan", "fghicyan":
				attrs = append(attrs, color.FgHiCyan)
			case "FgHiWhite", "fghiwhite":
				attrs = append(attrs, color.FgHiWhite)

			case "Reset", "reset":
				attrs = append(attrs, color.Reset)
			case "Bold", "bold":
				attrs = append(attrs, color.Bold)
			case "Faint", "faint":
				attrs = append(attrs, color.Faint)
			case "Italic", "italic":
				attrs = append(attrs, color.Italic)
			case "Underline", "underline":
				attrs = append(attrs, color.Underline)
			case "BlinkSlow", "blinkslow":
				attrs = append(attrs, color.BlinkSlow)
			case "BlinkRapid", "blinkrapid":
				attrs = append(attrs, color.BlinkRapid)
			case "ReverseVideo", "reversevideo":
				attrs = append(attrs, color.ReverseVideo)
			case "Concealed", "concealed":
				attrs = append(attrs, color.Concealed)
			case "CrossedOut", "crossedout":
				attrs = append(attrs, color.CrossedOut)
			default:
				return cty.NilVal, fmt.Errorf("%q: invalid attr name", attr)
			}
		}
		c := color.New(attrs...)
		return cty.StringVal(c.Sprintf(text)), nil
	},
})

ColorFunc is

View Source
var EqualFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name:             "a",
			Type:             cty.DynamicPseudoType,
			AllowUnknown:     true,
			AllowDynamicType: true,
			AllowNull:        true,
		},
		{
			Name:             "b",
			Type:             cty.DynamicPseudoType,
			AllowUnknown:     true,
			AllowDynamicType: true,
			AllowNull:        true,
		},
	},
	Type: function.StaticReturnType(cty.Bool),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		a := args[0]
		b := args[1]
		if a.Type() != b.Type() {
			return cty.NilVal, fmt.Errorf("type not same: left is %q but right is %q", a.Type().FriendlyName(), b.Type().FriendlyName())
		}
		return a.Equals(b), nil
	},
})

EqualFunc is

View Source
var ExistFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "path",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.Bool),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		path := args[0].AsString()
		_, err = os.Stat(path)
		exist := !os.IsNotExist(err)
		return cty.BoolVal(exist), nil
	},
})

ExistFunc returns true if given path exists

View Source
var ExtFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "file",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		return cty.StringVal(filepath.Ext(args[0].AsString())), nil
	},
})

ExtFunc returns an extension of given file

View Source
var GlobFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "pattern",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.List(cty.String)),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		pattern := args[0].AsString()

		files, err := filepath.Glob(pattern)
		if err != nil {
			return cty.NilVal, err
		}

		vals := make([]cty.Value, len(files))
		for i, file := range files {
			vals[i] = cty.StringVal(file)
		}

		if len(vals) == 0 {
			return cty.ListValEmpty(cty.String), nil
		}
		return cty.ListVal(vals), nil
	},
})

GlobFunc returns a list of files matching a given pattern

View Source
var GrepFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "pattern",
			Type: cty.String,
		},
		{
			Name: "text",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		pattern := args[0].AsString()
		text := args[1].AsString()
		var matches []string

		r := bufio.NewReader(strings.NewReader(text))
		for {
			l, err := r.ReadString('\n')
			if err == io.EOF {
				break
			} else if err != nil {
				return cty.NilVal, err
			}
			l = strings.TrimRight(l, "\n")
			matched, err := regexp.MatchString(pattern, l)
			if err != nil {
				return cty.NilVal, err
			}
			if matched {
				matches = append(matches, l)
			}
		}
		return cty.StringVal(strings.Join(matches, "\n")), nil
	},
})

GrepFunc is

View Source
var HogeFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "str",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		return cty.StringVal(args[0].AsString() + " hoge"), nil
	},
})

HogeFunc is

View Source
var LookupListFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "inputMap",
			Type: cty.DynamicPseudoType,
		},
		{
			Name: "key",
			Type: cty.String,
		},
	},
	Type: func(args []cty.Value) (ret cty.Type, err error) {
		mapVar := args[0]
		lookupKey := args[1].AsString()
		if !mapVar.IsWhollyKnown() {
			return cty.NilType, nil
		}
		m := mapVar.AsValueMap()
		val, ok := m[lookupKey]
		if !ok {
			return cty.List(cty.String), nil
		}
		i := 0
		types := make([]cty.Type, val.LengthInt())
		for it := val.ElementIterator(); it.Next(); {
			_, av := it.Element()
			types[i] = av.Type()
			i++
		}
		retType, _ := convert.UnifyUnsafe(types)
		if retType == cty.NilType {
			return cty.NilType, fmt.Errorf("all arguments must have the same type")
		}

		return cty.List(retType), nil
	},
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		mapVar := args[0]
		lookupKey := args[1].AsString()
		if !mapVar.IsWhollyKnown() {
			return cty.UnknownVal(retType), nil
		}

		m := mapVar.AsValueMap()
		val, ok := m[lookupKey]
		if !ok {
			return cty.ListValEmpty(cty.String), nil
		}
		list := make([]cty.Value, 0, val.LengthInt())
		for it := val.ElementIterator(); it.Next(); {
			_, av := it.Element()
			av, _ = convert.Convert(av, retType.ElementType())
			list = append(list, av)
		}
		return cty.ListVal(list), nil
	},
})

LookupListFunc contructs a function that performs dynamic lookups of map types.

View Source
var MatchFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "pattern",
			Type: cty.String,
		},
		{
			Name: "text",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.Bool),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		pattern := args[0].AsString()
		text := args[1].AsString()

		matched, err := regexp.MatchString(pattern, text)
		if err != nil {
			return cty.NilVal, err
		}

		return cty.BoolVal(matched), nil
	},
})

MatchFunc is

View Source
var PathShortenFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "path",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		path := args[0].AsString()
		return cty.StringVal(pathshorten.Run(path)), nil
	},
})

PathShortenFunc returns the shorten path of given path

View Source
var TypeFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name:             "arg",
			Type:             cty.DynamicPseudoType,
			AllowUnknown:     true,
			AllowDynamicType: true,
			AllowNull:        true,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		return cty.StringVal(args[0].Type().FriendlyName()), nil
	},
})

TypeFunc is

View Source
var WcFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "text",
			Type: cty.String,
		},
	},
	VarParam: &function.Parameter{
		Name: "opts",
		Type: cty.String,
	},
	Type: function.StaticReturnType(cty.Number),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		text := args[0].AsString()
		opts := args[1:]
		var (
			lines = int64(strings.Count(text, "\n"))
			chars = int64(len(text))
			words = int64(len(strings.Fields(text)))
		)
		for _, opt := range opts {
			switch opt.AsString() {
			case "l":
				return cty.NumberIntVal(lines), nil
			case "c":
				return cty.NumberIntVal(chars), nil
			case "w":
				return cty.NumberIntVal(words), nil
			default:
				return cty.NilVal, fmt.Errorf("%v: not supported option", opt.AsString())
			}
		}

		return cty.NumberIntVal(lines), nil
	},
})

WcFunc is

Functions

func GJSON added in v0.2.8

func GJSON(file string, data []byte, query cty.Value) (cty.Value, error)

GJSON determines whether a file exists at the given path.

The underlying function implementation works relative to a input file and its contents, so this wrapper takes a input file string, etc and uses it to onstruct the underlying function before calling it.

func GJSONFunc

func GJSONFunc(file string, data []byte) function.Function

GJSONFunc is

func Grep added in v0.2.10

func Grep(pattern cty.Value, text cty.Value) (cty.Value, error)

Grep returns the matched line from given text by using regex

func MapHogeFunc

func MapHogeFunc(ctx *hcl.EvalContext) function.Function

MapHogeFunc is

func Wc added in v0.2.10

func Wc(text cty.Value, opts ...cty.Value) (cty.Value, error)

Wc counts the characters, words, or lines from given text

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL