Skip to content

Commit

Permalink
feat: automatically download kusion modules in the mod add
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Jun 7, 2024
1 parent db5886a commit f18aae8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
24 changes: 17 additions & 7 deletions pkg/cmd/mod/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package mod
import (
"fmt"
"net/url"
"runtime"
"strings"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/kubectl/pkg/util/templates"
"kcl-lang.io/kpm/pkg/api"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/downloader"
pkg "kcl-lang.io/kpm/pkg/package"

"kusionstack.io/kusion/pkg/cmd/meta"
Expand Down Expand Up @@ -64,13 +66,20 @@ func (o *AddOptions) Run() error {
if stack == nil {
return fmt.Errorf("cannot find stack with empty name")
}
kclPkg, err := api.GetKclPackage(stack.Path)

cli, err := client.NewKpmClient()
if err != nil {
return err
}
cli.DepDownloader = downloader.NewOciDownloader(runtime.GOOS + "/" + runtime.GOARCH)

kclPkg, err := cli.LoadPkgFromPath(stack.Path)
if err != nil {
return err
}
dependencies := kclPkg.GetDependenciesInModFile()
dependencies := kclPkg.ModFile.Dependencies.Deps
if dependencies == nil {
dependencies = &pkg.Dependencies{Deps: make(map[string]pkg.Dependency)}
dependencies = make(map[string]pkg.Dependency)
}

// path example: oci://ghcr.io/kusionstack/service
Expand All @@ -83,7 +92,7 @@ func (o *AddOptions) Run() error {
return fmt.Errorf("invalid module path: %s", m.Path)
}

dependencies.Deps[o.ModuleName] = pkg.Dependency{
dependencies[o.ModuleName] = pkg.Dependency{
Name: o.ModuleName,
FullName: o.ModuleName + "_" + m.Version,
Version: m.Version,
Expand All @@ -96,11 +105,12 @@ func (o *AddOptions) Run() error {
},
}

// Save the dependencies to the kcl.mod file
err = kclPkg.StoreModFile()
// download dependencies
err = cli.UpdateDeps(kclPkg)
if err != nil {
return err
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/engine/api/generate/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func CopyDependentModules(workDir string) error {
dest = fmt.Sprintf("%s.exe", dest)
}
// copy the module binary to the $KUSION_HOME modules directory
// todo: replace with symlink
err = io.CopyFile(source, dest)
if err == nil {
// mark the dest file as executable
Expand Down

0 comments on commit f18aae8

Please sign in to comment.