Skip to content

cli: streamline autostart ux #2251

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

Merged
merged 14 commits into from
Jun 13, 2022
Prev Previous commit
Next Next commit
fix handling of powershell output
  • Loading branch information
johnstcn committed Jun 10, 2022
commit bcb1e8c385785d107e3b0f06dab1d93c7ff9ab33
13 changes: 10 additions & 3 deletions coderd/util/tz/tz_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
package tz

import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"

"golang.org/x/xerrors"
)

// cmdTimezone is a Powershell incantation that will return the system
// time location in IANA format.
const cmdTimezone = "[Windows.Globalization.Calendar,Windows.Globalization,ContentType=WindowsRuntime]::New().GetTimeZone()"

// TimezoneIANA attempts to determine the local timezone in IANA format.
Expand Down Expand Up @@ -49,12 +51,17 @@ func TimezoneIANA() (*time.Location, error) {

<-done

out, err := cmd.CombinedOutput()
outBytes, err := cmd.CombinedOutput()
if err != nil {
return nil, xerrors.Errorf("execute powershell command %q: %w", cmdTimezone, err)
}

locStr := string(bytes.TrimSpace(out))
outLines := strings.Split(string(outBytes), "\n")
if len(outLines) < 2 {
return nil, xerrors.Errorf("unexpected output from powershell command %q: %q", cmdTimezone, outLines)
}
// What we want is the second line of output
locStr := strings.TrimSpace(outLines[1])
loc, err := time.LoadLocation(locStr)
if err != nil {
return nil, xerrors.Errorf("invalid location %q from powershell: %w", locStr, err)
Expand Down