Remove more functions from the util package
parent
aea610b510
commit
3db7713399
|
@ -39,7 +39,6 @@ import (
|
||||||
"k8s.io/minikube/pkg/minikube/command"
|
"k8s.io/minikube/pkg/minikube/command"
|
||||||
"k8s.io/minikube/pkg/minikube/config"
|
"k8s.io/minikube/pkg/minikube/config"
|
||||||
"k8s.io/minikube/pkg/minikube/sshutil"
|
"k8s.io/minikube/pkg/minikube/sshutil"
|
||||||
"k8s.io/minikube/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// generic interface for minikube provisioner
|
// generic interface for minikube provisioner
|
||||||
|
@ -246,7 +245,30 @@ func rootFileSystemType(p provision.SSHCommander) (string, error) {
|
||||||
// (see systemd man pages for more info). This is not supported by minikube, thus needs to be escaped.
|
// (see systemd man pages for more info). This is not supported by minikube, thus needs to be escaped.
|
||||||
func escapeSystemdDirectives(engineConfigContext *provision.EngineConfigContext) {
|
func escapeSystemdDirectives(engineConfigContext *provision.EngineConfigContext) {
|
||||||
// escape '%' in Environment option so that it does not evaluate into a template specifier
|
// escape '%' in Environment option so that it does not evaluate into a template specifier
|
||||||
engineConfigContext.EngineOptions.Env = util.ReplaceChars(engineConfigContext.EngineOptions.Env, systemdSpecifierEscaper)
|
engineConfigContext.EngineOptions.Env = replaceChars(engineConfigContext.EngineOptions.Env, systemdSpecifierEscaper)
|
||||||
// input might contain whitespaces, wrap it in quotes
|
// input might contain whitespaces, wrap it in quotes
|
||||||
engineConfigContext.EngineOptions.Env = util.ConcatStrings(engineConfigContext.EngineOptions.Env, "\"", "\"")
|
engineConfigContext.EngineOptions.Env = concatStrings(engineConfigContext.EngineOptions.Env, "\"", "\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
// replaceChars returns a copy of the src slice with each string modified by the replacer
|
||||||
|
func replaceChars(src []string, replacer *strings.Replacer) []string {
|
||||||
|
ret := make([]string, len(src))
|
||||||
|
for i, s := range src {
|
||||||
|
ret[i] = replacer.Replace(s)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// concatStrings concatenates each string in the src slice with prefix and postfix and returns a new slice
|
||||||
|
func concatStrings(src []string, prefix string, postfix string) []string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
ret := make([]string, len(src))
|
||||||
|
for i, s := range src {
|
||||||
|
buf.WriteString(prefix)
|
||||||
|
buf.WriteString(s)
|
||||||
|
buf.WriteString(postfix)
|
||||||
|
ret[i] = buf.String()
|
||||||
|
buf.Reset()
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,15 +17,11 @@ limitations under the License.
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
units "github.com/docker/go-units"
|
units "github.com/docker/go-units"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -34,12 +30,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ErrPrefix notes an error
|
|
||||||
ErrPrefix = "! "
|
|
||||||
|
|
||||||
// OutPrefix notes output
|
|
||||||
OutPrefix = "> "
|
|
||||||
|
|
||||||
downloadURL = "https://storage.googleapis.com/minikube/releases/%s/minikube-%s-amd64%s"
|
downloadURL = "https://storage.googleapis.com/minikube/releases/%s/minikube-%s-amd64%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,46 +47,6 @@ func CalculateSizeInMB(humanReadableSize string) int {
|
||||||
return int(size / units.MB)
|
return int(size / units.MB)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Until endlessly loops the provided function until a message is received on the done channel.
|
|
||||||
// The function will wait the duration provided in sleep between function calls. Errors will be sent on provider Writer.
|
|
||||||
func Until(fn func() error, w io.Writer, name string, sleep time.Duration, done <-chan struct{}) {
|
|
||||||
var exitErr error
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
exitErr = fn()
|
|
||||||
if exitErr == nil {
|
|
||||||
fmt.Fprintf(w, Pad("%s: Exited with no errors.\n"), name)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(w, Pad("%s: Exit with error: %v"), name, exitErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait provided duration before trying again
|
|
||||||
time.Sleep(sleep)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pad pads the string with newlines
|
|
||||||
func Pad(str string) string {
|
|
||||||
return fmt.Sprintf("\n%s\n", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CanReadFile returns true if the file represented
|
|
||||||
// by path exists and is readable, otherwise false.
|
|
||||||
func CanReadFile(path string) bool {
|
|
||||||
f, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBinaryDownloadURL returns a suitable URL for the platform
|
// GetBinaryDownloadURL returns a suitable URL for the platform
|
||||||
func GetBinaryDownloadURL(version, platform string) string {
|
func GetBinaryDownloadURL(version, platform string) string {
|
||||||
switch platform {
|
switch platform {
|
||||||
|
@ -107,15 +57,6 @@ func GetBinaryDownloadURL(version, platform string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsDirectory checks if path is a directory
|
|
||||||
func IsDirectory(path string) (bool, error) {
|
|
||||||
fileInfo, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
return false, errors.Wrapf(err, "Error calling os.Stat on file %s", path)
|
|
||||||
}
|
|
||||||
return fileInfo.IsDir(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ChownR does a recursive os.Chown
|
// ChownR does a recursive os.Chown
|
||||||
func ChownR(path string, uid, gid int) error {
|
func ChownR(path string, uid, gid int) error {
|
||||||
return filepath.Walk(path, func(name string, info os.FileInfo, err error) error {
|
return filepath.Walk(path, func(name string, info os.FileInfo, err error) error {
|
||||||
|
@ -148,26 +89,3 @@ func MaybeChownDirRecursiveToMinikubeUser(dir string) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceChars returns a copy of the src slice with each string modified by the replacer
|
|
||||||
func ReplaceChars(src []string, replacer *strings.Replacer) []string {
|
|
||||||
ret := make([]string, len(src))
|
|
||||||
for i, s := range src {
|
|
||||||
ret[i] = replacer.Replace(s)
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConcatStrings concatenates each string in the src slice with prefix and postfix and returns a new slice
|
|
||||||
func ConcatStrings(src []string, prefix string, postfix string) []string {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
ret := make([]string, len(src))
|
|
||||||
for i, s := range src {
|
|
||||||
buf.WriteString(prefix)
|
|
||||||
buf.WriteString(s)
|
|
||||||
buf.WriteString(postfix)
|
|
||||||
ret[i] = buf.String()
|
|
||||||
buf.Reset()
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue