Skip to content

Commit

Permalink
Restructure and rewrite (#91)
Browse files Browse the repository at this point in the history
* fix: show verbose ELF headers only in case of errors

* style: Add spacing around concat operator

* style: remove redundant parenthesis

* fix: remove verbose logging to stdout elf sections

* feat: add helper function to determine if File or Folder exists

* feat: provide overwriteSecretFiles as an argument to function and remove flag dependency

The overwritePtr flag was defined globally which created conflicts
when calling functions within the setupsigning.go from other functions
i.e not through the command line. Poroviding an boolean argument solves
the problem

* feat: add a new dependency 'cli' instead of 'flag'

'cli' is a better module which helps to make the command line interfaces
more neater and well structured, both functionally as well as visually.
this would help
* users to understand the tool, as its style syncs with other popular
cli
* developers, very nice functional interface, best for programmers and
new contributors who are not familiar with the flag command

A pre-exisiting module, which is in its v2 is better than a self
implemented command line parser / low level go module .

* style: define constants globally

* feat: provide options DeployOptions to pass build instructions from cli to functions

* feat: restructure and rewrite appimagetool.go, use urfave/cli/v2

* chore: update go dependencies

* fix: do not pass options through all functions

instead declare a single options->DeployOptions in appimagetool.go prior to calling AppImageBuild()

* docs: add documentation for functions

* fix: use log.Fatal instead os.Write.StdErr as log.Fatal has built in Error handling and automatically exits with status 1

* style: explicitly ignore errors in removing and chmod'ing the file

* fix: returned err variable was not received. The check was done for the previous step instead

* style: use named arguments

* fix: use lower case in appimagetool name in cli

https://github.com/probonopd/go-appimage/issues/90\#issuecomment-720104568
  • Loading branch information
srevinsaju authored Nov 8, 2020
1 parent 31b6a3f commit 2fdd766
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 224 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9
github.com/urfave/cli/v2 v2.2.0
go.lsp.dev/uri v0.3.0
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/acobaugh/osrelease v0.0.0-20181218015638-a93a0a55a249 h1:fMi9ZZ/it4orHj3xWrM6cLkVFcCbkXQALFUiNtHtCPs=
Expand All @@ -16,6 +17,8 @@ github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEe
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/coreos/go-systemd/v22 v22.0.0 h1:XJIw/+VlJ+87J+doOxznsAWIdmWuViOVhkQamW5YV28=
github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -87,6 +90,8 @@ github.com/prometheus/procfs v0.0.10 h1:QJQN3jYQhkamO4mhfUWqdDH2asK7ONOI9MTWjyAx
github.com/prometheus/procfs v0.0.10/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sabhiram/png-embed v0.0.0-20180421025336-149afe9a3ccb h1:AuqP2DCYGHIPOtYhnvXiUOk2bneES9vTYfX3bBiwHdQ=
github.com/sabhiram/png-embed v0.0.0-20180421025336-149afe9a3ccb/go.mod h1:GwEonSgMIDQak3yZSNNKyrfVKJ23krN3Y/r51V8Ytr8=
github.com/sabhiram/pngr v0.0.0-20180419043407-2df49b015d4b h1:ks2d0TH6CtUISoThcRTP2jECmBMH3r/Dy1Q0GdfoD9E=
Expand All @@ -97,6 +102,8 @@ github.com/shirou/gopsutil v2.20.2+incompatible h1:ucK79BhBpgqQxPASyS2cu9HX8cfDV
github.com/shirou/gopsutil v2.20.2+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shuheiktgw/go-travis v0.2.4 h1:IAnh/Dyv7ql87qtJWUcvR5MM8e5iCDGoENl9VzpxAHc=
github.com/shuheiktgw/go-travis v0.2.4/go.mod h1:RtODX49bvgHTvfzFvGEPFtU0dKVk0D3PyvUQR/63hT0=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand All @@ -111,6 +118,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
go.lsp.dev/uri v0.3.0 h1:KcZJmh6nFIBeJzTugn5JTU6OOyG0lDOo3R9KwTxTYbo=
Expand Down Expand Up @@ -152,6 +161,7 @@ golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapK
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.55.0 h1:E8yzL5unfpW3M6fz/eB7Cb5MQAYSZ7GKo4Qth+N2sgQ=
Expand All @@ -164,3 +174,4 @@ gopkg.in/src-d/go-git.v4 v4.13.1 h1:SRtFyV8Kxc0UP7aCHcijOMQGPxHSmMOPrzulQWolkYE=
gopkg.in/src-d/go-git.v4 v4.13.1/go.mod h1:nx5NYcxdKxq5fpltdHnPa2Exj4Sx0EclMWZQbYDu2z8=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
22 changes: 13 additions & 9 deletions internal/helpers/elfsize.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,20 @@ func EmbedStringInSegment(path string, section string, s string) error {
return (err)
}
fmt.Println("")
fmt.Println("Section " + section + " before embedding:")
fmt.Println(uidata)
// fmt.Println("Section " + section + " before embedding:")
// fmt.Println(uidata)
fmt.Println("")
uioffset, uilength, err := GetSectionOffsetAndLength(path, section)
PrintError("GetSectionData for '"+section+"'", err)
if err != nil {
// only show the before embedding part as its too verbose in case of errors
fmt.Println("Section " + section + " before embedding:")
fmt.Println(uidata)
os.Stderr.WriteString("Could not determine Offset and Length of " + section + " in runtime, exiting\n")
return (err)
}
fmt.Println("Embedded "+section+" section Offset:", uioffset)
fmt.Println("Embedded "+section+" section Length:", uilength)
fmt.Println("Embedded " + section + " section Offset:", uioffset)
fmt.Println("Embedded " + section + " section Length:", uilength)
fmt.Println("")
// Exit if data exceeds available space in section
if len(s) > len(uidata) {
Expand All @@ -137,17 +140,18 @@ func EmbedStringInSegment(path string, section string, s string) error {
return (err)
}
uidata, err = GetSectionData(path, section)
PrintError("GetSectionData for '"+section+"'", err)
PrintError("GetSectionData for '" + section + "'", err)
if err != nil {
os.Stderr.WriteString("Could not find section " + section + " in runtime, exiting\n")
return (err)
return err
}
fmt.Println("")
fmt.Println("Embedded " + section + " section after embedding:")
fmt.Println(uidata)
// FIXME: Might be too verbose? I am removing it
// fmt.Println("Embedded " + section + " section after embedding:")
// fmt.Println(uidata)
fmt.Println("")
fmt.Println("Embedded " + section + " section now contains:")
fmt.Println(string(uidata))
fmt.Println("")
return (nil)
return nil
}
12 changes: 12 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func CheckIfFileExists(filepath string) bool {
return true
}


// CheckIfFileOrFolderExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
// Returns true if it does, false otherwise.
func CheckIfFileOrFolderExists(filepath string) bool {
_, err := os.Stat(filepath)
if os.IsNotExist(err){
return false
}
return true
}

// CheckIfExecFileExists checks whether a desktop file
// that points to an-existing Exec= entries.
// Returns true if it does, false otherwise.
Expand Down
51 changes: 30 additions & 21 deletions src/appimagetool/appdirtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"log"
"path"
"strconv"
"syscall"

Expand Down Expand Up @@ -165,8 +166,18 @@ var packagesContainingFiles = make(map[string]string) // Need to use 'make', oth
/usr/lib64.) If the binary was linked with the -z nodeflib linker option, this step is skipped.
*/

func AppDirDeploy(path string) {

type DeployOptions struct {
standalone bool
libAppRunHooks bool
}

// this is the public options instance
// which need to be set before the function is called
var options DeployOptions


func AppDirDeploy(path string) {
appdir, err := helpers.NewAppDir(path)
if err != nil {
helpers.PrintError("AppDir", err)
Expand Down Expand Up @@ -214,7 +225,7 @@ func AppDirDeploy(path string) {
}

// AppRun
if *libapprun_hooksPtr == false {
if options.libAppRunHooks == false {
// If libapprun_hooks is not used
log.Println("Adding AppRun...")
err = ioutil.WriteFile(appdir.Path+"/AppRun", []byte(AppRunData), 0755)
Expand Down Expand Up @@ -285,7 +296,6 @@ func AppDirDeploy(path string) {
for _, lib := range allELFs {

deployElf(lib, appdir, err)

patchRpathsInElf(appdir, libraryLocationsInAppDir, lib)

if strings.Contains(lib, "libQt5Core.so.5") {
Expand Down Expand Up @@ -329,7 +339,7 @@ func deployInterpreter(appdir helpers.AppDir) (string, error) {
}

}
if *standalonePtr == true {
if options.libAppRunHooks {
var err error
// ld-linux might be a symlink; hence we first need to resolve it
src, err := filepath.EvalSymlinks(ldLinux)
Expand All @@ -341,14 +351,14 @@ func deployInterpreter(appdir helpers.AppDir) (string, error) {
log.Println("Deploying", ldLinux+"...")

ldTargetPath := appdir.Path + ldLinux
if *libapprun_hooksPtr == true {
if options.libAppRunHooks {
// This file is part of the libc family of libraries and we want to use libapprun_hooks,
// hence copy to a separate directory unlike the rest of the libraries. The reason is
// that this familiy of libraries will only be used by libapprun_hooks if the
// bundled version is newer than what is already on the target system; this allows
// us to also load libraries from the system such as proprietary GPU drivers
log.Println(ldLinux, "is part of libc; copy to", libc_dir, "subdirectory")
ldTargetPath = appdir.Path + "/" + libc_dir + "/" + ldLinux // If libapprun_hooks is used
log.Println(ldLinux, "is part of libc; copy to", LibcDir, "subdirectory")
ldTargetPath = appdir.Path + "/" + LibcDir + "/" + ldLinux // If libapprun_hooks is used
}
err = copy.Copy(src, ldTargetPath)
if err != nil {
Expand Down Expand Up @@ -397,7 +407,7 @@ func deployInterpreter(appdir helpers.AppDir) (string, error) {
// if it is not on the exclude list and it is not yet at the target location
func deployElf(lib string, appdir helpers.AppDir, err error) {
for _, excludePrefix := range ExcludedLibraries {
if strings.HasPrefix(filepath.Base(lib), excludePrefix) == true && *standalonePtr == false {
if strings.HasPrefix(filepath.Base(lib), excludePrefix) == true && ! options.standalone {
log.Println("Skipping", lib, "because it is on the excludelist")
return
}
Expand All @@ -406,14 +416,14 @@ func deployElf(lib string, appdir helpers.AppDir, err error) {
log.Println("Working on", lib, "(TODO: Remove this message)")
if strings.HasPrefix(lib, appdir.Path) == false { // Do not copy if it is already in the AppDir
libTargetPath := appdir.Path + "/" + lib
if *libapprun_hooksPtr == true && checkWhetherPartOfLibc(lib) == true {
if options.libAppRunHooks && checkWhetherPartOfLibc(lib) == true {
// This file is part of the libc family of libraries and we want to use libapprun_hooks,
// hence copy to a separate directory unlike the rest of the libraries. The reason is
// that this familiy of libraries will only be used by libapprun_hooks if the
// bundled version is newer than what is already on the target system; this allows
// us to also load libraries from the system such as proprietary GPU drivers
log.Println(lib, "is part of libc; copy to", libc_dir, "subdirectory")
libTargetPath = appdir.Path + "/" + libc_dir + "/" + lib // If libapprun_hooks is used
log.Println(lib, "is part of libc; copy to", LibcDir, "subdirectory")
libTargetPath = appdir.Path + "/" + LibcDir + "/" + lib // If libapprun_hooks is used
}
log.Println("Copying to libTargetPath:", libTargetPath, "(TODO: Remove this message)")

Expand Down Expand Up @@ -502,7 +512,7 @@ func deployCopyrightFiles(appdir helpers.AppDir) {

shouldDoIt := true
for _, excludePrefix := range ExcludedLibraries {
if strings.HasPrefix(filepath.Base(lib), excludePrefix) == true && *standalonePtr == false {
if strings.HasPrefix(filepath.Base(lib), excludePrefix) == true && options.standalone == false {
log.Println("Skipping copyright file for ", lib, "because it is on the excludelist")
shouldDoIt = false
break
Expand All @@ -521,12 +531,12 @@ func deployCopyrightFiles(appdir helpers.AppDir) {
}
}
log.Println("Done")
if *standalonePtr == true {
if options.standalone == true {
log.Println("To check whether it is really self-contained, run:")
fmt.Println("LD_LIBRARY_PATH='' find " + appdir.Path + " -type f -exec ldd {} 2>&1 \\; | grep '=>' | grep -v " + appdir.Path)
}

if *libapprun_hooksPtr == true {
if options.libAppRunHooks == true {
log.Println("The option '-m' was used. Hence, you need to manually add AppRun, .env, and libapprun_hooks.so")
log.Println("from https://github.com/AppImageCrafters/AppRun/releases/tag/continuous. TODO: Automate this")
}
Expand Down Expand Up @@ -701,7 +711,7 @@ func patchRpathsInElf(appdir helpers.AppDir, libraryLocationsInAppDir []string,
newRpathStringForElf = strings.Join(newRpathStrings, ":")
// fmt.Println("Computed newRpathStringForElf:", appdir.Path+"/"+lib, newRpathStringForElf)

if *libapprun_hooksPtr == true && checkWhetherPartOfLibc(path) {
if options.libAppRunHooks && checkWhetherPartOfLibc(path) {
log.Println("Not writing rpath because file is part of the libc family of libraries")
return
}
Expand Down Expand Up @@ -786,7 +796,7 @@ func deployGtkDirectory(appdir helpers.AppDir, gtkVersion int) {
func appendLib(path string) {

for _, excludedlib := range ExcludedLibraries {
if filepath.Base(path) == excludedlib && *standalonePtr == false {
if filepath.Base(path) == excludedlib && ! options.standalone {
// log.Println("Skipping", excludedlib, "because it is on the excludelist")
return
}
Expand Down Expand Up @@ -1335,13 +1345,12 @@ func handleQt(appdir helpers.AppDir, qtVersion int) {
log.Println("qmlImport.Name:", qmlImport.Name)
log.Println("qmlImport.Path:", qmlImport.Path)
log.Println("qmlImport.RelativePath:", qmlImport.RelativePath)
os.MkdirAll(filepath.Dir(appdir.Path+"/"+qmlImport.Path), 0755)
copy.Copy(qmlImport.Path, appdir.Path+"/"+qmlImport.Path) // FIXME: Ideally we would not copy here but only after the point where we start copying everything
determineELFsInDirTree(appdir, appdir.Path+"/"+qmlImport.Path)
os.MkdirAll(filepath.Dir(path.Join(appdir.Path, qmlImport.Path)), 0755)
copy.Copy(qmlImport.Path, path.Join(appdir.Path, qmlImport.Path)) // FIXME: Ideally we would not copy here but only after the point where we start copying everything
path.Join("sss", "sss")
determineELFsInDirTree(appdir, path.Join(appdir.Path, qmlImport.Path))
}
}

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
}
}

Expand Down
Loading

0 comments on commit 2fdd766

Please sign in to comment.