|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/DefangLabs/defang/src/pkg/term" |
| 11 | +) |
| 12 | + |
| 13 | +func Upgrade(ctx context.Context) error { |
| 14 | + // Find path to the current executable to determine how to upgrade |
| 15 | + ex, err := os.Executable() |
| 16 | + if err != nil { |
| 17 | + return err |
| 18 | + } |
| 19 | + term.Debugf(" - Executable: %s\n", ex) |
| 20 | + |
| 21 | + ex, err = filepath.EvalSymlinks(ex) |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + term.Debugf(" - Evaluated: %s\n", ex) |
| 26 | + |
| 27 | + if strings.HasPrefix(ex, homebrewPrefix(ctx)) { |
| 28 | + printInstructions("brew upgrade defang") |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + if strings.HasPrefix(ex, "/nix/store/") { |
| 33 | + // Detect whether the user has used Flakes or nix-env |
| 34 | + if strings.Contains("-defang-cli-", ex) { |
| 35 | + printInstructions("nix-env -if https://github.com/DefangLabs/defang/archive/main.tar.gz") |
| 36 | + } else { |
| 37 | + printInstructions("nix profile install github:DefangLabs/defang#defang-bin --refresh") |
| 38 | + } |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + // Check if we're running in PowerShell |
| 43 | + if _, exists := os.LookupEnv("PSModulePath"); exists { |
| 44 | + printInstructions(`pwsh -c "iwr https://s.defang.io/defang_win_amd64.zip -OutFile defang.zip"`) |
| 45 | + |
| 46 | + return nil |
| 47 | + } |
| 48 | + |
| 49 | + // Default to the shell script |
| 50 | + printInstructions(`. <(curl -Ls https://s.defang.io/install)`) |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func homebrewPrefix(ctx context.Context) string { |
| 56 | + output, err := exec.CommandContext(ctx, "brew", "config").Output() |
| 57 | + if err != nil { |
| 58 | + return "" |
| 59 | + } |
| 60 | + homebrewPrefix := "" |
| 61 | + // filter out the line which includes HOMEBREW_PREFIX |
| 62 | + for _, line := range strings.Split(string(output), "\n") { |
| 63 | + config_key := "HOMEBREW_PREFIX: " |
| 64 | + if strings.HasPrefix(line, config_key) { |
| 65 | + // remove the prefix from the line |
| 66 | + homebrewPrefix = strings.TrimPrefix(line, config_key) |
| 67 | + break |
| 68 | + } |
| 69 | + } |
| 70 | + return homebrewPrefix |
| 71 | +} |
| 72 | + |
| 73 | +func printInstructions(cmd string) { |
| 74 | + term.Info("To upgrade defang, run the following command:\n\n", cmd) |
| 75 | +} |
0 commit comments