Last active
August 29, 2015 14:06
-
-
Save robertely/c8af0b6553a0fe65e7d3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"log" | |
"regexp" | |
"strings" | |
"os/user" | |
"os/exec" | |
"io/ioutil" | |
"github.com/vaughan0/go-ini" | |
) | |
type Application struct { | |
Type string | |
Version string | |
Name string | |
GenericName string | |
Comment string | |
Icon string | |
TryExec string | |
Exec string | |
Path string | |
StartupWMClass string | |
URL string | |
DesktopfilePath string | |
NoDisplay bool | |
StartupNotify bool | |
Hidden bool | |
DBusActivatable bool | |
Terminal bool | |
OnlyShowIn []string | |
NotShowIn []string | |
Actions []string | |
MimeType []string | |
Categories []string | |
Implements []string | |
Keywords []string | |
} | |
func SearchAppByName(pattern string) []Application{ | |
results := []Application{} | |
for _, app := range GetAppList(){ | |
match, _ := regexp.MatchString(pattern, strings.ToLower(app.Name)) | |
if match { | |
results = append(results, app) | |
} | |
} | |
return results | |
} | |
func SearchAppByExec(pattern string) []Application{ | |
results := []Application{} | |
for _, app := range GetAppList(){ | |
match, _ := regexp.MatchString(pattern, strings.ToLower(app.Exec)) | |
if match { | |
results = append(results, app) | |
} | |
} | |
return results | |
} | |
func LaunchAppByName(name string){ | |
cmd := strings.Split(GetAppByName(name).Exec, " ")[0] | |
Launch(cmd) | |
} | |
func Launch(command string){ | |
cmd := exec.Command(command) | |
err := cmd.Start() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func GetAppByName(name string) Application{ | |
for _, app := range GetAppList(){ | |
if app.Name == name{ | |
return app | |
} | |
} | |
return Application{} | |
} | |
func GetAppList() []Application{ | |
files := getFileList(getPaths()) | |
applist := []Application{} | |
for _, i := range files { | |
file, err := ini.LoadFile(i) | |
if err != nil { | |
log.Fatal(err) | |
} | |
app := Application{} | |
app.DesktopfilePath = i | |
app.Type = file["Desktop Entry"]["Type"] | |
app.Version = file["Desktop Entry"]["Version"] | |
app.Name = file["Desktop Entry"]["Name"] | |
app.GenericName = file["Desktop Entry"]["GenericName"] | |
app.Comment = file["Desktop Entry"]["Comment"] | |
app.Icon = file["Desktop Entry"]["Icon"] | |
app.TryExec = file["Desktop Entry"]["TryExec"] | |
app.Exec = file["Desktop Entry"]["Exec"] | |
app.Path = file["Desktop Entry"]["Path"] | |
app.StartupWMClass = file["Desktop Entry"]["StartupWMClass"] | |
app.URL = file["Desktop Entry"]["URL"] | |
app.DesktopfilePath = file["Desktop Entry"]["DesktopfilePath"] | |
app.NoDisplay = Stob(file["Desktop Entry"]["NoDisplay"]) | |
app.StartupNotify = Stob(file["Desktop Entry"]["StartupNotify"]) | |
app.Hidden = Stob(file["Desktop Entry"]["Hidden"]) | |
app.DBusActivatable = Stob(file["Desktop Entry"]["DBusActivatable"]) | |
app.Terminal = Stob(file["Desktop Entry"]["Terminal"]) | |
app.OnlyShowIn = Stosl(file["Desktop Entry"]["OnlyShowIn"]) | |
app.NotShowIn = Stosl(file["Desktop Entry"]["NotShowIn"]) | |
app.Actions = Stosl(file["Desktop Entry"]["Actions"]) | |
app.MimeType = Stosl(file["Desktop Entry"]["MimeType"]) | |
app.Categories = Stosl(file["Desktop Entry"]["Categories"]) | |
app.Implements = Stosl(file["Desktop Entry"]["Implements"]) | |
app.Keywords = Stosl(file["Desktop Entry"]["Keywords"]) | |
applist = append(applist, app) | |
} | |
return applist | |
} | |
func getPaths() []string{ | |
usr, _ := user.Current() | |
homedir := usr.HomeDir | |
return []string{"/usr/share/applications/", homedir + "/.local/share/applications/"} | |
} | |
func getFileList(paths []string) []string{ | |
var files = make([]string, 0) | |
for path := range paths { | |
filelist, err := ioutil.ReadDir(paths[path]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, f := range filelist { | |
files = append(files, paths[path]+f.Name()) | |
} | |
} | |
return files | |
} | |
func Stosl(s string)[]string{ | |
return strings.Split(s, ";") | |
} | |
func Stob(s string) bool { | |
if s == "true"{ | |
return true | |
} | |
return false | |
} | |
func main() { | |
// Search by pattern | |
pattern := "goog" | |
fmt.Printf("%+v\n", SearchAppByName(pattern)) | |
// get application object by name | |
fmt.Printf("%+v\n", GetAppByName("Google Chrome")) | |
// launch by name | |
LaunchAppByName("Google Chrome") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment