Debian Codegraph @

internal/pkg/selfupdate/update.go

raw symbols SCIP
1 // fork from https://github.com/superfly/flyctl/blob/master/internal/update/update.go
2
3 package selfupdate
4
5 import (
6 "context"
7 "io"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "strings"
13
14 "github.com/cli/safeexec"
15 )
16
17 func UpgradeInPlace(ctx context.Context, stdout, stderr io.Writer, stdin io.Reader) error {
18 if runtime.GOOS == "windows" {
19 if err := renameCurrentBinaries(); err != nil {
20 return err
21 }
22 }
23
24 shellToUse, ok := os.LookupEnv("SHELL")
25 switchToUse := "-c"
26
27 if !ok {
28 if runtime.GOOS == "windows" {
29 shellToUse = "powershell.exe"
30 switchToUse = "-Command"
31 } else {
32 shellToUse = "/bin/bash"
33 }
34 }
35
36 command := updateCommand()
37
38 cmd := exec.Command(shellToUse, switchToUse, command)
39 cmd.Stdout = stdout
40 cmd.Stderr = stderr
41 cmd.Stdin = stdin
42 return cmd.Run()
43 }
44
45 func isUnderHomebrew() bool {
46 binary, err := os.Executable()
47 if err != nil {
48 return false
49 }
50
51 brewExe, err := safeexec.LookPath("brew")
52 if err != nil {
53 return false
54 }
55
56 brewPrefixBytes, err := exec.Command(brewExe, "--prefix").Output()
57 if err != nil {
58 return false
59 }
60
61 brewBinPrefix := filepath.Join(strings.TrimSpace(string(brewPrefixBytes)), "bin") + string(filepath.Separator)
62 return strings.HasPrefix(binary, brewBinPrefix)
63 }
64
65 func updateCommand() string {
66 if isUnderHomebrew() {
67 return "brew upgrade humanlog"
68 }
69
70 if runtime.GOOS == "windows" {
71 return "iwr https://humanlog.io/install.ps1 -useb | iex"
72 } else {
73 return `curl -L "https://humanlog.io/install.sh" | sh`
74 }
75 }
76
77 // can't replace binary on windows, need to move
78 func renameCurrentBinaries() error {
79 binaries, err := currentWindowsBinaries()
80 if err != nil {
81 return err
82 }
83
84 for _, p := range binaries {
85 if err := os.Rename(p, p+".old"); err != nil {
86 return err
87 }
88 }
89
90 return nil
91 }
92
93 func currentWindowsBinaries() ([]string, error) {
94 binaryPath, err := os.Executable()
95 if err != nil {
96 return nil, err
97 }
98
99 return []string{binaryPath}, nil
100 }