Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: promote speakeasy version on successful run #1224

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions internal/model/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c ExecutableCommand[F]) Init() (*cobra.Command, error) {
return err
}
logger := log.From(cmd.Context())
logger.PrintfStyled(styles.DimmedItalic, "Failed to download latest Speakeasy version")
logger.PrintfStyled(styles.DimmedItalic, "Failed to download latest Speakeasy version: %s", err.Error())
logger.PrintfStyled(styles.DimmedItalic, "Running with local version. This might result in inconsistencies between environments\n")
}
}
Expand Down Expand Up @@ -291,15 +291,18 @@ func runWithVersionFromWorkflowFile(cmd *cobra.Command) error {
}
desiredVersion = latest.String()

logger.PrintfStyled(styles.DimmedItalic, "Running with latest Speakeasy version: %s\n", desiredVersion)
logger.PrintfStyled(styles.DimmedItalic, "Running with latest Speakeasy version\n")
} else {
logger.PrintfStyled(styles.DimmedItalic, "Running with speakeasyVersion from workflow.yaml: %s\n", desiredVersion)
logger.PrintfStyled(styles.DimmedItalic, "Running with speakeasyVersion defined in workflow.yaml\n")
}

// Get lockfile version before running the command, in case it gets overwritten
lockfileVersion := getSpeakeasyVersionFromLockfile()

runErr := runWithVersion(cmd, artifactArch, desiredVersion)
// If the workflow succeeds on latest, promote that version to the default
shouldPromote := wf.SpeakeasyVersion == "latest"

runErr := runWithVersion(cmd, artifactArch, desiredVersion, shouldPromote)
if runErr != nil {
// If the command failed to run with the latest version, try to run with the version from the lock file
if wf.SpeakeasyVersion == "latest" {
Expand All @@ -311,8 +314,8 @@ func runWithVersionFromWorkflowFile(cmd *cobra.Command) error {
}

if lockfileVersion != "" && lockfileVersion != desiredVersion {
logger.PrintfStyled(styles.DimmedItalic, "Rerunning with previous successful version: %s\n", lockfileVersion)
return runWithVersion(cmd, artifactArch, lockfileVersion)
logger.PrintfStyled(styles.DimmedItalic, "Rerunning with previous successful version")
return runWithVersion(cmd, artifactArch, lockfileVersion, false)
}
}

Expand All @@ -323,7 +326,8 @@ func runWithVersionFromWorkflowFile(cmd *cobra.Command) error {
return nil
}

func runWithVersion(cmd *cobra.Command, artifactArch, desiredVersion string) error {
// If promote is true, the version will be promoted to the default version (ie when running `speakeasy`)
func runWithVersion(cmd *cobra.Command, artifactArch, desiredVersion string, promote bool) error {
chase-crumbaugh marked this conversation as resolved.
Show resolved Hide resolved
vLocation, err := updates.InstallVersion(cmd.Context(), desiredVersion, artifactArch, 30)
if err != nil {
return ErrInstallFailed.Wrap(err)
Expand All @@ -349,6 +353,20 @@ func runWithVersion(cmd *cobra.Command, artifactArch, desiredVersion string) err
return fmt.Errorf("failed to run with version %s: %w", desiredVersion, err)
}

// If the workflow succeeded, make the used version the default
if promote && !env.IsGithubAction() && !env.IsLocalDev() {
currentExecPath, err := os.Executable()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose there is a valid reason for us to prompt and only do this when we are in interactive mode and we know we can. Idk what do you think

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, and I'm open to it, but it just felt a bit tedious. Like, if the CLI just ran using that version, why would I not want to install it (that's what im imagining customers saying/thinking)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya that's fair.

if err != nil {
log.From(cmd.Context()).Warnf("failed to promote version: %s", err.Error())
return nil
}

if err := os.Rename(vLocation, currentExecPath); err != nil {
log.From(cmd.Context()).Warnf("failed to promote version: %s", err.Error())
return nil
}
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/updates/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ func InstallVersion(ctx context.Context, desiredVersion, artifactArch string, ti
}

if _, err := os.Stat(dst); err == nil {
// It's important that these logs remain. We rely on them as part of `run` output
log.From(ctx).PrintfStyled(styles.DimmedItalic, "Found existing install for Speakeasy version %s\n", desiredVersion)
return dst, nil
}

// It's important that these logs remain. We rely on them as part of `run` output
log.From(ctx).PrintfStyled(styles.DimmedItalic, "Downloading Speakeasy version %s\n", desiredVersion)

return dst, install(artifactArch, asset.GetBrowserDownloadURL(), dst, timeout)
Expand Down
Loading