Skip to content

Commit

Permalink
feat: Added read binary in starlark transformer (#987)
Browse files Browse the repository at this point in the history
Signed-off-by: Padmanabha V Seshadri <[email protected]>
  • Loading branch information
seshapad authored Mar 9, 2023
1 parent 950cf30 commit 0f64f59
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
36 changes: 31 additions & 5 deletions transformer/external/starlarktransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const (
qaFnName = "query"
// fs package
fsExistsFnName = "exists"
fsReadFnName = "read"
fsReadAsStringFnName = "read_as_string"
fsReadAsBinaryFnName = "read_as_binary"
fsReadDirFnName = "read_dir"
fsIsDirFnName = "is_dir"
fsGetDirFnName = "get_dir"
Expand Down Expand Up @@ -351,7 +352,8 @@ func (t *Starlark) addFSModules() {
Name: "fs",
Members: starlark.StringDict{
fsExistsFnName: t.getStarlarkFSExists(),
fsReadFnName: t.getStarlarkFSRead(),
fsReadAsStringFnName: t.getStarlarkFSReadAsString(),
fsReadAsBinaryFnName: t.getStarlarkFSReadAsBinary(),
fsReadDirFnName: t.getStarlarkFSReadDir(),
fsIsDirFnName: t.getStarlarkFSIsDir(),
fsGetFilesWithPatternFnName: t.getStarlarkFSGetFilesWithPattern(),
Expand Down Expand Up @@ -584,10 +586,34 @@ func (t *Starlark) getStarlarkFSIsDir() *starlark.Builtin {
})
}

func (t *Starlark) getStarlarkFSRead() *starlark.Builtin {
return starlark.NewBuiltin(fsReadFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
func (t *Starlark) getStarlarkFSReadAsBinary() *starlark.Builtin {
return starlark.NewBuiltin(fsReadAsBinaryFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackPositionalArgs(fsReadFnName, args, kwargs, 1, &path); err != nil {
if err := starlark.UnpackPositionalArgs(fsReadAsBinaryFnName, args, kwargs, 1, &path); err != nil {
return nil, err
}
if !t.Env.IsPathValid(path) {
return starlark.None, fmt.Errorf("the path '%s' is invalid", path)
}
fileBytes, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return starlark.None, nil
}
return nil, fmt.Errorf("failed to read the file at path '%s' . Error: %w", path, err)
}
var ifaceList []interface{}
for _, b := range fileBytes {
ifaceList = append(ifaceList, b)
}
return starutil.Marshal(ifaceList)
})
}

func (t *Starlark) getStarlarkFSReadAsString() *starlark.Builtin {
return starlark.NewBuiltin(fsReadAsStringFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackPositionalArgs(fsReadAsStringFnName, args, kwargs, 1, &path); err != nil {
return nil, err
}
if !t.Env.IsPathValid(path) {
Expand Down
7 changes: 1 addition & 6 deletions transformer/kubernetes/apiresource/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ func (s *Storage) convertToClusterSupportedKinds(obj runtime.Object, supportedKi
}

func (s *Storage) createConfigMap(st irtypes.Storage) *core.ConfigMap {
data := map[string]string{}
for k, v := range st.Content {
data[k] = string(v)
}

configMap := &core.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: string(irtypes.ConfigMapKind),
Expand All @@ -74,7 +69,7 @@ func (s *Storage) createConfigMap(st irtypes.Storage) *core.ConfigMap {
ObjectMeta: metav1.ObjectMeta{
Name: st.Name,
},
Data: data,
BinaryData: st.Content,
}
return configMap
}
Expand Down

0 comments on commit 0f64f59

Please sign in to comment.