-
Notifications
You must be signed in to change notification settings - Fork 226
/
cmdget.go
111 lines (100 loc) · 3.22 KB
/
cmdget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2019 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 cmdget contains the get command
package cmdget
import (
"context"
"os"
"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/get"
"github.com/GoogleContainerTools/kpt/internal/util/parse"
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: "get REPO_URI[.git]/PKG_PATH[@VERSION] [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,
SuggestFor: []string{"clone", "cp", "fetch"},
}
cmdutil.FixDocs("kpt", parent, c)
r.Command = c
c.Flags().StringVar(&r.strategy, "strategy", string(kptfilev1.ResourceMerge),
"update strategy that should be used when updating this package -- must be one of: "+
strings.Join(kptfilev1.UpdateStrategiesAsStrings(), ","))
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
Get get.Command
Command *cobra.Command
strategy string
}
func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
const op errors.Op = "cmdget.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
}
}
t, err := parse.GitParseArgs(r.ctx, args)
if err != nil {
return errors.E(op, err)
}
r.Get.Git = &t.Git
p, err := pkg.New(t.Destination)
if err != nil {
return errors.E(op, types.UniquePath(t.Destination), err)
}
r.Get.Destination = string(p.UniquePath)
strategy, err := kptfilev1.ToUpdateStrategy(r.strategy)
if err != nil {
return err
}
r.Get.UpdateStrategy = strategy
return nil
}
func (r *Runner) runE(c *cobra.Command, _ []string) error {
const op errors.Op = "cmdget.runE"
if err := r.Get.Run(r.ctx); err != nil {
return errors.E(op, types.UniquePath(r.Get.Destination), err)
}
return nil
}