From 0e1794166cf982c4631bcf4814d4c18995b402b6 Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Tue, 14 Nov 2023 21:22:24 +0000 Subject: [PATCH 1/3] fix: overlay implementation --- internal/document/document.go | 37 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/internal/document/document.go b/internal/document/document.go index 82ee4890..52ddb832 100644 --- a/internal/document/document.go +++ b/internal/document/document.go @@ -115,29 +115,39 @@ func mergeFiles(files []string) (string, error) { return "", fmt.Errorf("failed to create openapi directory: %w", err) } - if err := cli.MergeDocuments(files, outPath); err != nil { + absOutPath, err := filepath.Abs(outPath) + if err != nil { + return "", fmt.Errorf("failed to get absolute path for openapi file: %w", err) + } + + if err := cli.MergeDocuments(files, absOutPath); err != nil { return "", fmt.Errorf("failed to merge openapi files: %w", err) } - return outPath, nil + return absOutPath, nil } func applyOverlay(filePath string, overlayFiles []string) (string, error) { - outPath := filepath.Join(environment.GetWorkspace(), "repo", ".openapi", "openapi_overlay") + outPath := filepath.Join(environment.GetWorkspace(), "openapi", "openapi_overlay") if err := os.MkdirAll(filepath.Dir(outPath), os.ModePerm); err != nil { return "", fmt.Errorf("failed to create openapi directory: %w", err) } + outPathAbs, err := filepath.Abs(outPath) + if err != nil { + return "", fmt.Errorf("failed to get absolute path for openapi overlay file: %w", err) + } + for _, overlayFile := range overlayFiles { - if err := cli.ApplyOverlay(overlayFile, filePath, outPath); err != nil { + if err := cli.ApplyOverlay(overlayFile, filePath, outPathAbs); err != nil { return "", fmt.Errorf("failed to apply overlay: %w", err) } - filePath = outPath + filePath = outPathAbs } - return outPath, nil + return filePath, nil } func resolveFiles(files []file, typ string) ([]string, error) { @@ -150,8 +160,12 @@ func resolveFiles(files []file, typ string) ([]string, error) { if _, err := os.Stat(localPath); err == nil { fmt.Printf("Found local %s file: %s\n", typ, localPath) + absPath, err := filepath.Abs(localPath) + if err != nil { + return nil, fmt.Errorf("failed to get absolute path for %s file: %w", localPath, err) + } - outFiles = append(outFiles, localPath) + outFiles = append(outFiles, absPath) } else { u, err := url.Parse(file.Location) if err != nil { @@ -172,11 +186,16 @@ func resolveFiles(files []file, typ string) ([]string, error) { return nil, fmt.Errorf("failed to create %s directory: %w", typ, err) } - if err := download.DownloadFile(u.String(), filePath, file.Header, file.Token); err != nil { + absPath, err := filepath.Abs(filePath) + if err != nil { + return nil, fmt.Errorf("failed to get absolute path for %s file: %w", filePath, err) + } + + if err := download.DownloadFile(u.String(), absPath, file.Header, file.Token); err != nil { return nil, fmt.Errorf("failed to download %s file: %w", typ, err) } - outFiles = append(outFiles, filePath) + outFiles = append(outFiles, absPath) } } From f9176a66fc5beda26f2e4d3d69dbca73c9496ef0 Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Tue, 14 Nov 2023 21:50:39 +0000 Subject: [PATCH 2/3] chore: also generate terraform docs --- internal/actions/generate.go | 7 +++++++ internal/cli/cli.go | 22 ++++++++++++++++++++++ internal/document/document.go | 18 +++++++++--------- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/internal/actions/generate.go b/internal/actions/generate.go index 29478822..03c78297 100644 --- a/internal/actions/generate.go +++ b/internal/actions/generate.go @@ -91,6 +91,13 @@ func Generate() error { Path: path, } } + if lang == "terraform" { + // Also trigger "go generate ./..." to regenerate docs + err = cli.TriggerGoGenerate() + if err != nil { + return err + } + } } } diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 869ce813..2acb6f59 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -2,7 +2,10 @@ package cli import ( "fmt" + "github.com/speakeasy-api/sdk-generation-action/internal/environment" "os" + "os/exec" + "path/filepath" "regexp" "strconv" "strings" @@ -45,6 +48,25 @@ func GetSupportedLanguages() ([]string, error) { return strings.Split(langs, ", "), nil } +func TriggerGoGenerate() error { + tidyCmd := exec.Command("go", "mod", "tidy") + tidyCmd.Dir = filepath.Join(environment.GetWorkspace(), "repo", environment.GetWorkingDirectory()) + output, err := tidyCmd.CombinedOutput() + if err != nil { + return fmt.Errorf("error running command: go mod tidy - %w\n", err, string(output)) + } + generateCmd := exec.Command("go", "generate", "./...") + generateCmd.Dir = filepath.Join(environment.GetWorkspace(), "repo", environment.GetWorkingDirectory()) + // connect generateCmd stdout to this stdout + generateCmd.Stdout = os.Stdout + err = generateCmd.Run() + if err != nil { + return fmt.Errorf("error running command: go generate ./... - %w\n", err) + } + + return nil +} + func GetSpeakeasyVersion() (*version.Version, error) { out, err := runSpeakeasyCommand("--version") if err != nil { diff --git a/internal/document/document.go b/internal/document/document.go index 52ddb832..999317bc 100644 --- a/internal/document/document.go +++ b/internal/document/document.go @@ -128,18 +128,18 @@ func mergeFiles(files []string) (string, error) { } func applyOverlay(filePath string, overlayFiles []string) (string, error) { - outPath := filepath.Join(environment.GetWorkspace(), "openapi", "openapi_overlay") + for i, overlayFile := range overlayFiles { + outPath := filepath.Join(environment.GetWorkspace(), "openapi", fmt.Sprintf("openapi_overlay_%v", i)) - if err := os.MkdirAll(filepath.Dir(outPath), os.ModePerm); err != nil { - return "", fmt.Errorf("failed to create openapi directory: %w", err) - } + if err := os.MkdirAll(filepath.Dir(outPath), os.ModePerm); err != nil { + return "", fmt.Errorf("failed to create openapi directory: %w", err) + } - outPathAbs, err := filepath.Abs(outPath) - if err != nil { - return "", fmt.Errorf("failed to get absolute path for openapi overlay file: %w", err) - } + outPathAbs, err := filepath.Abs(outPath) + if err != nil { + return "", fmt.Errorf("failed to get absolute path for openapi overlay file: %w", err) + } - for _, overlayFile := range overlayFiles { if err := cli.ApplyOverlay(overlayFile, filePath, outPathAbs); err != nil { return "", fmt.Errorf("failed to apply overlay: %w", err) } From e649f91223bf7b17dfe013e0a5bcca4738209792 Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Tue, 14 Nov 2023 21:52:55 +0000 Subject: [PATCH 3/3] fix: fmterrorf arg --- internal/cli/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 2acb6f59..c2c306d0 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -53,7 +53,7 @@ func TriggerGoGenerate() error { tidyCmd.Dir = filepath.Join(environment.GetWorkspace(), "repo", environment.GetWorkingDirectory()) output, err := tidyCmd.CombinedOutput() if err != nil { - return fmt.Errorf("error running command: go mod tidy - %w\n", err, string(output)) + return fmt.Errorf("error running command: go mod tidy - %w\n %s", err, string(output)) } generateCmd := exec.Command("go", "generate", "./...") generateCmd.Dir = filepath.Join(environment.GetWorkspace(), "repo", environment.GetWorkingDirectory())