Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pkg/cmd/extension/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,28 @@ func (m *Manager) installBin(repo ghrepo.Interface, target string) error {
}

platform, ext := m.platform()
isMacARM := platform == "darwin-arm64"
trueARMBinary := false

var asset *releaseAsset
for _, a := range r.Assets {
if strings.HasSuffix(a.Name, platform+ext) {
asset = &a
trueARMBinary = isMacARM
break
}
}

// if an arm64 binary is unavailable, fall back to amd64 if it can be executed through Rosetta 2
if asset == nil && isMacARM && hasRosetta() {
for _, a := range r.Assets {
if strings.HasSuffix(a.Name, "darwin-amd64") {
asset = &a
break
}
}
}

if asset == nil {
return fmt.Errorf(
"%[1]s unsupported for %[2]s. Open an issue: `gh issue create -R %[3]s/%[1]s -t'Support %[2]s'`",
Expand All @@ -404,6 +418,11 @@ func (m *Manager) installBin(repo ghrepo.Interface, target string) error {
if err != nil {
return fmt.Errorf("failed to download asset %s: %w", asset.Name, err)
}
if trueARMBinary {
if err := codesignBinary(binPath); err != nil {
return fmt.Errorf("failed to codesign downloaded binary: %w", err)
}
}
}

manifest := binManifest{
Expand Down Expand Up @@ -844,3 +863,17 @@ func possibleDists() []string {
"windows-arm",
}
}

func hasRosetta() bool {
_, err := os.Stat("/Library/Apple/usr/libexec/oah/libRosettaRuntime")
return err == nil
}

func codesignBinary(binPath string) error {
codesignExe, err := safeexec.LookPath("codesign")
if err != nil {
return err
}
cmd := exec.Command(codesignExe, "--sign", "-", "--force", "--preserve-metadata=entitlements,requirements,flags,runtime", binPath)
return cmd.Run()
}