| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | |
| 8 | "the-valley/flow/internal/config" |
| 9 | "the-valley/flow/internal/selfupdate" |
| 10 | "github.com/spf13/cobra" |
| 11 | ) |
| 12 | |
| 13 | var selfUpdateCmd = &cobra.Command{ |
| 14 | Use: "self-update", |
| 15 | Short: "Install the tip of the configured klaus branch into the nix profile", |
| 16 | Long: `Brings the installed klaus up to the tip of a configured branch, for a host |
| 17 | whose klaus should follow upstream without a system rebuild. It is off until |
| 18 | the host config, ~/.klaus/config.json, has a self_update block (a repo's |
| 19 | .klaus/config.json can't set it): |
| 20 | |
| 21 | "self_update": {"repo": "git@valley-host:~/the-valley", "branch": "main"} |
| 22 | |
| 23 | repo is an https://, ssh://, git:// or file:// URL, an absolute path, or |
| 24 | user@host:path, read as ssh://user@host/path (write user@host:~/path for |
| 25 | a path in your home on a plain server). Any other transport (ext::, fd::) |
| 26 | and any character outside [A-Za-z0-9._~/:@-], which nix's flake-ref parser |
| 27 | would re-encode, is refused; git ls-remote and nix |
| 28 | read the repo by the same URL, and the git that self-update runs, directly or |
| 29 | through nix, is limited to those transports (GIT_ALLOW_PROTOCOL). branch |
| 30 | defaults to main. Each run: |
| 31 | 1. takes a lock under $XDG_STATE_HOME/klaus, so runs never overlap: a run |
| 32 | that finds another in progress exits 0 and says so; |
| 33 | 2. resolves the branch's commit with git ls-remote; |
| 34 | 3. exits 0 if the installed klaus was built from that commit (quietly; |
| 35 | -v says so); |
| 36 | 4. reads the names of the packages the flake offers at that commit, and |
| 37 | refuses, changing nothing, unless it offers packages.<system>.klaus, as |
| 38 | the-valley's does. Point self_update.repo at a repository whose flake |
| 39 | offers it and run self-update again: the run builds, smoke-tests and |
| 40 | installs it as below; |
| 41 | 5. exits 0 if that commit already failed to build or test (it is retried |
| 42 | when the branch moves, or with --retry); |
| 43 | 6. builds exactly that commit's klaus, never the flake's default package: |
| 44 | nix build 'git+<repo>?ref=refs/heads/<branch>&rev=<sha>#packages.<system>.klaus'; |
| 45 | 7. smoke-tests the build: its klaus --version must report the commit, and |
| 46 | its klaus status must exit 0. A failure records the commit as failed, |
| 47 | installs nothing, and exits non-zero; |
| 48 | 8. installs it into the user's default nix profile in place of the profile |
| 49 | element named klaus, leaving other elements alone. The system klaus |
| 50 | stays underneath as the fallback; |
| 51 | 9. records the current and previous store paths and commits, the time and |
| 52 | any failure in $XDG_STATE_HOME/klaus/self-update.json. |
| 53 | |
| 54 | Only the configured branch installs, pinned to the commit ls-remote returned, |
| 55 | and only after it builds and passes the smoke test. |
| 56 | |
| 57 | --rollback reinstalls the previous store path; a second --rollback returns |
| 58 | to the newer one. --pin leaves the current install alone until --unpin: |
| 59 | runs while pinned exit 0 with a note, so a bad tip isn't reinstalled while |
| 60 | it gets fixed. |
| 61 | |
| 62 | A running klaus dashboard notices the new klaus on PATH and restarts onto it.`, |
| 63 | Args: cobra.NoArgs, |
| 64 | RunE: func(cmd *cobra.Command, args []string) error { |
| 65 | cmd.SilenceUsage = true |
| 66 | retry, _ := cmd.Flags().GetBool("retry") |
| 67 | rollback, _ := cmd.Flags().GetBool("rollback") |
| 68 | pin, _ := cmd.Flags().GetBool("pin") |
| 69 | unpin, _ := cmd.Flags().GetBool("unpin") |
| 70 | verbose, _ := cmd.Flags().GetBool("verbose") |
| 71 | |
| 72 | dir, err := selfupdate.DefaultDir() |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | // Host config only: a repo's config must not choose what installs. |
| 77 | cfg, err := config.Load("") |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | u := &selfupdate.Updater{Dir: dir, Version: version, Out: os.Stdout, Verbose: verbose, Branch: "main"} |
| 82 | if su := cfg.SelfUpdate; su != nil { |
| 83 | u.Repo, u.Branch = su.Repo, su.BranchName() |
| 84 | } |
| 85 | switch { |
| 86 | case rollback: |
| 87 | return u.Rollback(cmd.Context()) |
| 88 | case pin || unpin: |
| 89 | return u.Pin(pin) |
| 90 | } |
| 91 | if u.Repo == "" { |
| 92 | return errors.New(`self-update is off: add a self_update block to ~/.klaus/config.json, e.g. |
| 93 | "self_update": {"repo": "git@valley-host:~/the-valley", "branch": "main"}`) |
| 94 | } |
| 95 | if err := u.Update(cmd.Context(), retry); err != nil { |
| 96 | return fmt.Errorf("self-update: %w", err) |
| 97 | } |
| 98 | return nil |
| 99 | }, |
| 100 | } |
| 101 | |
| 102 | func init() { |
| 103 | selfUpdateCmd.Flags().BoolP("verbose", "v", false, "also say so when klaus is up to date") |
| 104 | selfUpdateCmd.Flags().Bool("retry", false, "build the branch's tip even if it failed before") |
| 105 | selfUpdateCmd.Flags().Bool("rollback", false, "reinstall the previous klaus; again to return to the newer one") |
| 106 | selfUpdateCmd.Flags().Bool("pin", false, "leave the current install alone until --unpin") |
| 107 | selfUpdateCmd.Flags().Bool("unpin", false, "let self-update install the branch's tip again") |
| 108 | selfUpdateCmd.MarkFlagsMutuallyExclusive("retry", "rollback", "pin", "unpin") |
| 109 | rootCmd.AddCommand(selfUpdateCmd) |
| 110 | } |