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 feature branch #2666

Merged
merged 1 commit into from
Jan 26, 2022
Merged
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
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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
Expand Down
421 changes: 415 additions & 6 deletions go.sum

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions internal/cmdget/cmdget.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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 +39,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 @@ -84,15 +85,24 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
args[1] = resolvedPath
}
}
t, err := parse.GitParseArgs(r.ctx, args)
destination, err := parse.ParseArgs(r.ctx, args, parse.Options{
SetGit: func(git *kptfilev1.Git) error {
r.Get.Git = git
r.Get.Upstream = remote.NewGitUpstream(git)
return nil
},
SetOci: func(oci *kptfilev1.Oci) error {
r.Get.Upstream = remote.NewOciUpstream(oci)
return nil
},
})
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 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
108 changes: 108 additions & 0 deletions internal/cmdpull/cmdpull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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"
"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"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"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.PullShort,
Long: docs.PullShort + "\n" + docs.PullLong,
Example: docs.PullExamples,
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 := parse.ParseArgs(r.ctx, args, parse.Options{
SetGit: func(git *kptfilev1.Git) error {
r.Pull.Origin = remote.NewGitOrigin(git)
return nil
},
SetOci: func(oci *kptfilev1.Oci) error {
r.Pull.Origin = remote.NewOciOrigin(oci)
return nil
},
})
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) 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
}
161 changes: 161 additions & 0 deletions internal/cmdpush/cmdpush.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// 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 cmdpush

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

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/push"
"github.com/GoogleContainerTools/kpt/internal/util/remote"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"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: "push [DIR@VERSION] [flags]",
Args: cobra.MaximumNArgs(1),
Short: docs.PushShort,
Long: docs.PushShort + "\n" + docs.PushLong,
Example: docs.PushExamples,
RunE: r.runE,
PreRunE: r.preRunE,
}

c.Flags().StringVar(&r.Origin, "origin", "",
"assigns or changes the location where the package should be pushed. Default is to push it to "+
"the origin from which the package was pulled.")
c.Flags().BoolVar(&r.Increment, "increment", false,
"increment the version of the package when pushed. This will increment the DIR@VERSION if provided, "+
"otherwise it will increment the origin's version when pulled. The version must be semver or integer, and "+
"may have an optional leading 'v'")
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
Push push.Command
Command *cobra.Command
Origin string
Increment bool
}

func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
const op errors.Op = "cmdpush.preRunE"

var path string
var ref string

if len(args) >= 1 {
parts := strings.Split(args[0], "@")
if len(parts) > 2 {
return errors.E(op, errors.InvalidParam, fmt.Errorf("at most 1 version permitted"))
}

path = parts[0]
if len(parts) == 2 {
ref = parts[1]
}
}

if path == "" {
// default to current directory
path = "."
}

resolvedPath, err := argutil.ResolveSymlink(r.ctx, path)
if err != nil {
return err
}

if ref != "" {
r.Push.Ref = ref
}

r.Push.Increment = r.Increment

r.Push.Pkg, err = pkg.New(resolvedPath)
if err != nil {
return errors.E(op, err)
}
relPath, err := resolveRelPath(r.Push.Pkg.UniquePath)
if err != nil {
return errors.E(op, r.Push.Pkg.UniquePath, err)
}
if strings.HasPrefix(relPath, pkg.ParentDir) {
return errors.E(op, r.Push.Pkg.UniquePath, fmt.Errorf("package path must be under current working directory"))
}

if r.Origin != "" {
_, err := parse.ParseArgs(r.ctx, []string{r.Origin, path}, parse.Options{
SetOci: func(oci *kptfilev1.Oci) error {
r.Push.Origin = remote.NewOciOrigin(oci)
return nil
},
})
if err != nil {
return err
}
}

return nil
}

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

return nil
}

func resolveRelPath(path types.UniquePath) (string, error) {
const op errors.Op = "cmdpush.resolveRelPath"
cwd, err := os.Getwd()
if err != nil {
return "", errors.E(op, errors.IO,
fmt.Errorf("error looking up current working directory: %w", err))
}

relPath, err := filepath.Rel(cwd, path.String())
if err != nil {
return "", errors.E(op, errors.IO,
fmt.Errorf("error resolving the relative path: %w", err))
}
return relPath, nil
}
Loading