From 3cd2b4681b842f9ae018f8deac1a90bf3e7dfb90 Mon Sep 17 00:00:00 2001 From: Dayuan Date: Sun, 28 Apr 2024 16:43:28 +0800 Subject: [PATCH] feat: parse module name and version from the kcl.mod in `mod push` --- go.mod | 12 +++++------ go.sum | 15 ++++++++------ pkg/cmd/mod/mod_push.go | 46 ++++++++++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 6c5e4968..07249646 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,7 @@ require ( github.com/go-chi/render v1.0.3 github.com/go-git/go-git/v5 v5.11.0 github.com/go-sql-driver/mysql v1.7.0 - github.com/go-test/deep v1.0.3 + github.com/go-test/deep v1.0.8 github.com/goccy/go-yaml v1.11.3 github.com/gonvenience/bunt v1.1.1 github.com/gonvenience/neat v1.3.0 @@ -79,7 +79,7 @@ require ( k8s.io/utils v0.0.0-20230726121419-3b25d923346b kcl-lang.io/kcl-go v0.8.2 kcl-lang.io/kcl-plugin v0.5.0 - kcl-lang.io/kpm v0.8.3-0.20240321122901-fd054837ea5c + kcl-lang.io/kpm v0.8.6-0.20240428063742-f3e75d94c905 kusionstack.io/kube-api v0.1.1 sigs.k8s.io/controller-runtime v0.15.1 ) @@ -121,7 +121,7 @@ require ( github.com/googleapis/gax-go/v2 v2.12.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect - github.com/hashicorp/go-getter v1.7.3 // indirect + github.com/hashicorp/go-getter v1.7.4 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect @@ -269,7 +269,7 @@ require ( github.com/morikuni/aec v1.0.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect - github.com/opencontainers/image-spec v1.1.0-rc5 // indirect + github.com/opencontainers/image-spec v1.1.0 // indirect github.com/otiai10/copy v1.12.0 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -279,7 +279,7 @@ require ( github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.11.1 // indirect github.com/rivo/uniseg v0.4.4 // indirect - github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/ryanuber/go-glob v1.0.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect @@ -323,5 +323,3 @@ require ( sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) - -replace kcl-lang.io/kpm => github.com/KusionStack/kpm v0.8.4 diff --git a/go.sum b/go.sum index 883f1830..f22ffe4c 100644 --- a/go.sum +++ b/go.sum @@ -533,8 +533,8 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9 github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= -github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= -github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= +github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/goccy/go-yaml v1.11.3 h1:B3W9IdWbvrUu2OYQGwvU1nZtvMQJPBKgBUuweJjLj6I= github.com/goccy/go-yaml v1.11.3/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= @@ -709,8 +709,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/hashicorp/go-getter v1.7.3 h1:bN2+Fw9XPFvOCjB0UOevFIMICZ7G2XSQHzfvLUyOM5E= -github.com/hashicorp/go-getter v1.7.3/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= +github.com/hashicorp/go-getter v1.7.4 h1:3yQjWuxICvSpYwqSayAdKRFcvBl1y/vogCxczWSmix0= +github.com/hashicorp/go-getter v1.7.4/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744= github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= github.com/hashicorp/go-hclog v0.16.2 h1:K4ev2ib4LdQETX5cSZBG0DVLk1jwGqSPXBjdah3veNs= github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= @@ -911,6 +911,7 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/cbdlwvlWt0pnFI= github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8= +github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= github.com/orcaman/concurrent-map v0.0.0-20210501183033-44dafcb38ecc h1:Ak86L+yDSOzKFa7WM5bf5itSOo1e3Xh8bm5YCMUXIjQ= github.com/orcaman/concurrent-map v0.0.0-20210501183033-44dafcb38ecc/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI= github.com/otiai10/copy v1.12.0 h1:cLMgSQnXBs1eehF0Wy/FAGsgDTDmAqFR7rQylBb1nDY= @@ -968,8 +969,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= @@ -1754,6 +1755,8 @@ kcl-lang.io/kcl-go v0.8.2 h1:XbF7NJ2dHXREmZx/dJSvEdMdaOM9NA8ePZj3LdcEZ34= kcl-lang.io/kcl-go v0.8.2/go.mod h1:KLPIalP4jr19KUqnXWbe2sVNEMEH7EhnsXH+ihnTUQw= kcl-lang.io/kcl-plugin v0.5.0 h1:eoh6y4l81rwA8yhJXU4hN7YmJeTUNB1nfYCP9OffSxc= kcl-lang.io/kcl-plugin v0.5.0/go.mod h1:QnZ5OLcyBw5nOnHpChRHtvBq8wvjwiHu/ZZ8j1dfz48= +kcl-lang.io/kpm v0.8.6-0.20240428063742-f3e75d94c905 h1:c3QhaGQG/c6jbX7n1ZRQBe+UbeobTqvsl3ffJ0A0+RM= +kcl-lang.io/kpm v0.8.6-0.20240428063742-f3e75d94c905/go.mod h1:ws42LNStszL2UJ/fA6Zmof+XTAPbrJRTOlThDqB5jsA= kcl-lang.io/lib v0.8.2 h1:G8Fm+eHSMpVOSNJhd5T41XgoKZjysHRU290q6Xotepk= kcl-lang.io/lib v0.8.2/go.mod h1:ubsalGXxJaa5II/EsHmsI/tL2EluYHIcW+BwzQPt+uY= kusionstack.io/kube-api v0.1.1 h1:ieoZhaUfK78hsyQ7GsU6ZuxBAcVU+ZuKs7vedGkO8sI= diff --git a/pkg/cmd/mod/mod_push.go b/pkg/cmd/mod/mod_push.go index c15951ff..a8e31f3e 100644 --- a/pkg/cmd/mod/mod_push.go +++ b/pkg/cmd/mod/mod_push.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "fmt" + "net/url" "os" "os/exec" "path/filepath" @@ -15,6 +16,7 @@ import ( v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericiooptions" + "kcl-lang.io/kpm/pkg/api" cmdutil "kusionstack.io/kusion/pkg/cmd/util" "kusionstack.io/kusion/pkg/oci" @@ -34,24 +36,24 @@ var ( pushExample = i18n.T(` # Push a module of current OS arch to an OCI Registry using a token - kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 --creds + kusion mod push /path/to/my-module oci://ghcr.io/org --creds # Push a module of specific OS arch to an OCI Registry using a token - kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --os-arch==darwin/arm64 --version=1.0.0 --creds + kusion mod push /path/to/my-module oci://ghcr.io/org --os-arch==darwin/arm64 --creds # Push a module to an OCI Registry using a credentials in : format. - kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 --creds : + kusion mod push /path/to/my-module oci://ghcr.io/org --creds : # Push a release candidate without marking it as the latest stable - kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0-rc.1 --latest=false + kusion mod push /path/to/my-module oci://ghcr.io/org --latest=false # Push a module with custom OCI annotations - kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 \ + kusion mod push /path/to/my-module oci://ghcr.io/org \ --annotation='org.opencontainers.image.documentation=https://app.org/docs' # Push and sign a module with Cosign (the cosign binary must be present in PATH) export COSIGN_PASSWORD=password - kusion mod push /path/to/my-module oci://ghcr.io/org/my-module --version=1.0.0 \ + kusion mod push /path/to/my-module oci://ghcr.io/org \ --sign=cosign --cosign-key=/path/to/cosign.key`) ) @@ -64,7 +66,6 @@ const LatestVersion = "latest" // // This structure reduces the transformation to wiring and makes the logic itself easy to unit test. type PushModFlags struct { - Version string Latest bool OSArch string Annotations []string @@ -83,6 +84,7 @@ type PushModOptions struct { OCIUrl string Latest bool OSArch string + Name string Version string Sign string CosignKey string @@ -129,7 +131,6 @@ func NewCmdPush(ioStreams genericiooptions.IOStreams) *cobra.Command { // AddFlags registers flags for a cli. func (flags *PushModFlags) AddFlags(cmd *cobra.Command) { - cmd.Flags().StringVarP(&flags.Version, "version", "v", "", "The version of the module e.g. '1.0.0' or '1.0.0-rc.1'.") cmd.Flags().StringVar(&flags.OSArch, "os-arch", "", "The os arch of the module e.g. 'darwin/arm64', 'linux/amd64'.") cmd.Flags().BoolVar(&flags.Latest, "latest", true, "Tags the current version as the latest stable module version.") cmd.Flags().StringVar(&flags.Credentials, "creds", flags.Credentials, @@ -148,11 +149,22 @@ func (flags *PushModFlags) ToOptions(args []string, ioStreams genericiooptions.I } // version - version := flags.Version - if _, err := semver.StrictNewVersion(version); err != nil { + kclPkg, err := api.GetKclPackage(args[0]) + if err != nil { + return nil, err + } + version := kclPkg.GetVersion() + if _, err = semver.StrictNewVersion(version); err != nil { return nil, fmt.Errorf("version is not in semver format: %w", err) } + name := kclPkg.GetPkgName() + if name == "" { + return nil, fmt.Errorf("module name is empty") + } + + ociURL := strings.TrimRight(args[1], "/") + "/" + name + // If creds in format, creds must be base64 encoded if len(flags.Credentials) != 0 && !strings.Contains(flags.Credentials, ":") { flags.Credentials = base64.StdEncoding.EncodeToString([]byte(flags.Credentials)) @@ -205,12 +217,13 @@ func (flags *PushModFlags) ToOptions(args []string, ioStreams genericiooptions.I opt := &PushModOptions{ ModulePath: args[0], - OCIUrl: args[1], + OCIUrl: ociURL, Latest: flags.Latest, Sign: flags.Sign, CosignKey: flags.CosignKey, Client: client, Metadata: meta, + Name: name, Version: version, IOStreams: ioStreams, } @@ -224,7 +237,10 @@ func (o *PushModOptions) Validate() error { return fmt.Errorf("no module found at path %s", o.ModulePath) } - // TODO: add oci url validation + _, err := url.Parse(o.OCIUrl) + if err != nil { + return fmt.Errorf("illegal oci url:%s, %w", o.OCIUrl, err) + } return nil } @@ -322,10 +338,6 @@ func (o *PushModOptions) buildModule() (string, error) { pOS := o.Metadata.Platform.OS pArch := o.Metadata.Platform.Architecture - // OCIUrl example: oci://ghcr.io/org/my-module - split := strings.Split(o.OCIUrl, "/") - name := split[len(split)-1] - if matches, err := filepath.Glob(goFileSearchPattern); err != nil || len(matches) == 0 { return "", fmt.Errorf("no go source code files found for 'go build' matching %s", goFileSearchPattern) } @@ -335,7 +347,7 @@ func (o *PushModOptions) buildModule() (string, error) { return "", fmt.Errorf("unable to find executable 'go' binary: %w", err) } - output := filepath.Join(targetDir, "_dist", pOS, pArch, "kusion-module-"+name+"_"+o.Version) + output := filepath.Join(targetDir, "_dist", pOS, pArch, "kusion-module-"+o.Name+"_"+o.Version) if strings.Contains(o.OSArch, "windows") { output += ".exe" }