-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.go
72 lines (57 loc) · 1.83 KB
/
pull.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
package cli
import (
"context"
"fmt"
"strings"
"github.com/futurice/jalapeno/internal/cli/option"
"github.com/futurice/jalapeno/pkg/recipe"
"github.com/spf13/cobra"
)
type pullOptions struct {
TargetRef string
option.Common
option.OCIRepository
option.WorkingDirectory
}
func NewPullCmd() *cobra.Command {
var opts pullOptions
var cmd = &cobra.Command{
Use: "pull URL",
Short: "Pull a recipe from OCI repository",
Long: "Pull a recipe from OCI repository and save it locally. You can authenticate by using the `--username` and `--password` flags or logging in first with `docker login`.",
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.TargetRef = args[0]
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
err := runPull(cmd, opts)
return errorHandler(cmd, err)
},
Example: `# Pull recipe from OCI repository
jalapeno pull ghcr.io/user/recipe:latest
# Pull recipe from OCI repository with inline authentication
jalapeno pull oci://ghcr.io/user/my-recipe:latest --username user --password pass
# Pull recipe from OCI repository with Docker authentication
docker login ghcr.io
jalapeno pull oci://ghcr.io/user/my-recipe:latest
# Pull recipe to different directory
jalapeno pull oci://ghcr.io/user/my-recipe:latest --dir other/dir`,
}
if err := option.ApplyFlags(&opts, cmd.Flags()); err != nil {
return nil
}
return cmd
}
func runPull(cmd *cobra.Command, opts pullOptions) error {
ctx := context.Background()
err := recipe.SaveRemoteRecipe(ctx, opts.Dir, opts.Repository(opts.TargetRef))
if err != nil {
if strings.Contains(err.Error(), "not found") {
return fmt.Errorf("recipe not found from %s", opts.TargetRef)
}
return err
}
cmd.Printf("Recipe pulled %s\n", opts.Colors.Green.Render("successfully!"))
return nil
}