forked from kptdev/kpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oci-support feature branch (kptdev#2666)
Rebased on main and squashed Co-authored-by: Louis DeJardin <[email protected]> OCI: Fix Porch Build
- Loading branch information
1 parent
ec19882
commit d5ee178
Showing
36 changed files
with
3,101 additions
and
466 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.