Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move DetectWSL function into util package #4721

Merged
merged 2 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions pkg/skaffold/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -164,22 +163,8 @@ func getUserAgentHeader() map[string]string {
}
}

func detectWsl() (bool, error) {
if _, err := os.Stat("/proc/version"); err == nil {
b, err := ioutil.ReadFile("/proc/version")
if err != nil {
return false, fmt.Errorf("read /proc/version: %w", err)
}
str := strings.ToLower(string(b))
if strings.Contains(str, "microsoft") {
return true, nil
}
}
return false, nil
}

func getMiniKubeFilename() (string, error) {
if found, _ := detectWsl(); found {
if found, _ := util.DetectWSL(); found {
filename, err := exec.LookPath("minikube.exe")
if err != nil {
return "", errors.New("unable to find minikube.exe. Please add it to PATH environment variable")
Expand Down Expand Up @@ -221,7 +206,8 @@ func getMinikubeDockerEnv(minikubeProfile string) (map[string]string, error) {
env[kv[0]] = kv[1]
}

if found, _ := detectWsl(); found {
if found, _ := util.DetectWSL(); found {
// rewrite Unix path to Windows
cmd := exec.Command("wslpath", env["DOCKER_CERT_PATH"])
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
out, err := util.RunCmdOut(cmd)
if err == nil {
Expand Down
41 changes: 41 additions & 0 deletions pkg/skaffold/util/wsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2020 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"fmt"
"io/ioutil"
"os"
"strings"
)

// DetectWSL checks for Windows Subsystem for Linux
func DetectWSL() (bool, error) {
if _, err := os.Stat("/proc/version"); err == nil {
b, err := ioutil.ReadFile("/proc/version")
if err != nil {
return false, fmt.Errorf("read /proc/version: %w", err)
}

// Microsoft changed the case between WSL1 and WSL2
str := strings.ToLower(string(b))
if strings.Contains(str, "microsoft") {
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
return true, nil
}
}
return false, nil
}