From d29a14d802e547d20a791610e2453e78d344980e Mon Sep 17 00:00:00 2001 From: notoriaga Date: Fri, 7 Oct 2022 16:45:10 -0700 Subject: [PATCH] add gzip api --- pkg/yttlibrary/all.go | 3 ++ pkg/yttlibrary/gzip.go | 81 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 pkg/yttlibrary/gzip.go diff --git a/pkg/yttlibrary/all.go b/pkg/yttlibrary/all.go index 500d2a8f..179effc3 100644 --- a/pkg/yttlibrary/all.go +++ b/pkg/yttlibrary/all.go @@ -59,6 +59,9 @@ func NewAPI( "url": URLAPI, "ip": IPAPI, + // Compression + "gzip": GzipAPI, + // Templating "template": NewTemplateModule(replaceNodeFunc).AsModule(), "data": dataMod.AsModule(), diff --git a/pkg/yttlibrary/gzip.go b/pkg/yttlibrary/gzip.go new file mode 100644 index 00000000..bfff65fc --- /dev/null +++ b/pkg/yttlibrary/gzip.go @@ -0,0 +1,81 @@ +// Copyright 2020 VMware, Inc. +// SPDX-License-Identifier: Apache-2.0 + +package yttlibrary + +import ( + "bytes" + "compress/gzip" + "fmt" + + "github.com/k14s/starlark-go/starlark" + "github.com/k14s/starlark-go/starlarkstruct" + "github.com/vmware-tanzu/carvel-ytt/pkg/template/core" +) + +var ( + GzipAPI = starlark.StringDict{ + "gzip": &starlarkstruct.Module{ + Name: "gzip", + Members: starlark.StringDict{ + "compress": starlark.NewBuiltin("gzip.compress", core.ErrWrapper(gzipModule{}.Compress)), + "decompress": starlark.NewBuiltin("gzip.decompress", core.ErrWrapper(gzipModule{}.Decompress)), + }, + }, + } +) + +type gzipModule struct{} + +func (b gzipModule) Compress(thread *starlark.Thread, f *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + if args.Len() != 1 { + return starlark.None, fmt.Errorf("expected exactly one argument") + } + + val, err := core.NewStarlarkValue(args.Index(0)).AsString() + if err != nil { + return starlark.None, err + } + + var buf bytes.Buffer + gz := gzip.NewWriter(&buf) + + if _, err := gz.Write([]byte(val)); err != nil { + return nil, err + } + + if err := gz.Close(); err != nil { + return nil, err + } + + return starlark.String(buf.String()), nil +} + +func (b gzipModule) Decompress(thread *starlark.Thread, f *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + if args.Len() != 1 { + return starlark.None, fmt.Errorf("expected exactly one argument") + } + + val, err := core.NewStarlarkValue(args.Index(0)).AsString() + if err != nil { + return starlark.None, err + } + + buf := bytes.NewBufferString(val) + + gz, err := gzip.NewReader(buf) + if err != nil { + return nil, err + } + + var out bytes.Buffer + if _, err := out.ReadFrom(gz); err != nil { + return nil, err + } + + if err := gz.Close(); err != nil { + return nil, err + } + + return starlark.String(out.String()), nil +}