From 079872030eff075035cb7ab5c6b76897df84e3ab Mon Sep 17 00:00:00 2001 From: simon-cj Date: Tue, 8 Dec 2020 11:55:37 +0800 Subject: [PATCH] feat(cmd): support plainHTTP opt for Pull&Push func --- cmd/ormb-storage-initializer/cmd/pull-and-export.go | 2 +- cmd/ormb/cmd/pull.go | 3 ++- cmd/ormb/cmd/push.go | 2 +- pkg/oras/client.go | 7 +++++++ pkg/oras/interface.go | 2 ++ pkg/ormb/ormb.go | 12 ++++++++---- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/ormb-storage-initializer/cmd/pull-and-export.go b/cmd/ormb-storage-initializer/cmd/pull-and-export.go index 954df935..0cc1aa2c 100644 --- a/cmd/ormb-storage-initializer/cmd/pull-and-export.go +++ b/cmd/ormb-storage-initializer/cmd/pull-and-export.go @@ -76,7 +76,7 @@ var pullExportCmd = &cobra.Command{ } // Pull the model from the remote registry. - if err := ormbClient.Pull(modelURI); err != nil { + if err := ormbClient.Pull(modelURI, false); err != nil { return err } // Export it to the specified directory. diff --git a/cmd/ormb/cmd/pull.go b/cmd/ormb/cmd/pull.go index 17c75d88..06eb401e 100644 --- a/cmd/ormb/cmd/pull.go +++ b/cmd/ormb/cmd/pull.go @@ -29,7 +29,8 @@ This will store the model in the local registry cache to be used later.`, PreRunE: preRunE, RunE: func(cmd *cobra.Command, args []string) error { // TODO(gaocegege): Validate. - return ormbClient.Pull(args[0]) + + return ormbClient.Pull(args[0], plainHTTPOpt) }, } diff --git a/cmd/ormb/cmd/push.go b/cmd/ormb/cmd/push.go index 83c90483..b61847d0 100644 --- a/cmd/ormb/cmd/push.go +++ b/cmd/ormb/cmd/push.go @@ -29,7 +29,7 @@ Must first run "ormb save" or "ormb pull".`, PreRunE: preRunE, RunE: func(cmd *cobra.Command, args []string) error { // TODO(gaocegege): Validate. - return ormbClient.Push(args[0]) + return ormbClient.Push(args[0], plainHTTPOpt) }, } diff --git a/pkg/oras/client.go b/pkg/oras/client.go index bce0722e..147ed19e 100644 --- a/pkg/oras/client.go +++ b/pkg/oras/client.go @@ -90,6 +90,13 @@ func NewClient(opts ...ClientOption) (Interface, error) { return client, nil } +// AddOption add opt to Client. +func (c *Client) AddOption(opts ...ClientOption) { + for _, opt := range opts { + opt(c) + } +} + // Login logs into a registry func (c *Client) Login(hostname string, username string, password string, insecure bool) error { if insecure { diff --git a/pkg/oras/interface.go b/pkg/oras/interface.go index f427f31d..e59a6ddc 100644 --- a/pkg/oras/interface.go +++ b/pkg/oras/interface.go @@ -7,6 +7,8 @@ import ( // Interface is the interface of the client. type Interface interface { + AddOption(opts ...ClientOption) + Login(hostname string, username string, password string, insecure bool) error Logout(hostname string) error SaveModel(ch *model.Model, ref *oci.Reference) error diff --git a/pkg/ormb/ormb.go b/pkg/ormb/ormb.go index db6863da..db9ba361 100644 --- a/pkg/ormb/ormb.go +++ b/pkg/ormb/ormb.go @@ -14,8 +14,8 @@ import ( // models with a remote registry. type Interface interface { Login(hostname, username, password string, insecureOpt bool) error - Push(refStr string) error - Pull(refStr string) error + Push(refStr string, plainHTTP bool) error + Pull(refStr string, plainHTTP bool) error Export(refStr, dst string) error Save(src, refStr string) error Tag(refStr, targetStr string) error @@ -45,7 +45,9 @@ func (o ORMB) Login(hostname, username, password string, insecureOpt bool) error return o.client.Login(hostname, username, password, insecureOpt) } -func (o ORMB) Push(refStr string) error { +func (o ORMB) Push(refStr string, plainHTTP bool) error { + o.client.AddOption(oras.ClientOptPlainHTTP(plainHTTP)) + ref, err := oci.ParseReference(refStr) if err != nil { return err @@ -53,7 +55,9 @@ func (o ORMB) Push(refStr string) error { return o.client.PushModel(ref) } -func (o ORMB) Pull(refStr string) error { +func (o ORMB) Pull(refStr string, plainHTTP bool) error { + o.client.AddOption(oras.ClientOptPlainHTTP(plainHTTP)) + ref, err := oci.ParseReference(refStr) if err != nil { return err