newUpdateCmd implements `snix update`: fetches the latest GitHub release, verifies its SHA-256 against the published checksum file, and atomically swaps the running binary for the new one. Minisign verification is deliberately OFF by default — users install via the one-liner installer which has a p
(g *globalFlags)
| 27 | // the one-liner installer which has a pinned pubkey. Power users can pass |
| 28 | // --verify=minisign once they have a trusted pubkey on disk. |
| 29 | func newUpdateCmd(g *globalFlags) *cobra.Command { |
| 30 | var ( |
| 31 | dryRun bool |
| 32 | toVer string |
| 33 | ) |
| 34 | cmd := &cobra.Command{ |
| 35 | Use: "update", |
| 36 | Short: "Upgrade snix to the latest GitHub release", |
| 37 | Long: `Downloads the latest release from github.com/SamNet-dev/snix, |
| 38 | verifies its SHA-256, and atomically replaces the current binary. |
| 39 | |
| 40 | If --to is passed, installs that specific version instead of latest. |
| 41 | Use --dry-run to see what would happen without changing anything.`, |
| 42 | RunE: func(cmd *cobra.Command, args []string) error { |
| 43 | out := cmd.OutOrStdout() |
| 44 | current := version |
| 45 | fmt.Fprintf(out, "current version: %s\n", current) |
| 46 | |
| 47 | target := strings.TrimPrefix(toVer, "v") |
| 48 | if target == "" { |
| 49 | latest, err := resolveLatest() |
| 50 | if err != nil { |
| 51 | return fmt.Errorf("resolve latest: %w", err) |
| 52 | } |
| 53 | target = latest |
| 54 | } |
| 55 | fmt.Fprintf(out, "target version: %s\n", target) |
| 56 | |
| 57 | if strings.TrimPrefix(current, "v") == target { |
| 58 | fmt.Fprintln(out, "already up to date.") |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | exe, err := os.Executable() |
| 63 | if err != nil { |
| 64 | return fmt.Errorf("locate self: %w", err) |
| 65 | } |
| 66 | |
| 67 | archive := archiveName() |
| 68 | url := fmt.Sprintf("https://github.com/SamNet-dev/snix/releases/download/v%s/%s", target, archive) |
| 69 | fmt.Fprintf(out, "download: %s\n", url) |
| 70 | |
| 71 | if dryRun { |
| 72 | fmt.Fprintln(out, "(dry run — nothing downloaded or replaced)") |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | tmpDir, err := os.MkdirTemp("", "snix-update-*") |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | defer os.RemoveAll(tmpDir) |
| 81 | archPath := filepath.Join(tmpDir, archive) |
| 82 | |
| 83 | if err := downloadWithProgress(out, url, archPath); err != nil { |
| 84 | return fmt.Errorf("download: %w", err) |
| 85 | } |
| 86 | if err := verifySHA(out, archPath, url+".sha256"); err != nil { |
no test coverage detected