Simplify Go installation and support standard OpenCode releases
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package installation
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"debug/buildinfo"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Uninstall removes only verified binaries from the current command's Go bin directory.
|
||||
// Data is deliberately retained: custom server database paths cannot be safely inferred.
|
||||
func Uninstall(executable string, args []string, in io.Reader, out io.Writer) error {
|
||||
f := flag.NewFlagSet("uninstall", flag.ContinueOnError)
|
||||
f.SetOutput(out)
|
||||
yes := f.Bool("yes", false, "Skip confirmation")
|
||||
if err := f.Parse(args); err != nil {
|
||||
if err == flag.ErrHelp {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if len(f.Args()) > 0 {
|
||||
return fmt.Errorf("uninstall takes no positional arguments")
|
||||
}
|
||||
resolved, err := filepath.EvalSymlinks(executable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dir := filepath.Dir(resolved)
|
||||
var files []string
|
||||
for _, command := range []string{"pipelines", "pipelines-server", "pipelines-tui"} {
|
||||
path := filepath.Join(dir, command)
|
||||
info, err := buildinfo.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot verify %s; no binaries removed: %w", path, err)
|
||||
}
|
||||
if info.Path != "git.a-wan.net/alan/pipelines/cmd/"+command {
|
||||
return fmt.Errorf("%s is not a Pipelines Go binary; no binaries removed", path)
|
||||
}
|
||||
files = append(files, path)
|
||||
}
|
||||
if !*yes {
|
||||
fmt.Fprintf(out, "Remove %s? Data and drafts are retained. [y/N] ", strings.Join(files, ", "))
|
||||
answer, err := bufio.NewReader(in).ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(strings.ToLower(answer)) != "y" {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
for _, path := range files {
|
||||
if err := os.Remove(path); err != nil {
|
||||
return fmt.Errorf("remove %s: %w (use the permissions appropriate to your bin directory)", path, err)
|
||||
}
|
||||
}
|
||||
fmt.Fprintln(out, "Removed Go binaries. Pipeline history, drafts, and OpenCode configuration were retained.")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user