Move http timeout to the general configuration

Fixes https://github.com/mudler/luet/issues/250 as now it is documented
in the cli --help too.
This commit is contained in:
Ettore Di Giacinto
2021-10-23 11:41:15 +02:00
parent 57c8236184
commit 315bfb5a54
3 changed files with 7 additions and 13 deletions

View File

@@ -122,6 +122,7 @@ func setDefaults(viper *viper.Viper) {
viper.SetDefault("general.debug", false) viper.SetDefault("general.debug", false)
viper.SetDefault("general.show_build_output", false) viper.SetDefault("general.show_build_output", false)
viper.SetDefault("general.fatal_warnings", false) viper.SetDefault("general.fatal_warnings", false)
viper.SetDefault("general.http_timeout", 360)
u, err := user.Current() u, err := user.Current()
// os/user doesn't work in from scratch environments // os/user doesn't work in from scratch environments
@@ -178,6 +179,7 @@ func InitViper(ctx *types.Context, RootCmd *cobra.Command) {
} }
pflags.Bool("same-owner", ctx.Config.GetGeneral().SameOwner, "Maintain same owner on uncompress.") pflags.Bool("same-owner", ctx.Config.GetGeneral().SameOwner, "Maintain same owner on uncompress.")
pflags.Int("concurrency", runtime.NumCPU(), "Concurrency") pflags.Int("concurrency", runtime.NumCPU(), "Concurrency")
pflags.Int("http-timeout", ctx.Config.General.HTTPTimeout, "Default timeout for http(s) requests")
viper.BindPFlag("logging.color", pflags.Lookup("color")) viper.BindPFlag("logging.color", pflags.Lookup("color"))
viper.BindPFlag("logging.enable_emoji", pflags.Lookup("emoji")) viper.BindPFlag("logging.enable_emoji", pflags.Lookup("emoji"))
@@ -189,6 +191,7 @@ func InitViper(ctx *types.Context, RootCmd *cobra.Command) {
viper.BindPFlag("general.fatal_warnings", pflags.Lookup("fatal")) viper.BindPFlag("general.fatal_warnings", pflags.Lookup("fatal"))
viper.BindPFlag("general.same_owner", pflags.Lookup("same-owner")) viper.BindPFlag("general.same_owner", pflags.Lookup("same-owner"))
viper.BindPFlag("plugin", pflags.Lookup("plugin")) viper.BindPFlag("plugin", pflags.Lookup("plugin"))
viper.BindPFlag("general.http_timeout", pflags.Lookup("http-timeout"))
// Currently I maintain this only from cli. // Currently I maintain this only from cli.
viper.BindPFlag("no_spinner", pflags.Lookup("no-spinner")) viper.BindPFlag("no_spinner", pflags.Lookup("no-spinner"))

View File

@@ -60,6 +60,7 @@ type LuetGeneralConfig struct {
Debug bool `yaml:"debug,omitempty" mapstructure:"debug"` Debug bool `yaml:"debug,omitempty" mapstructure:"debug"`
ShowBuildOutput bool `yaml:"show_build_output,omitempty" mapstructure:"show_build_output"` ShowBuildOutput bool `yaml:"show_build_output,omitempty" mapstructure:"show_build_output"`
FatalWarns bool `yaml:"fatal_warnings,omitempty" mapstructure:"fatal_warnings"` FatalWarns bool `yaml:"fatal_warnings,omitempty" mapstructure:"fatal_warnings"`
HTTPTimeout int `yaml:"http_timeout,omitempty" mapstructure:"http_timeout"`
} }
type LuetSolverOptions struct { type LuetSolverOptions struct {

View File

@@ -23,7 +23,6 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strconv"
"time" "time"
"github.com/mudler/luet/pkg/api/core/types" "github.com/mudler/luet/pkg/api/core/types"
@@ -48,20 +47,11 @@ func NewHttpClient(r RepoData, ctx *types.Context) *HttpClient {
} }
} }
func NewGrabClient() *grab.Client { func NewGrabClient(timeout int) *grab.Client {
httpTimeout := 360
timeout := os.Getenv("HTTP_TIMEOUT")
if timeout != "" {
timeoutI, err := strconv.Atoi(timeout)
if err == nil {
httpTimeout = timeoutI
}
}
return &grab.Client{ return &grab.Client{
UserAgent: "grab", UserAgent: "grab",
HTTPClient: &http.Client{ HTTPClient: &http.Client{
Timeout: time.Duration(httpTimeout) * time.Second, Timeout: time.Duration(timeout) * time.Second,
Transport: &http.Transport{ Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
}, },
@@ -101,7 +91,7 @@ func (c *HttpClient) DownloadFile(p string) (string, error) {
} }
defer os.RemoveAll(temp) defer os.RemoveAll(temp)
client := NewGrabClient() client := NewGrabClient(c.context.Config.General.HTTPTimeout)
for _, uri := range c.RepoData.Urls { for _, uri := range c.RepoData.Urls {
file, err = c.context.Config.GetSystem().TempFile("HttpClient") file, err = c.context.Config.GetSystem().TempFile("HttpClient")