This repository was archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the code related to local app checks into a seperate package.
- Loading branch information
Showing
5 changed files
with
89 additions
and
72 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package macapp | ||
package localcheck | ||
|
||
import ( | ||
"debug/macho" | ||
|
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,61 @@ | ||
package localcheck | ||
|
||
import ( | ||
"debug/macho" | ||
"os" | ||
"path/filepath" | ||
|
||
"howett.net/plist" | ||
) | ||
|
||
type executableDecoded struct { | ||
CFBundleExecutable string | ||
} | ||
|
||
func getExecutableName(path string) (string, error) { | ||
plistFile := filepath.Join(path, "Contents", "Info.plist") | ||
|
||
f, err := os.Open(plistFile) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
decoder := plist.NewDecoder(f) | ||
var plistDecoded executableDecoded | ||
err = decoder.Decode(&plistDecoded) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return plistDecoded.CFBundleExecutable, err | ||
} | ||
|
||
func GetArchitectures(path string) (Architectures, error) { | ||
var ( | ||
arch = Architectures{} | ||
) | ||
|
||
executable, err := getExecutableName(path) | ||
if err != nil { | ||
return arch, err | ||
} | ||
|
||
// binary file path | ||
binary := filepath.Join(path, "Contents", "MacOS", executable) | ||
|
||
fat, err := macho.OpenFat(binary) | ||
if err == nil { | ||
// file is Mach-O universal | ||
arch.LoadFromFat(fat.Arches) | ||
} else { | ||
// file is Mach-O | ||
f, err := macho.Open(binary) | ||
if err != nil { | ||
return arch, err | ||
} | ||
arch.Load(f.Cpu) | ||
} | ||
|
||
return arch, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package localcheck | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestApplication_GetExecutableName(t *testing.T) { | ||
exec, err := getExecutableName("/System/Applications/Maps.app") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "Maps", exec) | ||
} | ||
|
||
func TestApplication_GetArchitectures(t *testing.T) { | ||
arch, err := GetArchitectures("/System/Applications/Maps.app") | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"Intel 64"}, arch) | ||
} |
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
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