add install commands
This commit is contained in:
71
main.go
71
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"luxtools-client/internal/installer"
|
||||
"luxtools-client/internal/openfolder"
|
||||
)
|
||||
|
||||
@@ -42,6 +44,21 @@ func main() {
|
||||
infoLog := log.New(os.Stdout, "", log.LstdFlags)
|
||||
errLog := log.New(os.Stderr, "ERROR: ", log.LstdFlags)
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
switch os.Args[1] {
|
||||
case "install":
|
||||
if err := runInstall(os.Args[2:], infoLog, errLog); err != nil {
|
||||
errLog.Fatal(err)
|
||||
}
|
||||
return
|
||||
case "uninstall":
|
||||
if err := runUninstall(os.Args[2:], infoLog, errLog); err != nil {
|
||||
errLog.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
listen := flag.String("listen", "127.0.0.1:8765", "listen address (host:port), should be loopback")
|
||||
var allowed allowList
|
||||
flag.Var(&allowed, "allow", "allowed path prefix (repeatable); if none, any path is allowed")
|
||||
@@ -131,6 +148,60 @@ func main() {
|
||||
errLog.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
|
||||
func runInstall(args []string, infoLog, errLog *log.Logger) error {
|
||||
fs := flag.NewFlagSet("install", flag.ContinueOnError)
|
||||
fs.SetOutput(io.Discard)
|
||||
|
||||
listen := fs.String("listen", "127.0.0.1:8765", "listen address (host:port), should be loopback")
|
||||
dryRun := fs.Bool("dry-run", false, "print/validate only; do not write files or register services")
|
||||
var allowed allowList
|
||||
fs.Var(&allowed, "allow", "allowed path prefix (repeatable); if none, any path is allowed")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return fmt.Errorf("install: %w", err)
|
||||
}
|
||||
|
||||
if !installer.Supported() {
|
||||
return fmt.Errorf("install: unsupported OS: %s", runtime.GOOS)
|
||||
}
|
||||
if !isLoopbackListenAddr(*listen) {
|
||||
return fmt.Errorf("install: refusing non-loopback listen address: %s", *listen)
|
||||
}
|
||||
|
||||
if err := installer.Install(installer.InstallOptions{Listen: *listen, Allow: []string(allowed), DryRun: *dryRun}); err != nil {
|
||||
return err
|
||||
}
|
||||
if *dryRun {
|
||||
infoLog.Printf("install dry-run OK")
|
||||
return nil
|
||||
}
|
||||
infoLog.Printf("installed %s", installer.ServiceName)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runUninstall(args []string, infoLog, errLog *log.Logger) error {
|
||||
fs := flag.NewFlagSet("uninstall", flag.ContinueOnError)
|
||||
fs.SetOutput(io.Discard)
|
||||
|
||||
keepConfig := fs.Bool("keep-config", false, "keep config on disk")
|
||||
dryRun := fs.Bool("dry-run", false, "print/validate only; do not remove files or unregister services")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return fmt.Errorf("uninstall: %w", err)
|
||||
}
|
||||
|
||||
if !installer.Supported() {
|
||||
return fmt.Errorf("uninstall: unsupported OS: %s", runtime.GOOS)
|
||||
}
|
||||
if err := installer.Uninstall(installer.UninstallOptions{KeepConfig: *keepConfig, DryRun: *dryRun}); err != nil {
|
||||
return err
|
||||
}
|
||||
if *dryRun {
|
||||
infoLog.Printf("uninstall dry-run OK")
|
||||
return nil
|
||||
}
|
||||
infoLog.Printf("uninstalled %s", installer.ServiceName)
|
||||
return nil
|
||||
}
|
||||
|
||||
func isLoopbackListenAddr(addr string) bool {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user