Skip to content

Commit 040a950

Browse files
committed
feat: enhance workspace init with auto-detection and relative path resolution
- Auto-detect and pre-fill OpenAPI spec and Stainless config files in workspace init form - Resolve workspace config file paths relative to config file location in build command - Add support for common file patterns (openapi.yml, api.yml, spec.yml, etc.)
1 parent 0a0975c commit 040a950

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

pkg/cmd/build.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"os/exec"
13+
"path/filepath"
1314
"time"
1415

1516
"github.com/stainless-api/stainless-api-go"
@@ -533,18 +534,25 @@ func initAPICommandWithWorkspaceDefaults(ctx context.Context, cmd *cli.Command)
533534
if err != nil {
534535
return nil, err
535536
}
536-
config, _, err := FindWorkspaceConfig()
537+
config, configPath, err := FindWorkspaceConfig()
537538
if err == nil && config != nil {
539+
// Get the directory containing the workspace config file
540+
configDir := filepath.Dir(configPath)
541+
538542
if !cmd.IsSet("openapi-spec") && !cmd.IsSet("oas") && config.OpenAPISpec != "" {
543+
// Resolve OpenAPI spec path relative to workspace config directory
544+
openAPIPath := filepath.Join(configDir, config.OpenAPISpec)
539545
fileAction := getAPIFlagFileAction("body", "revision.openapi\\.yml.content")
540-
if err := fileAction(cc, cmd, config.OpenAPISpec); err != nil {
546+
if err := fileAction(cc, cmd, openAPIPath); err != nil {
541547
return nil, fmt.Errorf("failed to load OpenAPI spec from workspace config: %v", err)
542548
}
543549
}
544550

545551
if !cmd.IsSet("stainless-config") && !cmd.IsSet("config") && config.StainlessConfig != "" {
552+
// Resolve Stainless config path relative to workspace config directory
553+
stainlessConfigPath := filepath.Join(configDir, config.StainlessConfig)
546554
fileAction := getAPIFlagFileAction("body", "revision.openapi\\.stainless\\.yml.content")
547-
if err := fileAction(cc, cmd, config.StainlessConfig); err != nil {
555+
if err := fileAction(cc, cmd, stainlessConfigPath); err != nil {
548556
return nil, fmt.Errorf("failed to load Stainless config from workspace config: %v", err)
549557
}
550558
}

pkg/cmd/workspace.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func handleInitWorkspace(ctx context.Context, cmd *cli.Command) error {
7474
projectName := cmd.String("project")
7575
var openAPISpec, stainlessConfig string
7676

77+
// Pre-fill OpenAPI spec and Stainless config if found
78+
openAPISpec = findOpenAPISpec()
79+
stainlessConfig = findStainlessConfig()
80+
7781
// If project name wasn't provided via flag, prompt for all fields interactively
7882
if projectName == "" {
7983
projectInfoMap := fetchUserProjects(ctx)
@@ -249,6 +253,42 @@ func GetProjectNameFromConfig() string {
249253
return config.Project
250254
}
251255

256+
// findOpenAPISpec searches for common OpenAPI spec files in the current directory
257+
func findOpenAPISpec() string {
258+
commonOpenAPIFiles := []string{
259+
"openapi.yml",
260+
"openapi.yaml",
261+
"api.yml",
262+
"api.yaml",
263+
"spec.yml",
264+
"spec.yaml",
265+
}
266+
267+
for _, filename := range commonOpenAPIFiles {
268+
if _, err := os.Stat(filename); err == nil {
269+
return filename
270+
}
271+
}
272+
return ""
273+
}
274+
275+
// findStainlessConfig searches for common Stainless config files in the current directory
276+
func findStainlessConfig() string {
277+
commonStainlessFiles := []string{
278+
"openapi.stainless.yml",
279+
"openapi.stainless.yaml",
280+
"stainless.yml",
281+
"stainless.yaml",
282+
}
283+
284+
for _, filename := range commonStainlessFiles {
285+
if _, err := os.Stat(filename); err == nil {
286+
return filename
287+
}
288+
}
289+
return ""
290+
}
291+
252292
// InitWorkspaceConfig initializes a new workspace config in the current directory
253293
func InitWorkspaceConfig(projectName, openAPISpec, stainlessConfig string) error {
254294
// Get current working directory

0 commit comments

Comments
 (0)