Browse Source

Add un/install

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main v0.0.2
jolheiser 1 month ago
parent
commit
f61c665ff2
Signed by: jolheiser GPG Key ID: B853ADA5DA7BBF7A
  1. 28
      gadgets.go
  2. 70
      install.go
  3. 55
      main.go

28
gadgets.go

@ -0,0 +1,28 @@
package main
import (
"os"
"path/filepath"
"strings"
)
func listGadgets() []string {
set := make(map[string]struct{})
path := os.Getenv("PATH")
for _, dir := range filepath.SplitList(path) {
entries, err := os.ReadDir(dir)
if err != nil {
continue
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasPrefix(entry.Name(), "go-") {
set[entry.Name()] = struct{}{}
}
}
}
s := make([]string, 0, len(set))
for k := range set {
s = append(s, k)
}
return s
}

70
install.go

@ -0,0 +1,70 @@
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func install() error {
if isInstalled() {
return errors.New("gogo is already installed as go")
}
original, err := exec.LookPath("go")
if err != nil {
return err
}
this, err := os.Executable()
if err != nil {
return err
}
if err := os.Rename(original, filepath.Join(filepath.Dir(original), "_" + filepath.Base(original))); err != nil {
return err
}
if err := os.Rename(this, original); err != nil {
return err
}
fmt.Println("gogo installed as go")
return nil
}
func uninstall() error {
if !isInstalled() {
return errors.New("gogo is not installed as go")
}
this, err := os.Executable()
if err != nil {
return err
}
original := filepath.Join(filepath.Dir(this), "_" + filepath.Base(this))
if err := os.Remove(this); err != nil {
return err
}
if err := os.Rename(original, filepath.Join(filepath.Dir(original), filepath.Base(original)[1:])); err != nil {
return err
}
fmt.Println("gogo uninstalled as go")
return nil
}
func isInstalled() bool {
cmd := exec.Command("go", "--gadget")
out, err := cmd.Output()
if err != nil {
return false
}
return strings.EqualFold(string(out), goGoGadget)
}

55
main.go

@ -1,26 +1,69 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
)
const goGoGadget = "https://en.wikipedia.org/wiki/Inspector_Gadget"
func main() {
fs := flag.NewFlagSet("gogo", flag.ContinueOnError)
listFlag := fs.Bool("list", false, "list gadgets")
installFlag := fs.Bool("install", false, "install gogo as go")
uninstallFlag := fs.Bool("uninstall", false, "uninstall gogo as go")
gadgetFlag := fs.Bool("gadget", false, "go go gadget!")
fs.Usage = func() {}
fs.Parse(os.Args[1:])
if *gadgetFlag {
fmt.Print(goGoGadget)
return
}
if *listFlag {
fmt.Println("Available Gadgets:")
for _, gadget := range listGadgets() {
fmt.Println("\t", gadget)
}
return
}
if *installFlag {
if err := install(); err != nil {
fmt.Println(err)
}
return
}
if *uninstallFlag {
if err := uninstall(); err != nil {
fmt.Println(err)
}
return
}
// Run Go
binPath, err := exec.LookPath("go")
if err != nil {
log.Println("go cannot be found in PATH")
return
}
args := os.Args
if len(args) > 1 {
args = args[1:]
if isInstalled() {
binPath = filepath.Join(filepath.Dir(binPath), "_" + filepath.Base(binPath))
}
args := os.Args[1:]
if len(args) > 0 {
if extPath, err := exec.LookPath("go-" + args[0]); err == nil {
args = args[1:]
binPath = extPath
if len(args) > 1 {
args = args[1:]
}
}
}

Loading…
Cancel
Save