Skip to content

Commit

Permalink
feat: Add create_dir, copy_dir and remove modules to the builtin star…
Browse files Browse the repository at this point in the history
…lark transformer (#989)

Signed-off-by: Akash Nayak <[email protected]>
  • Loading branch information
Akash-Nayak authored Mar 9, 2023
1 parent 0f64f59 commit f625c4d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ require (
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2538,8 +2538,9 @@ golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
68 changes: 68 additions & 0 deletions transformer/external/starlarktransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/antchfx/xpath"
"github.com/konveyor/move2kube/common"
"github.com/konveyor/move2kube/environment"
"github.com/konveyor/move2kube/filesystem"
"github.com/konveyor/move2kube/qaengine"
"github.com/konveyor/move2kube/types"
qatypes "github.com/konveyor/move2kube/types/qaengine"
Expand Down Expand Up @@ -64,6 +65,9 @@ const (
fsReadDirFnName = "read_dir"
fsIsDirFnName = "is_dir"
fsGetDirFnName = "get_dir"
fsCreateDirFnName = "create_dir"
fsCopyDirFnName = "copy_dir"
fsRemoveAllFnName = "remove"
fsGetYamIsWithTypeMetaFnName = "get_yamls_with_type_meta"
fsFindXmlPathFnName = "find_xml_path"
fsGetFilesWithPatternFnName = "get_files_with_pattern"
Expand Down Expand Up @@ -364,6 +368,9 @@ func (t *Starlark) addFSModules() {
fsGetYamIsWithTypeMetaFnName: t.getStarlarkFSGetYamlsWithTypeMeta(),
fsPathBaseFnName: t.getStarlarkFSPathBase(),
fsGetDirFnName: t.getStarlarkFSGetDir(),
fsCreateDirFnName: t.getStarlarkFSCreateDir(),
fsCopyDirFnName: t.getStarlarkFSCopyDir(),
fsRemoveAllFnName: t.getStarlarkFSRemoveAll(),
fsPathRelFnName: t.getStarlarkFSPathRel(),
fsFindXmlPathFnName: t.getStarlarkFindXmlPath(),
},
Expand Down Expand Up @@ -717,6 +724,67 @@ func (t *Starlark) getStarlarkFSGetDir() *starlark.Builtin {
})
}

func (t *Starlark) getStarlarkFSCreateDir() *starlark.Builtin {
return starlark.NewBuiltin(fsCreateDirFnName, func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
var permissions = common.DefaultDirectoryPermission
if err := starlark.UnpackArgs(fsCreateDirFnName, args, kwargs, "path", &path, "perm?", &permissions); err != nil {
return starlark.None, fmt.Errorf("invalid args provided to '%s'. Error: %w", fsWriteFnName, err)
}
if path == "" {
return starlark.None, fmt.Errorf("the path is missing in the parameters")
}
if !t.Env.IsPathValid(path) {
return starlark.None, fmt.Errorf("the path '%s' is invalid", path)
}
if err := os.MkdirAll(path, permissions); err != nil {
return starlark.None, err
}
return starlark.None, nil
})
}

func (t *Starlark) getStarlarkFSRemoveAll() *starlark.Builtin {
return starlark.NewBuiltin(fsRemoveAllFnName, func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackArgs(fsRemoveAllFnName, args, kwargs, "path", &path); err != nil {
return starlark.None, fmt.Errorf("invalid args provided to '%s'. Error: %w", fsWriteFnName, err)
}
if path == "" {
return starlark.None, fmt.Errorf("the path is missing in the parameters")
}
if !t.Env.IsPathValid(path) {
return starlark.None, fmt.Errorf("the path '%s' is invalid", path)
}
if err := os.RemoveAll(path); err != nil {
return starlark.None, err
}
return starlark.None, nil
})
}

func (t *Starlark) getStarlarkFSCopyDir() *starlark.Builtin {
return starlark.NewBuiltin(fsCopyDirFnName, func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var srcPath, destPath string
if err := starlark.UnpackArgs(fsCopyDirFnName, args, kwargs, "srcPath", &srcPath, "destPath", &destPath); err != nil {
return starlark.None, fmt.Errorf("invalid args provided to '%s'. Error: %w", fsWriteFnName, err)
}
if srcPath == "" || destPath == "" {
return starlark.None, fmt.Errorf("the source or destination path is missing in the parameters")
}
if !t.Env.IsPathValid(srcPath) {
return starlark.None, fmt.Errorf("the source path '%s' is invalid", srcPath)
}
if !t.Env.IsPathValid(destPath) {
return starlark.None, fmt.Errorf("the destination path '%s' is invalid", destPath)
}
if err := filesystem.Replicate(srcPath, destPath); err != nil {
return starlark.None, err
}
return starlark.None, nil
})
}

func (t *Starlark) getStarlarkFSPathRel() *starlark.Builtin {
return starlark.NewBuiltin(fsPathRelFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var basePath, targetPath string
Expand Down

0 comments on commit f625c4d

Please sign in to comment.