Skip to content

Commit

Permalink
fix logic around getting WSL IP, add more event logging
Browse files Browse the repository at this point in the history
  • Loading branch information
shayne committed Nov 12, 2019
1 parent b76050d commit 2d93fce
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/wsl2host/internal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ loop:
for {
select {
case <-tick:
err := service.Run()
err := service.Run(elog)
if err != nil {
elog.Error(1, fmt.Sprintf("%v", err))
}
Expand Down
14 changes: 10 additions & 4 deletions cmd/wsl2host/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/shayne/go-wsl2-host/internal/wsl2hosts"
"golang.org/x/sys/windows/svc/debug"

"github.com/shayne/go-wsl2-host/pkg/hostsapi"

Expand All @@ -25,14 +26,16 @@ func distroNameToHostname(distroname string) string {
}

// Run main entry point to service logic
func Run() error {
func Run(elog debug.Log) error {
infos, err := wslapi.GetAllInfo()
if err != nil {
elog.Error(1, fmt.Sprintf("failed to get infos: %v", err))
return fmt.Errorf("failed to get infos: %w", err)
}

hapi, err := hostsapi.CreateAPI("wsl2-host") // filtere only managed host entries
if err != nil {
elog.Error(1, fmt.Sprintf("failed to create hosts api: %v", err))
return fmt.Errorf("failed to create hosts api: %w", err)
}

Expand All @@ -52,10 +55,11 @@ func Run() error {

// update IPs of running distros
ip, err := wslapi.GetIP(i.Name)
if err != nil {
elog.Info(1, fmt.Sprintf("failed to get IP for distro %q: %v", i.Name, err))
continue
}
if he, exists := hostentries[hostname]; exists {
if err != nil {
return fmt.Errorf("failed to get IP for distro %q: %w", i.Name, err)
}
if he.IP != ip {
updated = true
he.IP = ip
Expand All @@ -76,6 +80,7 @@ func Run() error {
// process aliases
defdistro, _ := wslapi.GetDefaultDistro()
if err != nil {
elog.Error(1, fmt.Sprintf("GetDefaultDistro failed: %v", err))
return fmt.Errorf("GetDefaultDistro failed: %w", err)
}
var aliasmap = make(map[string]interface{})
Expand Down Expand Up @@ -125,6 +130,7 @@ func Run() error {
if updated {
err = hapi.Write()
if err != nil {
elog.Error(1, fmt.Sprintf("failed to write hosts file: %v", err))
return fmt.Errorf("failed to write hosts file: %w", err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/wslapi/wslapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func GetIP(name string) (string, error) {
if err != nil {
return "", fmt.Errorf("IsRunning failed: %w", err)
}
if running {
return wslcli.GetIP(name)
if !running {
return "", fmt.Errorf("GetIP failed, distro '%s' is not running", name)
}
return "", fmt.Errorf("GetIP failed, distro '%s' is not running", name)
return wslcli.GetIP(name)
}

// GetHostAliases returns custom hosts referenced in `~/.wsl2hosts`
Expand Down
3 changes: 2 additions & 1 deletion pkg/wslcli/wslcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func GetIP(name string) (string, error) {
return "", err
}
sout := string(out)
sout = strings.TrimSpace(sout)
ips := strings.Split(sout, " ")
if len(ips) == 0 {
if sout == "" || len(ips) == 0 {
return "", errors.New("invalid output from hostname -I")
}
// first IP is the correct interface
Expand Down

0 comments on commit 2d93fce

Please sign in to comment.