(ctx context.Context)
| 65 | } |
| 66 | |
| 67 | func update(ctx context.Context) error { |
| 68 | // Download the binary |
| 69 | downloadData, err := GetDownloadData(ctx) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | if downloadData.Version == "v0."+lib.Version { |
| 74 | fmt.Printf("Latest version (v0.%s) is already installed\n", lib.Version) |
| 75 | return nil |
| 76 | } |
| 77 | err = downloadFiles(downloadData) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | // Verify the SLSA attestation |
| 83 | var slsaError error |
| 84 | if runtime.GOOS == "darwin" { |
| 85 | slsaError = verifyBinaryMac(ctx, getTmpClientPath(), downloadData) |
| 86 | } else { |
| 87 | slsaError = lib.VerifyBinary(ctx, getTmpClientPath(), getTmpClientPath()+".intoto.jsonl", getPossiblyOverriddenVersion(downloadData)) |
| 88 | } |
| 89 | if slsaError != nil { |
| 90 | err = lib.HandleSlsaFailure(slsaError) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Unlink the existing binary so we can overwrite it even though it is still running |
| 97 | if runtime.GOOS == "linux" { |
| 98 | homedir := hctx.GetHome(ctx) |
| 99 | err = syscall.Unlink(path.Join(homedir, data.GetHishtoryPath(), "hishtory")) |
| 100 | if err != nil { |
| 101 | return fmt.Errorf("failed to unlink %s for update: %w", path.Join(homedir, data.GetHishtoryPath(), "hishtory"), err) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Install the new one |
| 106 | cmd := exec.Command("chmod", "+x", getTmpClientPath()) |
| 107 | var stdout bytes.Buffer |
| 108 | cmd.Stdout = &stdout |
| 109 | var stderr bytes.Buffer |
| 110 | cmd.Stderr = &stderr |
| 111 | err = cmd.Run() |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("failed to chmod +x the update (stdout=%#v, stderr=%#v): %w", stdout.String(), stderr.String(), err) |
| 114 | } |
| 115 | cmd = exec.Command(getTmpClientPath(), "install", "--skip-update-config-modification", "--currently-installed-version", "v0."+lib.Version) |
| 116 | cmd.Stdout = os.Stdout |
| 117 | stderr = bytes.Buffer{} |
| 118 | cmd.Stdin = os.Stdin |
| 119 | err = cmd.Run() |
| 120 | if err != nil { |
| 121 | return fmt.Errorf("failed to install update (stderr=%#v), is %s in a noexec directory? (if so, set the TMPDIR environment variable): %w", stderr.String(), getTmpClientPath(), err) |
| 122 | } |
| 123 | fmt.Printf("Successfully updated hishtory from v0.%s to %s\n", lib.Version, getPossiblyOverriddenVersion(downloadData)) |
| 124 |
no test coverage detected