-
Notifications
You must be signed in to change notification settings - Fork 5
/
push.go
48 lines (39 loc) · 1.24 KB
/
push.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
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, CIQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0
package static
import (
"fmt"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/cobra"
"go.ciq.dev/beskar/cmd/beskarctl/ctl"
"go.ciq.dev/beskar/pkg/oras"
"go.ciq.dev/beskar/pkg/orasfile"
)
var pushCmd = &cobra.Command{
Use: "push [file]",
Short: "Push a file.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
file := args[0]
if file == "" {
return ctl.Err("file must be specified")
}
if err := push(file, ctl.Repo(), ctl.Registry()); err != nil {
return ctl.Errf("while pushing static file: %s", err)
}
return nil
},
}
func PushCmd() *cobra.Command {
return pushCmd
}
func push(filepath, repo, registry string) error {
pusher, err := orasfile.NewStaticFilePusher(filepath, repo, name.WithDefaultRegistry(registry))
if err != nil {
return fmt.Errorf("while creating StaticFile pusher: %w", err)
}
fmt.Printf("Pushing %s to %s\n", filepath, pusher.Reference())
return oras.Push(pusher, remote.WithAuthFromKeychain(authn.DefaultKeychain))
}