Skip to content

Commit f54a62b

Browse files
authored
Move DetectWSL function into util package (#4721)
* Move `DetectWSL` function into util package * add comments
1 parent 9cd8bdd commit f54a62b

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

pkg/skaffold/docker/client.go

+3-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"io/ioutil"
2423
"net/http"
2524
"os"
2625
"os/exec"
@@ -164,22 +163,8 @@ func getUserAgentHeader() map[string]string {
164163
}
165164
}
166165

167-
func detectWsl() (bool, error) {
168-
if _, err := os.Stat("/proc/version"); err == nil {
169-
b, err := ioutil.ReadFile("/proc/version")
170-
if err != nil {
171-
return false, fmt.Errorf("read /proc/version: %w", err)
172-
}
173-
str := strings.ToLower(string(b))
174-
if strings.Contains(str, "microsoft") {
175-
return true, nil
176-
}
177-
}
178-
return false, nil
179-
}
180-
181166
func getMiniKubeFilename() (string, error) {
182-
if found, _ := detectWsl(); found {
167+
if found, _ := util.DetectWSL(); found {
183168
filename, err := exec.LookPath("minikube.exe")
184169
if err != nil {
185170
return "", errors.New("unable to find minikube.exe. Please add it to PATH environment variable")
@@ -221,7 +206,8 @@ func getMinikubeDockerEnv(minikubeProfile string) (map[string]string, error) {
221206
env[kv[0]] = kv[1]
222207
}
223208

224-
if found, _ := detectWsl(); found {
209+
if found, _ := util.DetectWSL(); found {
210+
// rewrite Unix path to Windows
225211
cmd := exec.Command("wslpath", env["DOCKER_CERT_PATH"])
226212
out, err := util.RunCmdOut(cmd)
227213
if err == nil {

pkg/skaffold/util/wsl.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2020 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
"os"
23+
"strings"
24+
)
25+
26+
// DetectWSL checks for Windows Subsystem for Linux
27+
func DetectWSL() (bool, error) {
28+
if _, err := os.Stat("/proc/version"); err == nil {
29+
b, err := ioutil.ReadFile("/proc/version")
30+
if err != nil {
31+
return false, fmt.Errorf("read /proc/version: %w", err)
32+
}
33+
34+
// Microsoft changed the case between WSL1 and WSL2
35+
str := strings.ToLower(string(b))
36+
if strings.Contains(str, "microsoft") {
37+
return true, nil
38+
}
39+
}
40+
return false, nil
41+
}

0 commit comments

Comments
 (0)