-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/osutil: move MacOSProductVersion() from pkg/qemu
Signed-off-by: Akihiro Suda <[email protected]>
- Loading branch information
1 parent
52c68fa
commit 2c2c4d5
Showing
2 changed files
with
31 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package osutil | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/coreos/go-semver/semver" | ||
) | ||
|
||
// MacOSProductVersion returns the macOS product version like "12.3.1". | ||
func MacOSProductVersion() (*semver.Version, error) { | ||
cmd := exec.Command("sw_vers", "-productVersion") | ||
// output is like "12.3.1\n" | ||
b, err := cmd.Output() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to execute %v: %w", cmd.Args, err) | ||
} | ||
verTrimmed := strings.TrimSpace(string(b)) | ||
// macOS 12.4 returns just "12.4\n" | ||
for strings.Count(verTrimmed, ".") < 2 { | ||
verTrimmed += ".0" | ||
} | ||
verSem, err := semver.NewVersion(verTrimmed) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse macOS version %q: %w", verTrimmed, err) | ||
} | ||
return verSem, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters