/* open --- Super simple alternative to xdg-open Copyright (c) 2022 Sameer Rahmani This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package main import ( "bufio" "fmt" "io/ioutil" "log" "os" "os/exec" "strings" "github.com/gabriel-vasile/mimetype" ) func main() { if len(os.Args) != 2 { log.Fatal("'open' get's exactly one argument") } file := os.Args[1] home := os.Getenv("HOME") var prog string mtype, err := mimetype.DetectFile(file) if err != nil { log.Fatalf("%v", err) } dirpath := home + "/.config/open/" os.MkdirAll(dirpath, 0775) opener_file := dirpath + mtype.Extension() if _, err := os.Stat(opener_file); err == nil { content, err := ioutil.ReadFile(opener_file) if err != nil { log.Fatalf("%v", err) } prog = strings.TrimSuffix(string(content), "\n") } else { fmt.Printf("Don't know how to open '%s' file. What program should I use? ", mtype.Extension()) reader := bufio.NewReader(os.Stdin) prog_string, err := reader.ReadString('\n') if err != nil { log.Fatal(err) } prog_string = string(prog_string[0 : len(prog_string)-1]) prog = strings.Clone(prog_string) s := []byte(prog_string) err = ioutil.WriteFile(opener_file, s, 0644) if err != nil { log.Fatal(err) } } command := strings.Split(prog, " ") command = append(command, file) fmt.Printf("Running command: '%s'\n", command) out, err := exec.Command(command[0], command[1:]...).Output() if err != nil { log.Fatal(err) } fmt.Println(string(out)) }