Skip to content

Commit

Permalink
Merge pull request #58 from Peefy/refactor-source-field
Browse files Browse the repository at this point in the history
refactor: source field to impl better local package running
  • Loading branch information
Peefy committed May 7, 2024
2 parents 9c320aa + 49f6ecd commit f5c3fd3
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 207 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
vendor/
.kclvm
.DS_store
kcl.mod.lock
50 changes: 26 additions & 24 deletions pkg/edit/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"kcl-lang.io/cli/pkg/options"
src "kcl-lang.io/krm-kcl/pkg/source"
"kcl-lang.io/krm-kcl/pkg/source"

"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/kio"
Expand All @@ -36,7 +36,7 @@ const (
// A pointer to []*yaml.RNode objects that represent the output YAML objects of the KCL program.
func RunKCL(name, source string, resourceList *yaml.RNode) ([]*yaml.RNode, error) {
// 1. Construct KCL code from source.
file, err := SourceToTempFile(source)
entry, err := SourceToTempEntry(source)
if err != nil {
return nil, errors.Wrap(err)
}
Expand All @@ -47,7 +47,7 @@ func RunKCL(name, source string, resourceList *yaml.RNode) ([]*yaml.RNode, error
}
// 3. Run the KCL code.
result := bytes.NewBuffer([]byte{})
opts.Entries = []string{file}
opts.Entries = []string{entry}
opts.Writer = result
err = opts.Run()
if err != nil {
Expand Down Expand Up @@ -77,28 +77,30 @@ func ToKCLValueString(value *yaml.RNode, defaultValue string) (string, error) {
return string(jsonString), nil
}

// SourceToTempFile convert source to a temp KCL file.
func SourceToTempFile(source string) (string, error) {
// 1. Construct KCL code from source.
localeSource, err := src.LocaleSource(source)
if err != nil {
return "", errors.Wrap(err)
}
if src.IsOCI(source) {
return source, nil
}
// Create temp files.
tmpDir, err := os.MkdirTemp("", "sandbox")
if err != nil {
return "", fmt.Errorf("error creating temp directory: %v", err)
}
// Write kcl code in the temp file.
file := filepath.Join(tmpDir, "prog.k")
err = os.WriteFile(file, []byte(localeSource), 0666)
if err != nil {
return "", errors.Wrap(err)
// SourceToTempEntry convert source to a temp KCL file.
func SourceToTempEntry(src string) (string, error) {
if source.IsOCI(src) {
// Read code from a OCI source.
return src, nil
} else if source.IsLocal(src) {
return src, nil
} else if source.IsRemoteUrl(src) || source.IsGit(src) || source.IsVCSDomain(src) {
// Read code from local path or a remote url.
return source.ReadThroughGetter(src)
} else {
// May be a inline code source.
tmpDir, err := os.MkdirTemp("", "sandbox")
if err != nil {
return "", fmt.Errorf("error creating temp directory: %v", err)
}
// Write kcl code in the temp file.
file := filepath.Join(tmpDir, "prog.k")
err = os.WriteFile(file, []byte(src), 0666)
if err != nil {
return "", errors.Wrap(err)
}
return file, nil
}
return file, nil
}

func constructOptions(resourceList *yaml.RNode) (*options.RunOptions, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/options/testdata/set-annotation.k
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ schema KCLRunSpec:

resource: ResourceList = option("resource_list")
# Use `k = v` to override existing annotations
annotations = {"${k}" = v for k, v in option("params").annotations or {}}
annotations = {"${k}" = v for k, v in option("params")?.annotations or {}}
items = [item | {
metadata.annotations: annotations
} for item in option("items")]
} for item in option("items") or []]
22 changes: 0 additions & 22 deletions pkg/source/api.go

This file was deleted.

114 changes: 0 additions & 114 deletions pkg/source/entry.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/source/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func ReadThroughGetter(src string) (string, error) {
if err != nil {
return "", fmt.Errorf("error creating temp file: %v", err)
}
defer os.RemoveAll(tmpDir)

// Build the client
client := &getter.Client{
Expand Down Expand Up @@ -64,5 +63,5 @@ func ReadThroughGetter(src string) (string, error) {
}

// 5. Read source from the temp directory
return GetSourceFromDir(tmpDir)
return tmpDir, nil
}
2 changes: 1 addition & 1 deletion pkg/source/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

// IsGit determines whether or not a source is to be treated as a git source.
func IsGit(src string) bool {
return strings.HasPrefix(src, fmt.Sprintf("%s://", GitScheme))
return strings.HasPrefix(src, fmt.Sprintf("%s::", GitScheme))
}

// IsVCSDomain determines whether or not a source is to be treated as a VCS source.
Expand Down
25 changes: 0 additions & 25 deletions pkg/source/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package source
import (
"path/filepath"
"strings"

"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/runner"
)

// IsLocal determines whether or not a source is to be treated as a local path.
Expand All @@ -15,25 +12,3 @@ func IsLocal(src string) bool {
}
return false
}

// ReadFromLocalSource reads source code from a local file system path source.
//
// Parameters:
// - src: a local file system path.
//
// Return:
// A string containing the source code, and an error if any.
func ReadFromLocalSource(src string) (string, error) {
// Find the mod root

modpath, kpmerr := runner.FindModRootFrom(src)
if kpmerr != nil {
if kpmerr.Type() != reporter.KclModNotFound {
return "", kpmerr
} else {
modpath = filepath.Dir(src)
}
}

return GetSourceFromDir(modpath)
}
13 changes: 0 additions & 13 deletions pkg/source/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,9 @@ import (
const (
// OCIScheme is the URL scheme for OCI-based requests
OCIScheme = "oci"
// TarPattern is the wildcard pattern for tar files.
TarPattern = "*.tar"
)

// IsOCI determines whether or not a URL is to be treated as an OCI URL
func IsOCI(src string) bool {
return strings.HasPrefix(src, fmt.Sprintf("%s://", OCIScheme))
}

// ReadFromOCISource reads source code from an OCI (Open Container Initiative) source.
//
// Parameters:
// - src: a string containing the OCI source URL.
//
// Return:
// A string containing the source code, and an error if any.
func ReadFromOCISource(src string) (string, error) {
return src, nil
}
8 changes: 4 additions & 4 deletions tests/mutation/set-annotations/main.k
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
items = lambda {
resource = option("resource_list")
items = resource.items
params = option("params")
resource = option("resource_list") or {}
items = resource.items or []
params = option("params") or {}
# Use `k = v` to override existing annotations
annotations = {k = v for k, v in params.annotations}
annotations = {k = v for k, v in params.annotations or {}}
[item | {
metadata.annotations: annotations
} for item in items]
Expand Down

0 comments on commit f5c3fd3

Please sign in to comment.