Skip to content

Commit

Permalink
add Delete and list features
Browse files Browse the repository at this point in the history
  • Loading branch information
Drosaca committed Dec 16, 2023
1 parent 2ae6b49 commit d8491c8
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 15 deletions.
18 changes: 18 additions & 0 deletions desktopFile/desktopFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type DesktopFile struct {
data map[string]map[string]string
selectedCategory string
source string
}

func (d DesktopFile) Category(name string) DesktopFile {
Expand Down Expand Up @@ -39,10 +40,12 @@ func New() *DesktopFile {
desktop := new(DesktopFile)
desktop.data = make(map[string]map[string]string)
desktop.selectedCategory = "root"
desktop.source = "self generated"
return desktop
}
func (d DesktopFile) FromMap(desktopMap map[string]map[string]string) {
d.data = desktopMap
d.source = "self-generated"
}

func (d DesktopFile) FromFile(path string) error {
Expand All @@ -52,6 +55,7 @@ func (d DesktopFile) FromFile(path string) error {
}
scanner := bufio.NewScanner(file)
d.parseFile(scanner)
d.source = path
return nil
}

Expand All @@ -75,3 +79,17 @@ func (d DesktopFile) ToFile(path string) error {
}
return nil
}

func (d DesktopFile) HasValues(category string, values []string) bool {
for _, value := range values {
_, err := d.Category(category).Get(value)
if err != nil {
return false
}
}
return true
}

func (d DesktopFile) GetSource() string {
return d.source
}
80 changes: 65 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
f "appImageInstaller/functions"
"appImageInstaller/manager"
"appImageInstaller/structs"
"appImageInstaller/utils"
"fmt"
Expand Down Expand Up @@ -96,30 +97,79 @@ func install(config structs.Config) {

}

func main() {

if len(os.Args) != 2 || os.Args[1] == "-h" {
fmt.Println("usage: sudo appinstall path/to/app.AppImage")
fmt.Println("after install use sudo update-desktop-database to reload gnome icons")
os.Exit(0)
}
if os.Args[1] == "-v" {
fmt.Println("0.5")
os.Exit(0)
}
path, _ := filepath.Abs(os.Args[1])
func runInstallScript(appPath string) error {
path, _ := filepath.Abs(appPath)
_, err := os.Stat(path)
if err != nil {
log.Fatal(err)
return fmt.Errorf("install", appPath, ":", err)
}
config := setConfig(path)
err = os.RemoveAll(config.ExtractDir)
if err != nil {
log.Fatal("removing", err)
return fmt.Errorf("removing extract directory :", err)
}
install(config)
err = os.RemoveAll(config.ExtractDir)
if err != nil {
log.Fatal("removing: ", err)
return fmt.Errorf("removing extract dir: ", err)
}
return nil
}

func help() {
fmt.Println("usage: sudo appinstall path/to/app.AppImage")
fmt.Println("(after install use sudo update-desktop-database to reload gnome icons)")
fmt.Println("other options [BETA]: ")
fmt.Println("-l #to list installed apps (from this tool only)")
fmt.Println(" #can also raise some errors about other desktop files")
fmt.Println(" ")
fmt.Println("-d appName #to delete the app (installed by this tool)")

}

func listing() error {
config := setConfig("")
m := manager.New(config)
entries := m.List()
fmt.Println("Apps:")
for _, entry := range entries {
fmt.Println(entry.Category("Desktop Entry").Get("Name"))
}
return nil
}

func deleteApp(appName string) error {
config := setConfig("")
m := manager.New(config)
return m.Delete(appName)
}

func chooseScript() error {
if os.Args[1] == "-l" {
return listing()
}
if os.Args[1] == "-d" && len(os.Args) >= 3 {
return deleteApp(os.Args[2])
}
if len(os.Args) == 2 {
return runInstallScript(os.Args[1])
}
help()
return nil
}

func main() {

if len(os.Args) != 2 || os.Args[1] == "-h" {
help()
os.Exit(0)
}
if os.Args[1] == "-v" {
fmt.Println("0.6")
os.Exit(0)
}
err := chooseScript()
if err != nil {
fmt.Println(err)
}
}
110 changes: 110 additions & 0 deletions manager/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package manager

import (
"appImageInstaller/desktopFile"
"appImageInstaller/structs"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)

type Manager struct {
config structs.Config
}

func New(config structs.Config) *Manager {
manager := new(Manager)
manager.config = config
return manager
}

func (m *Manager) IsGeneratedDesktop(deskFile *desktopFile.DesktopFile) (bool, error) {
isGood, err := m.IsValidDesktop(deskFile)
if !isGood {
return isGood, err
}
execPath, _ := deskFile.Category("Desktop Entry").Get("Exec")
if !strings.Contains(execPath, m.config.ExecDir) {
return false, fmt.Errorf("external exec path")
}
return true, nil
}

func (m *Manager) IsValidDesktop(deskFile *desktopFile.DesktopFile) (bool, error) {
if !deskFile.HasValues("Desktop Entry", []string{"Get", "Name", "Exec"}) {
return false, fmt.Errorf("missing basic values")
}
path, _ := deskFile.Category("Desktop Entry").Get("Exec")
_, err := os.Stat(path)
if err != nil {
return false, fmt.Errorf("fail to find binary")
}
return true, nil
}

func (m *Manager) List() []*desktopFile.DesktopFile {
var appList []*desktopFile.DesktopFile
entries, err := os.ReadDir(m.config.GnomeDesktopDir)
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
deskFile := desktopFile.New()
deskFilePath := filepath.Join(m.config.GnomeDesktopDir, e.Name())
err := deskFile.FromFile(deskFilePath)
if err != nil {
fmt.Println("Listing Error: ", "file: ", deskFilePath, ":", err)
continue
}
_, err = m.IsValidDesktop(deskFile)
if err != nil {
fmt.Println("Listing Error for file:", deskFilePath, " ", err)
continue
}
isGenerated, err := m.IsGeneratedDesktop(deskFile)
if !isGenerated {
continue
}
appList = append(appList, deskFile)
}
return appList
}

func (m *Manager) Delete(appName string) error {
entries, err := os.ReadDir(m.config.GnomeDesktopDir)
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
deskFile := desktopFile.New()
deskFilePath := filepath.Join(m.config.GnomeDesktopDir, e.Name())
err := deskFile.FromFile(deskFilePath)
if err != nil {
fmt.Println("Listing Error: ", "file: ", deskFilePath, ":", err)
continue
}
isGenerated, err := m.IsGeneratedDesktop(deskFile)
if !isGenerated {
continue
}
ExecPath, _ := deskFile.Category("Desktop Entry").Get("Exec")
name, _ := deskFile.Category("Desktop Entry").Get("Name")
if name != appName {
continue
}
err = os.Remove(ExecPath)
if err != nil {
return err
}
err = os.Remove(deskFilePath)
if err != nil {
return err
}
fmt.Println("removed binary file", ExecPath)
fmt.Println("removed Desktop file", deskFilePath)
return nil
}
return fmt.Errorf("App not found")
}

0 comments on commit d8491c8

Please sign in to comment.