Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oci support #2566

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion commands/pkgcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/GoogleContainerTools/kpt/internal/cmddiff"
"github.com/GoogleContainerTools/kpt/internal/cmdget"
"github.com/GoogleContainerTools/kpt/internal/cmdinit"
"github.com/GoogleContainerTools/kpt/internal/cmdpull"
"github.com/GoogleContainerTools/kpt/internal/cmdpush"
"github.com/GoogleContainerTools/kpt/internal/cmdupdate"
"github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs"
"github.com/GoogleContainerTools/kpt/thirdparty/cmdconfig/commands/cmdtree"
Expand Down Expand Up @@ -47,7 +49,8 @@ func GetPkgCommand(ctx context.Context, name string) *cobra.Command {
pkg.AddCommand(
cmdget.NewCommand(ctx, name), cmdinit.NewCommand(ctx, name),
cmdupdate.NewCommand(ctx, name), cmddiff.NewCommand(ctx, name),
cmdtree.NewCommand(ctx, name),
cmdtree.NewCommand(ctx, name), cmdpull.NewCommand(ctx, name),
cmdpush.NewCommand(ctx, name),
)
return pkg
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ module github.com/GoogleContainerTools/kpt
go 1.16

require (
github.com/Masterminds/semver v1.5.0
github.com/cpuguy83/go-md2man/v2 v2.0.0
github.com/go-errors/errors v1.4.0
github.com/google/go-containerregistry v0.6.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/igorsobreira/titlecase v0.0.0-20140109233139-4156b5b858ac
github.com/otiai10/copy v1.6.0
github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f
github.com/posener/complete/v2 v2.0.1-alpha.12
github.com/spf13/cobra v1.1.3
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
Expand Down
567 changes: 554 additions & 13 deletions go.sum

Large diffs are not rendered by default.

32 changes: 26 additions & 6 deletions internal/cmdget/cmdget.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmdget

import (
"context"
"fmt"
"os"
"strings"

Expand All @@ -28,6 +29,7 @@ import (
"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
"github.com/GoogleContainerTools/kpt/internal/util/get"
"github.com/GoogleContainerTools/kpt/internal/util/parse"
"github.com/GoogleContainerTools/kpt/internal/util/remote"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/spf13/cobra"
)
Expand All @@ -38,7 +40,7 @@ func NewRunner(ctx context.Context, parent string) *Runner {
ctx: ctx,
}
c := &cobra.Command{
Use: "get REPO_URI[.git]/PKG_PATH[@VERSION] [LOCAL_DEST_DIRECTORY]",
Use: "get {REPO_URI[.git]/PKG_PATH[@VERSION]|IMAGE:TAG} [LOCAL_DEST_DIRECTORY]",
Args: cobra.MinimumNArgs(1),
Short: docs.GetShort,
Long: docs.GetShort + "\n" + docs.GetLong,
Expand Down Expand Up @@ -81,15 +83,14 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
args[1] = resolvedPath
}
}
t, err := parse.GitParseArgs(r.ctx, args)
destination, err := r.parseArgs(args)
if err != nil {
return errors.E(op, err)
return err
}

r.Get.Git = &t.Git
p, err := pkg.New(t.Destination)
p, err := pkg.New(destination)
if err != nil {
return errors.E(op, types.UniquePath(t.Destination), err)
return errors.E(op, types.UniquePath(destination), err)
}
r.Get.Destination = string(p.UniquePath)

Expand All @@ -101,6 +102,25 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
return nil
}

func (r *Runner) parseArgs(args []string) (string, error) {
const op errors.Op = "cmdget.preRunE"

t1, err1 := parse.GitParseArgs(r.ctx, args)
if err1 == nil {
r.Get.Git = &t1.Git
r.Get.Upstream = remote.NewGitUpstream(&t1.Git)
return t1.Destination, nil
}

t2, err2 := parse.OciParseArgs(r.ctx, args)
if err2 == nil {
r.Get.Upstream = remote.NewOciUpstream(&t2.Oci)
return t2.Destination, nil
}

return "", errors.E(op, fmt.Errorf("%v %v", err1, err2))
}

func (r *Runner) runE(c *cobra.Command, _ []string) error {
const op errors.Op = "cmdget.runE"
if err := r.Get.Run(r.ctx); err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/cmdget/cmdget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func TestCmd_Execute_flagAndArgParsing(t *testing.T) {
validations: func(repo, dir string, r *cmdget.Runner, err error) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "specify '.git'")
assert.Contains(t, err.Error(), "specify 'oci://'")
},
},
"valid strategy provided": {
Expand Down
117 changes: 117 additions & 0 deletions internal/cmdpull/cmdpull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmdpull

import (
"context"
"fmt"
"os"

docs "github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs"
"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/pkg"
"github.com/GoogleContainerTools/kpt/internal/types"
"github.com/GoogleContainerTools/kpt/internal/util/argutil"
"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
"github.com/GoogleContainerTools/kpt/internal/util/parse"
"github.com/GoogleContainerTools/kpt/internal/util/pull"
"github.com/GoogleContainerTools/kpt/internal/util/remote"
"github.com/spf13/cobra"
)

// NewRunner returns a command runner
func NewRunner(ctx context.Context, parent string) *Runner {
r := &Runner{
ctx: ctx,
}
c := &cobra.Command{
Use: "pull {REPO_URI[.git]/PKG_PATH[@VERSION]|IMAGE:TAG} [LOCAL_DEST_DIRECTORY]",
Args: cobra.MinimumNArgs(1),
Short: docs.GetShort,
Long: docs.GetShort + "\n" + docs.GetLong,
Example: docs.GetExamples,
RunE: r.runE,
PreRunE: r.preRunE,
}
cmdutil.FixDocs("kpt", parent, c)
r.Command = c
return r
}

func NewCommand(ctx context.Context, parent string) *cobra.Command {
return NewRunner(ctx, parent).Command
}

// Runner contains the run function
type Runner struct {
ctx context.Context
Pull pull.Command
Command *cobra.Command
}

func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
const op errors.Op = "cmdpull.preRunE"
if len(args) == 1 {
args = append(args, pkg.CurDir)
} else {
_, err := os.Lstat(args[1])
if err == nil || os.IsExist(err) {
resolvedPath, err := argutil.ResolveSymlink(r.ctx, args[1])
if err != nil {
return errors.E(op, err)
}
args[1] = resolvedPath
}
}
destination, err := r.parseArgs(args)
if err != nil {
return err
}

p, err := pkg.New(destination)
if err != nil {
return errors.E(op, types.UniquePath(destination), err)
}
r.Pull.Destination = string(p.UniquePath)

return nil
}

func (r *Runner) parseArgs(args []string) (string, error) {
const op errors.Op = "cmdpull.preRunE"

t1, err1 := parse.GitParseArgs(r.ctx, args)
if err1 == nil {
r.Pull.Origin = remote.NewGitOrigin(&t1.Git)
return t1.Destination, nil
}

t2, err2 := parse.OciParseArgs(r.ctx, args)
if err2 == nil {
r.Pull.Origin = remote.NewOciOrigin(&t2.Oci)
return t2.Destination, nil
}

return "", errors.E(op, fmt.Errorf("%v %v", err1, err2))
}

func (r *Runner) runE(c *cobra.Command, _ []string) error {
const op errors.Op = "cmdpull.runE"
if err := r.Pull.Run(r.ctx); err != nil {
return errors.E(op, types.UniquePath(r.Pull.Destination), err)
}

return nil
}
Loading