Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add pull command * Add push cmd
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func main() { | ||
cmd := &cobra.Command{ | ||
Use: "oras [OPTIONS] COMMAND", | ||
SilenceUsage: true, | ||
} | ||
cmd.AddCommand(pullCmd(), pushCmd()) | ||
if err := cmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"path" | ||
|
||
"github.com/shizhMSFT/oras/pkg/oras" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type pullOptions struct { | ||
targetRef string | ||
output string | ||
username string | ||
password string | ||
} | ||
|
||
func pullCmd() *cobra.Command { | ||
var opts pullOptions | ||
cmd := &cobra.Command{ | ||
Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]", | ||
Short: "Pull files from remote registry", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.targetRef = args[0] | ||
return runPull(opts) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.output, "output", "o", "", "output directory") | ||
cmd.Flags().StringVarP(&opts.username, "username", "u", "", "registry username") | ||
cmd.Flags().StringVarP(&opts.password, "password", "p", "", "registry password") | ||
return cmd | ||
} | ||
|
||
func runPull(opts pullOptions) error { | ||
resolver := newResolver(opts.username, opts.password) | ||
contents, err := oras.Pull(context.Background(), resolver, opts.targetRef) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for name, content := range contents { | ||
if opts.output != "" { | ||
name = path.Join(opts.output, name) | ||
} | ||
if err := ioutil.WriteFile(name, content, 0644); err != nil { | ||
return 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,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
|
||
"github.com/shizhMSFT/oras/pkg/oras" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type pushOptions struct { | ||
targetRef string | ||
filenames []string | ||
username string | ||
password string | ||
} | ||
|
||
func pushCmd() *cobra.Command { | ||
var opts pushOptions | ||
cmd := &cobra.Command{ | ||
Use: "push [OPTIONS] NAME[:TAG|@DIGEST] FILE [FILE...]", | ||
Short: "Push files to remote registry", | ||
Args: cobra.MinimumNArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.targetRef = args[0] | ||
opts.filenames = args[1:] | ||
return runPush(opts) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.username, "username", "u", "", "registry username") | ||
cmd.Flags().StringVarP(&opts.password, "password", "p", "", "registry password") | ||
return cmd | ||
} | ||
|
||
func runPush(opts pushOptions) error { | ||
resolver := newResolver(opts.username, opts.password) | ||
|
||
contents := make(map[string][]byte) | ||
for _, filename := range opts.filenames { | ||
content, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return err | ||
} | ||
contents[filename] = content | ||
} | ||
|
||
return oras.Push(context.Background(), resolver, opts.targetRef, contents) | ||
} |
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,35 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/containerd/containerd/remotes" | ||
"github.com/containerd/containerd/remotes/docker" | ||
"github.com/docker/cli/cli/config" | ||
"github.com/docker/docker/registry" | ||
) | ||
|
||
func newResolver(username, password string) remotes.Resolver { | ||
cfg := config.LoadDefaultConfigFile(os.Stderr) | ||
credential := func(hostName string) (string, string, error) { | ||
if hostName == registry.DefaultV2Registry.Host { | ||
hostName = registry.IndexServer | ||
} | ||
auth, err := cfg.GetAuthConfig(hostName) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if auth.IdentityToken != "" { | ||
return "", auth.IdentityToken, nil | ||
} | ||
return auth.Username, auth.Password, nil | ||
} | ||
if username != "" { | ||
credential = func(hostName string) (string, string, error) { | ||
return username, password, nil | ||
} | ||
} | ||
return docker.NewResolver(docker.ResolverOptions{ | ||
Credentials: credential, | ||
}) | ||
} |