Skip to content
This repository was archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Move the code related to local app checks into a seperate package.
  • Loading branch information
harryzcy committed Mar 28, 2021
1 parent f3fd4ac commit b93408a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 72 deletions.
2 changes: 1 addition & 1 deletion internal/macapp/arch.go → internal/localcheck/arch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package macapp
package localcheck

import (
"debug/macho"
Expand Down
61 changes: 61 additions & 0 deletions internal/localcheck/local.go
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
}
19 changes: 19 additions & 0 deletions internal/localcheck/local_test.go
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)
}
60 changes: 8 additions & 52 deletions internal/macapp/macapp.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package macapp

import (
"debug/macho"
"io/fs"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"

"howett.net/plist"
"github.com/harryzcy/ascheck/internal/localcheck"
)

var (
Expand All @@ -30,55 +29,7 @@ func init() {
type Application struct {
Name string
Path string
Architectures Architectures
}

type executableDecoded struct {
CFBundleExecutable string
}

func (a *Application) GetExecutableName() (string, error) {
plistFile := filepath.Join(a.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 (a *Application) GetArchitectures() (Architectures, error) {
executable, err := a.GetExecutableName()
if err != nil {
return a.Architectures, err
}

// binary file path
binary := filepath.Join(a.Path, "Contents", "MacOS", executable)

fat, err := macho.OpenFat(binary)
if err == nil {
// file is Mach-O universal
a.Architectures.LoadFromFat(fat.Arches)
} else {
// file is Mach-O
f, err := macho.Open(binary)
if err != nil {
return a.Architectures, err
}
a.Architectures.Load(f.Cpu)
}

return a.Architectures, nil
Architectures localcheck.Architectures
}

// GetAllApplications returns all applications
Expand Down Expand Up @@ -119,7 +70,12 @@ func resolveApplication(dir string, f fs.FileInfo) (Application, error) {
Name: strings.TrimSuffix(f.Name(), ".app"),
Path: filepath.Join(dir, f.Name()),
}
app.GetArchitectures()

var err error
app.Architectures, err = localcheck.GetArchitectures(app.Path)
if err != nil {
return Application{}, err
}

return app, nil
}
19 changes: 0 additions & 19 deletions internal/macapp/macapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestApplication_GetExecutableName(t *testing.T) {
app := Application{
Path: "/System/Applications/Maps.app",
}

exec, err := app.GetExecutableName()
assert.Nil(t, err)
assert.Equal(t, "Maps", exec)
}

func TestApplication_GetArchitectures(t *testing.T) {
app := Application{
Path: "/System/Applications/Maps.app",
}
arch, err := app.GetArchitectures()
assert.Nil(t, err)
assert.Equal(t, []string{"Intel 64"}, arch)
}

func TestGetAllApplications(t *testing.T) {
apps, err := GetAllApplications(nil)
assert.Nil(t, err)
Expand Down

0 comments on commit b93408a

Please sign in to comment.