86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package app
|
|
|
|
import (
|
|
"image"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Parameters to run the pwa-launcher app.
|
|
type Parameters struct {
|
|
Title string
|
|
Icon string
|
|
iconData image.Image
|
|
URL string
|
|
Exec string
|
|
}
|
|
|
|
// Run the main app logic.
|
|
func Run(p Parameters) error {
|
|
// If no title or icon given, parse the site.
|
|
if p.Title == "" || p.Icon == "" {
|
|
insights, err := Parse(p.URL)
|
|
if err != nil {
|
|
log.Printf("Insights error: %s", err)
|
|
} else {
|
|
log.Printf("insights: %+v", insights)
|
|
if p.Title == "" && insights.Title != "" {
|
|
p.Title = insights.Title
|
|
}
|
|
}
|
|
|
|
if icons, err := DetectIcons(p.URL); err == nil {
|
|
log.Printf("detected icons: %+v", icons)
|
|
insights.Icons = append(insights.Icons, icons...)
|
|
} else {
|
|
log.Printf("DetectIcons error: %s", err)
|
|
}
|
|
|
|
log.Printf("Final insights: %+v", insights)
|
|
|
|
// Select the best icon.
|
|
if p.Icon == "" {
|
|
icon, err := BestIcon(insights)
|
|
if err != nil {
|
|
panic("No suitable app icon found, provide one manually with -icon")
|
|
}
|
|
p.iconData = icon
|
|
}
|
|
}
|
|
|
|
// Do we need to get or download an icon?
|
|
if p.iconData == nil && p.Icon != "" {
|
|
if strings.HasPrefix(p.Icon, "http:") || strings.HasPrefix(p.Icon, "https:") {
|
|
// Download an icon from the web.
|
|
png, err := ParseWebPNG(p.Icon)
|
|
if err != nil {
|
|
log.Fatalf("Couldn't download -icon from %s: %s", p.Icon, err)
|
|
}
|
|
p.iconData = png
|
|
} else {
|
|
fh, err := os.Open(p.Icon)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
png, err := ParsePNG(fh)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
p.iconData = png
|
|
fh.Close()
|
|
}
|
|
}
|
|
|
|
// Missing a title?
|
|
if p.Title == "" {
|
|
panic("Couldn't detect a page title, provide one with the -title option")
|
|
}
|
|
|
|
// Install the icon and launcher.
|
|
Install(p)
|
|
log.Printf("Launcher installed successfully.")
|
|
|
|
return nil
|
|
}
|