Skip to content

Commit

Permalink
feat: open studio after run interactive and quickstart (#913)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Crumbaugh <chasecrumbaugh4@gmail.com>
  • Loading branch information
mfbx9da4 and chase-crumbaugh committed Sep 6, 2024
1 parent 584cbee commit ac801cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
47 changes: 30 additions & 17 deletions cmd/quickstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"context"
_ "embed"
"fmt"
"github.com/pkg/browser"
"github.com/speakeasy-api/speakeasy/internal/config"
"github.com/speakeasy-api/speakeasy/internal/studio"
"golang.org/x/exp/maps"
"io"
"os"
"path/filepath"
"strings"

"github.com/speakeasy-api/speakeasy/internal/log"

"github.com/pkg/browser"
"github.com/speakeasy-api/speakeasy/internal/charm/styles"
"github.com/speakeasy-api/speakeasy/internal/log"
"github.com/speakeasy-api/speakeasy/internal/model/flag"

"github.com/speakeasy-api/huh"
Expand All @@ -21,7 +23,7 @@ import (
"github.com/iancoleman/strcase"
"github.com/pkg/errors"
"github.com/speakeasy-api/openapi-generation/v2/pkg/generate"
config "github.com/speakeasy-api/sdk-gen-config"
sdkGenConfig "github.com/speakeasy-api/sdk-gen-config"
"github.com/speakeasy-api/sdk-gen-config/workflow"
speakeasyErrors "github.com/speakeasy-api/speakeasy-core/errors"
"github.com/speakeasy-api/speakeasy/internal/charm"
Expand Down Expand Up @@ -96,7 +98,7 @@ func quickstartExec(ctx context.Context, flags QuickstartFlags) error {
Sources: make(map[string]workflow.Source),
Targets: make(map[string]workflow.Target),
},
LanguageConfigs: make(map[string]*config.Configuration),
LanguageConfigs: make(map[string]*sdkGenConfig.Configuration),
}

if flags.Schema != "" {
Expand Down Expand Up @@ -238,7 +240,7 @@ func quickstartExec(ctx context.Context, flags QuickstartFlags) error {
}

for key, outConfig := range quickstartObj.LanguageConfigs {
if err := config.SaveConfig(outDir, outConfig); err != nil {
if err := sdkGenConfig.SaveConfig(outDir, outConfig); err != nil {
return errors.Wrapf(err, "failed to save config file for target %s", key)
}
}
Expand Down Expand Up @@ -294,16 +296,17 @@ func quickstartExec(ctx context.Context, flags QuickstartFlags) error {
}
}

// There should only be one target after quickstart
if len(wf.SDKOverviewURLs) == 1 {
overviewURL := wf.SDKOverviewURLs[initialTarget]
browser.OpenURL(overviewURL)
}

if changeDirMsg != "" {
logger.Println(styles.RenderWarningMessage("! ATTENTION DO THIS !", changeDirMsg))
}

if shouldLaunchStudio(ctx, wf) {
err = studio.LaunchStudio(ctx, wf)
} else if len(wf.SDKOverviewURLs) == 1 { // There should only be one target after quickstart
overviewURL := wf.SDKOverviewURLs[initialTarget]
browser.OpenURL(overviewURL)
}

return nil
}

Expand Down Expand Up @@ -347,15 +350,25 @@ func retryWithSampleSpec(ctx context.Context, workflowFile *workflow.Workflow, i
)

err = wf.RunWithVisualization(ctx)
// There should only be one target after quickstart
if err == nil && len(wf.SDKOverviewURLs) == 1 {
overviewURL := wf.SDKOverviewURLs[initialTarget]
browser.OpenURL(overviewURL)
}

return true, err
}

func shouldLaunchStudio(ctx context.Context, wf *run.Workflow) bool {
// Only one source at a time is supported in the studio at the moment
if len(wf.SourceResults) == 1 {
// If the source has a linting result, then it was loaded successfully so we can show something in the studio
if maps.Values(wf.SourceResults)[0].LintResult != nil {
// This will be true in most cases for admins, unless they have `auth switch`ed to a different workspace
if config.GetWorkspaceID() == "self" {
return true
}
}
}

return false
}

func printSampleSpecMessage(absSchemaPath string) {
fmt.Println(
styles.RenderInfoMessage(
Expand Down
20 changes: 9 additions & 11 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,15 @@ func runInteractive(ctx context.Context, flags RunFlags) error {
run.WithFrozenWorkflowLock(flags.FrozenWorkflowLock),
}

// Don't cleanup if we're launching studio, we need the temp output
if flags.LaunchStudio {
opts = append(opts, run.WithSkipCleanup())
}

workflow, err := run.NewWorkflow(
ctx,
opts...,
)

defer func() {
workflow.Cleanup()
}()

if err != nil {
return err
}
Expand All @@ -385,17 +385,15 @@ func runInteractive(ctx context.Context, flags RunFlags) error {
workflow.RootStep.Finalize(err == nil)
}

if err != nil && !flags.LaunchStudio {
return err
if err != nil {
log.From(ctx).Error(err.Error())
}

workflow.PrintSuccessSummary(ctx)

// Pass initial results to launch studio

if flags.LaunchStudio {
if shouldLaunchStudio(ctx, workflow) {
return studio.LaunchStudio(ctx, workflow)
}

return nil
return err
}

0 comments on commit ac801cf

Please sign in to comment.