add system proxy config support for cli requests

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2022-12-27 15:44:31 -08:00
parent 88852e2330
commit 4992574ed9
No known key found for this signature in database
GPG Key ID: AFA9DE5F8AB7AF39
2 changed files with 47 additions and 0 deletions

View File

@ -56,6 +56,11 @@ func main() {
os.Exit(1)
}
if err := configureProxy(cmd); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if plugin.RunningStandalone() {
err = runStandalone(cmd)
} else {

42
cmd/buildx/proxy.go Normal file
View File

@ -0,0 +1,42 @@
package main
import (
"encoding/json"
"os"
"path/filepath"
"github.com/docker/buildx/util/confutil"
"github.com/docker/cli/cli/command"
"github.com/pkg/errors"
)
func configureProxy(cmd *command.DockerCli) error {
fp := filepath.Join(confutil.ConfigDir(cmd), "proxy.json")
dt, err := os.ReadFile(fp)
if err != nil {
if os.IsNotExist(err) || os.IsPermission(err) {
return nil
}
return err
}
env := map[string]string{}
if err := json.Unmarshal(dt, &env); err != nil {
return errors.Wrapf(err, "failed to parse proxy config %s", fp)
}
permitted := map[string]struct{}{
"HTTP_PROXY": {},
"HTTPS_PROXY": {},
"NO_PROXY": {},
"FTP_PROXY": {},
"ALL_PROXY": {},
}
for k := range permitted {
if _, ok := os.LookupEnv(k); ok {
continue
}
if val, ok := env[k]; ok {
os.Setenv(k, val)
}
}
return nil
}