Skip to content

Commit b4769c9

Browse files
authored
feat: add http proxy option for pull command (#34)
Signed-off-by: Gaius <[email protected]>
1 parent 111cbf2 commit b4769c9

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

cmd/pull.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var pullCmd = &cobra.Command{
4646
func init() {
4747
flags := pullCmd.Flags()
4848
flags.BoolVarP(&pullConfig.PlainHTTP, "plain-http", "p", false, "use plain HTTP instead of HTTPS")
49+
flags.StringVar(&pullConfig.Proxy, "proxy", "", "use proxy for the pull operation")
4950

5051
if err := viper.BindPFlags(flags); err != nil {
5152
panic(fmt.Errorf("bind cache pull flags to viper: %w", err))
@@ -68,6 +69,10 @@ func runPull(ctx context.Context, target string) error {
6869
opts = append(opts, backend.WithPlainHTTP())
6970
}
7071

72+
if pullConfig.Proxy != "" {
73+
opts = append(opts, backend.WithProxy(pullConfig.Proxy))
74+
}
75+
7176
if err := b.Pull(ctx, target, opts...); err != nil {
7277
return err
7378
}

pkg/backend/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Option func(*Options)
2020

2121
type Options struct {
2222
plainHTTP bool
23+
proxy string
2324
}
2425

2526
// WithPlainHTTP sets the plain HTTP option.
@@ -28,3 +29,10 @@ func WithPlainHTTP() Option {
2829
opts.plainHTTP = true
2930
}
3031
}
32+
33+
// WithProxy sets the proxy option.
34+
func WithProxy(proxy string) Option {
35+
return func(opts *Options) {
36+
opts.proxy = proxy
37+
}
38+
}

pkg/backend/pull.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"net/http"
25+
"net/url"
2426

2527
"github.com/CloudNativeAI/modctl/pkg/storage"
2628

@@ -58,9 +60,23 @@ func (b *backend) Pull(ctx context.Context, target string, opts ...Option) error
5860
return fmt.Errorf("failed to create credential store: %w", err)
5961
}
6062

63+
// create the http client.
64+
httpClient := http.DefaultClient
65+
if options.proxy != "" {
66+
proxyURL, err := url.Parse(options.proxy)
67+
if err != nil {
68+
return fmt.Errorf("failed to parse the proxy URL: %w", err)
69+
}
70+
71+
httpClient.Transport = &http.Transport{
72+
Proxy: http.ProxyURL(proxyURL),
73+
}
74+
}
75+
6176
src.Client = &auth.Client{
6277
Cache: auth.NewCache(),
6378
Credential: credentials.Credential(credStore),
79+
Client: httpClient,
6480
}
6581

6682
if options.plainHTTP {

pkg/config/pull.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package config
1818

1919
type Pull struct {
2020
PlainHTTP bool
21+
Proxy string
2122
}
2223

2324
func NewPull() *Pull {
2425
return &Pull{
2526
PlainHTTP: false,
27+
Proxy: "",
2628
}
2729
}

0 commit comments

Comments
 (0)