upgradecli

package
v0.7.0-alpha.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package upgradecli provides the command to update the CLI version in Nix.

Index

Constants

This section is empty.

Variables

View Source
var UpgradeCliCmd = &cobra.Command{
	Use:   "upgrade-cli",
	Short: "Updates the CLI version definition in .idx/contextvibes.nix",
	RunE: func(cmd *cobra.Command, _ []string) error {
		presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr())
		ctx := cmd.Context()

		nixFilePath := filepath.Join(".idx", "contextvibes.nix")
		if _, err := os.Stat(nixFilePath); os.IsNotExist(err) {
			presenter.Error("File not found: %s", nixFilePath)
			presenter.Advice("Run 'contextvibes factory scaffold idx' first.")

			return errors.New("nix file missing")
		}

		presenter.Step("Checking for updates...")
		ghClient, err := github.NewClient(ctx, globals.AppLogger, "contextvibes", "cli")
		if err != nil {
			return fmt.Errorf("failed to init github client: %w", err)
		}

		release, _, err := ghClient.Repositories.GetLatestRelease(ctx, "contextvibes", "cli")
		if err != nil {
			return fmt.Errorf("failed to get latest release: %w", err)
		}

		latestVersion := strings.TrimPrefix(release.GetTagName(), "v")
		presenter.Info("Latest version: %s", latestVersion)

		contentBytes, err := os.ReadFile(nixFilePath)
		if err != nil {
			return fmt.Errorf("failed to read %s: %w", nixFilePath, err)
		}
		content := string(contentBytes)

		versionRegex := regexp.MustCompile(`version [?=]= "([0-9.]+)";`)
		matches := versionRegex.FindStringSubmatch(content)

		if len(matches) < 2 {

			return fmt.Errorf("could not parse current version in %s", nixFilePath)
		}
		currentVersion := matches[1]

		if currentVersion == latestVersion {
			presenter.Success("Already up to date (%s).", currentVersion)

			return nil
		}

		presenter.Info("Current version: %s. Upgrading to %s...", currentVersion, latestVersion)

		downloadURL := fmt.Sprintf("https://github.com/contextvibes/cli/releases/download/v%s/contextvibes", latestVersion)

		presenter.Step("Prefetching hash for %s...", downloadURL)
		if !globals.ExecClient.CommandExists("nix-prefetch-url") {

			return errors.New("nix-prefetch-url not found; are you in the Nix environment?")
		}

		hashOutput, _, err := globals.ExecClient.CaptureOutput(ctx, ".", "nix-prefetch-url", downloadURL)
		if err != nil {
			return fmt.Errorf("failed to prefetch url: %w", err)
		}
		newHash := strings.TrimSpace(hashOutput)

		newContent := versionRegex.ReplaceAllString(content, fmt.Sprintf(`version ? "%s";`, latestVersion))

		if strings.Contains(newContent, "binHash ?") {
			hashRegex := regexp.MustCompile(`binHash \? "[^"]+";`)
			newContent = hashRegex.ReplaceAllString(newContent, fmt.Sprintf(`binHash ? "%s";`, newHash))
		} else {
			hashRegex := regexp.MustCompile(`sha256 = "[^"]+";`)
			newContent = hashRegex.ReplaceAllString(newContent, fmt.Sprintf(`sha256 = "%s";`, newHash))
		}

		if err := os.WriteFile(nixFilePath, []byte(newContent), 0o600); err != nil {
			return fmt.Errorf("failed to write %s: %w", nixFilePath, err)
		}

		presenter.Success("Updated %s to version %s.", nixFilePath, latestVersion)
		presenter.Advice("Please rebuild your environment (Command Palette > Rebuild Environment) to apply changes.")

		return nil
	},
}

UpgradeCliCmd represents the upgrade-cli command.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL