env.go (1700B)
1 // Copyright 2019 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package config 15 16 import ( 17 "os" 18 "runtime" 19 "strconv" 20 "strings" 21 ) 22 23 // GetNumWorkerMultiplier returns the base value used to calculate the number 24 // of workers to use for Hugo's parallel execution. 25 // It returns the value in HUGO_NUMWORKERMULTIPLIER OS env variable if set to a 26 // positive integer, else the number of logical CPUs. 27 func GetNumWorkerMultiplier() int { 28 if gmp := os.Getenv("HUGO_NUMWORKERMULTIPLIER"); gmp != "" { 29 if p, err := strconv.Atoi(gmp); err == nil && p > 0 { 30 return p 31 } 32 } 33 return runtime.NumCPU() 34 } 35 36 // SetEnvVars sets vars on the form key=value in the oldVars slice. 37 func SetEnvVars(oldVars *[]string, keyValues ...string) { 38 for i := 0; i < len(keyValues); i += 2 { 39 setEnvVar(oldVars, keyValues[i], keyValues[i+1]) 40 } 41 } 42 43 func SplitEnvVar(v string) (string, string) { 44 name, value, _ := strings.Cut(v, "=") 45 return name, value 46 } 47 48 func setEnvVar(vars *[]string, key, value string) { 49 for i := range *vars { 50 if strings.HasPrefix((*vars)[i], key+"=") { 51 (*vars)[i] = key + "=" + value 52 return 53 } 54 } 55 // New var. 56 *vars = append(*vars, key+"="+value) 57 }