amfora/amfora.go

130 lines
2.8 KiB
Go
Raw Normal View History

2020-06-18 23:54:48 +03:00
package main
import (
"fmt"
"io"
2020-06-18 23:54:48 +03:00
"os"
"path/filepath"
"strings"
2020-06-18 23:54:48 +03:00
2021-02-27 08:13:11 +03:00
"github.com/makeworld-the-better-one/amfora/bookmarks"
"github.com/makeworld-the-better-one/amfora/client"
2020-06-18 23:54:48 +03:00
"github.com/makeworld-the-better-one/amfora/config"
"github.com/makeworld-the-better-one/amfora/display"
"github.com/makeworld-the-better-one/amfora/logger"
2020-11-28 01:01:29 +03:00
"github.com/makeworld-the-better-one/amfora/subscriptions"
2020-06-18 23:54:48 +03:00
)
2020-09-02 02:55:06 +03:00
var (
version = "v1.9.2"
2020-09-02 02:55:06 +03:00
commit = "unknown"
builtBy = "unknown"
)
2020-06-18 23:54:48 +03:00
func main() {
log, err := logger.GetLogger()
if err != nil {
panic(err)
}
debugModeEnabled := os.Getenv("AMFORA_DEBUG") == "1"
if debugModeEnabled {
log.Println("Debug mode enabled")
}
2020-06-18 23:54:48 +03:00
if len(os.Args) > 1 {
if os.Args[1] == "--version" || os.Args[1] == "-v" {
2020-09-02 02:55:06 +03:00
fmt.Println("Amfora", version)
fmt.Println("Commit:", commit)
fmt.Println("Built by:", builtBy)
return
}
if os.Args[1] == "--help" || os.Args[1] == "-h" {
2020-07-10 02:28:39 +03:00
fmt.Println("Amfora is a fancy terminal browser for the Gemini protocol.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println("amfora [URL]")
fmt.Println("amfora --version, -v")
return
}
}
err = config.Init()
2020-06-18 23:54:48 +03:00
if err != nil {
fmt.Fprintf(os.Stderr, "Config error: %v\n", err)
os.Exit(1)
}
err = client.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "Client error: %v\n", err)
os.Exit(1)
}
2021-02-27 08:13:11 +03:00
2020-11-28 01:01:29 +03:00
err = subscriptions.Init()
if err != nil {
2020-11-28 01:01:29 +03:00
fmt.Fprintf(os.Stderr, "subscriptions.json error: %v\n", err)
2020-07-10 02:28:39 +03:00
os.Exit(1)
2020-06-18 23:54:48 +03:00
}
2021-02-27 08:13:11 +03:00
err = bookmarks.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "bookmarks.xml error: %v\n", err)
os.Exit(1)
}
// Initialize lower-level cview app
if err = display.App.Init(); err != nil {
panic(err)
}
// Initialize Amfora's settings
2020-12-21 00:39:33 +03:00
display.Init(version, commit, builtBy)
// Load a URL, file, or render from stdin
if len(os.Args[1:]) > 0 {
url := os.Args[1]
if !strings.Contains(url, "://") || strings.HasPrefix(url, "../") || strings.HasPrefix(url, "./") {
fileName := url
if _, err := os.Stat(fileName); err == nil {
if !strings.HasPrefix(fileName, "/") {
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting working directory path: %v\n", err)
os.Exit(1)
}
fileName = filepath.Join(cwd, fileName)
}
url = "file://" + fileName
}
}
display.NewTabWithURL(url)
} else if !isStdinEmpty() {
2021-12-09 02:02:43 +03:00
display.NewTab()
renderFromStdin()
} else {
display.NewTab()
2020-06-18 23:54:48 +03:00
}
// Start
2020-06-18 23:54:48 +03:00
if err = display.App.Run(); err != nil {
panic(err)
}
}
func isStdinEmpty() bool {
stat, _ := os.Stdin.Stat()
return (stat.Mode() & os.ModeCharDevice) != 0
}
func renderFromStdin() {
stdinTextBuilder := new(strings.Builder)
_, err := io.Copy(stdinTextBuilder, os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading from standard input: %v\n", err)
os.Exit(1)
}
stdinText := stdinTextBuilder.String()
display.RenderFromString(stdinText)
}