-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix: try finding a socket, otherwise fail, respect user choice #1745
Conversation
🦙 MegaLinter status: ✅ SUCCESS
See detailed report in MegaLinter reports |
Codecov Report
@@ Coverage Diff @@
## master #1745 +/- ##
==========================================
+ Coverage 61.22% 62.28% +1.06%
==========================================
Files 46 48 +2
Lines 7141 7576 +435
==========================================
+ Hits 4372 4719 +347
- Misses 2462 2527 +65
- Partials 307 330 +23
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
dd76c7e
to
213f545
Compare
213f545
to
d5b2141
Compare
// returns socket path or false if not found any | ||
func socketLocation() (string, bool) { | ||
if dockerHost, exists := os.LookupEnv("DOCKER_HOST"); exists { | ||
return dockerHost, true | ||
} | ||
|
||
for _, p := range commonSocketPaths { | ||
if _, err := os.Lstat(os.ExpandEnv(p)); err == nil { | ||
if strings.HasPrefix(p, `\\.\`) { | ||
return "npipe://" + os.ExpandEnv(p), true | ||
} | ||
return "unix://" + os.ExpandEnv(p), true | ||
} | ||
} | ||
|
||
return "", false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
// returns socket path or false if not found any | |
func socketLocation() (string, bool) { | |
if dockerHost, exists := os.LookupEnv("DOCKER_HOST"); exists { | |
return dockerHost, true | |
} | |
for _, p := range commonSocketPaths { | |
if _, err := os.Lstat(os.ExpandEnv(p)); err == nil { | |
if strings.HasPrefix(p, `\\.\`) { | |
return "npipe://" + os.ExpandEnv(p), true | |
} | |
return "unix://" + os.ExpandEnv(p), true | |
} | |
} | |
return "", false | |
} | |
// socketLocation returns socket path or false if not found any | |
func socketLocation() (string, bool) { | |
if dockerHost, exists := os.LookupEnv("DOCKER_HOST"); exists { | |
return dockerHost, true | |
} | |
for _, p := range commonSocketPaths { | |
p = os.ExpandEnv(p) | |
// `os.Stat` to find a working file, not `os.Lstate`, or the mode could be `ModeSymlink` | |
if info, err := os.Stat(p); err == nil { | |
switch info.Mode().Type() { | |
case os.ModeSocket: | |
return "unix://" + p, true | |
case os.ModeNamedPipe: | |
return "npipe://" + p, true | |
default: | |
return p, true | |
} | |
} | |
} | |
return "", false | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the suggestions of wolfogre, but that's no reason for me to not approve your PR.
Windows works, however I was unshure that npipe://\\.\pipe\docker_engine
is accepted by docker due to mixed \
and /
.
@@ -118,6 +118,33 @@ func configLocations() []string { | |||
} | |||
} | |||
|
|||
var commonSocketPaths = []string{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: Jason Song <[email protected]>
Co-authored-by: Jason Song <[email protected]>
Fixes #1658