jitty-scripts/flatface.go

185 lines
4.0 KiB
Go
Raw Normal View History

2018-01-02 02:49:14 -07:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
)
func input(s string) (string, error) {
stdinBuf := bufio.NewReader(os.Stdin)
2018-01-02 02:49:14 -07:00
fmt.Print(s)
text, err := stdinBuf.ReadString('\n')
2018-01-02 02:49:14 -07:00
if err != nil {
panic(err)
}
return text[:len(text)-1], err
}
func searchRepo() {
print("\n| Search Repositories |\n\n")
print("Enter a search term and copy the namespace to clipboard\n")
print("Paste this later to the install menu.\n\n")
print("Type: 'm' for main menu\n")
print("Type: 'q' to quit the application\n\n")
var programName, _ = input(">> ")
if programName == "m" {
return
} else if programName == "q" {
os.Exit(0)
} else {
cmd := exec.Command("flatpak", "search", programName)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Print("\n", "Results for ", programName, ": \n\n", string(out), "\n")
searchRepo()
2018-01-02 02:49:14 -07:00
}
}
func installPak() {
print("\n| Install Menu |\n\n")
print("To Use: Copy paste a flatpak namespace from search results\n\n")
print("EXAMPLE: 'com.play0ad.zeroad' - installs the Game '0ad'\n\n")
print("Type: 'm' for main menu\n")
print("Type: 'q' to quit the application\n\n")
print("NOTE: install can take several seconds and appear to hang. Please be patient...\n\n")
var programName, _ = input(">> ")
if programName == "m" {
return
} else if programName == "q" {
os.Exit(0)
} else {
cmd := exec.Command("flatpak", "install", "flathub", programName)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Print(string(out), "\n")
installPak()
2018-01-02 02:49:14 -07:00
}
}
func listPak() {
print("\n| List of installed Flatpaks |\n\n")
cmd := exec.Command("flatpak", "list")
2018-01-02 02:49:14 -07:00
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Print(string(out), "\n\n")
2018-01-02 02:49:14 -07:00
}
func removePak() {
print("\n| Remove Flatpaks |\n\n")
listPak()
print("\n\nCopy and paste a namespace above and press enter to remove from system\n\n")
print("Type: 'm' for main menu\n")
print("Type: 'q' to quit the application\n\n")
var programName, _ = input(">> ")
if programName == "m" {
return
} else if programName == "q" {
os.Exit(0)
} else {
cmd := exec.Command("flatpak", "uninstall", programName)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Print(string(out), "\n\n")
removePak()
2018-01-02 02:49:14 -07:00
}
}
//// TODO
2018-01-02 02:49:14 -07:00
/*
func optionsMenu() {
var choice string
print("\n| FlatFace Options |\n\n")
print("\n1. Add Repository\n\n")
choice, _ = input(">> ")
if choice == "1" {
}
}
*/
func syncRepo() {
print("Syncing repo data...\n")
2018-01-02 03:32:32 -07:00
cmd := exec.Command("flatpak", "update", "--appstream")
2018-01-02 02:49:14 -07:00
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Print(string(out))
2018-01-02 02:49:14 -07:00
}
func checkRepo() {
print("\nAdding flathub repo if needed...\n")
2018-01-02 02:49:14 -07:00
cmd := exec.Command("flatpak", "remote-add", "--if-not-exists", "flathub", "https://flathub.org/repo/flathub.flatpakrepo")
out, err := cmd.CombinedOutput()
syncRepo()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Print(string(out))
}
func updatePak() {
print("Checking for app upgrades..\n")
cmd := exec.Command("flatpak", "update")
out, err := cmd.CombinedOutput()
2018-01-02 02:49:14 -07:00
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Print(string(out))
2018-01-02 02:49:14 -07:00
}
func mainMenu() {
print("\n\n| FlatFace v0.0.1a-3 |\n\n")
2018-01-02 02:49:14 -07:00
var choice string
print("[S]earch remote for flatpaks\n")
print("[I]nstall flatpaks from remote\n")
print("[R]emove local flatpaks\n")
print("[L]ist install flatpaks\n")
print("[U]pdate\n")
print("[Q]uit\n\n")
2018-01-02 02:49:14 -07:00
choice, _ = input(">> ")
if choice == "s" {
2018-01-02 02:49:14 -07:00
searchRepo()
} else if choice == "i" {
2018-01-02 02:49:14 -07:00
installPak()
} else if choice == "r" {
2018-01-02 02:49:14 -07:00
removePak()
} else if choice == "l" {
2018-01-02 02:49:14 -07:00
listPak()
} else if choice == "u" {
2018-01-02 02:49:14 -07:00
updatePak()
} else if choice == "q" {
os.Exit(0)
2018-01-02 02:49:14 -07:00
} else {
print("Unknown option")
}
}
func main() {
checkRepo()
2018-01-02 02:49:14 -07:00
for {
mainMenu()
}
}