file

package
v0.0.0-...-bb0c416 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Exists = rego.Function1(
	&rego.Function{
		Name: "file.exists",
		Decl: types.NewFunction(types.Args(types.S), types.B),
	},
	func(bctx rego.BuiltinContext, op1 *ast.Term) (*ast.Term, error) {
		var path string
		if err := ast.As(op1.Value, &path); err != nil {
			return nil, err
		}

		cpath := filepath.Clean(path)
		finfo, err := os.Stat(cpath)
		if err != nil {
			if errors.Is(err, os.ErrNotExist) {
				return ast.BooleanTerm(false), nil
			}
			return nil, err
		}

		if finfo.IsDir() {
			return ast.BooleanTerm(false), nil
		}

		return ast.BooleanTerm(true), nil
	},
)
View Source
var Read = rego.Function1(
	&rego.Function{
		Name: "file.read",
		Decl: types.NewFunction(types.Args(types.S), types.S),
	},
	func(bctx rego.BuiltinContext, op1 *ast.Term) (*ast.Term, error) {
		var path string
		if err := ast.As(op1.Value, &path); err != nil {
			return nil, err
		}

		cpath := filepath.Clean(path)
		f, err := os.Open(cpath)
		if err != nil {
			return nil, err
		}

		defer f.Close()

		all, rerr := io.ReadAll(f)
		if rerr != nil {
			return nil, rerr
		}

		allstr := ast.String(all)
		return ast.NewTerm(allstr), nil
	},
)

Functions

func Library

func Library() []func(*rego.Rego)

Types

This section is empty.

Jump to

Keyboard shortcuts

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