Skip to content

Commit

Permalink
fix: overlay implementation (#65)
Browse files Browse the repository at this point in the history
* fix: overlay implementation

* chore: also generate terraform docs

* fix: fmterrorf arg
  • Loading branch information
ThomasRooney authored Nov 14, 2023
1 parent e16893a commit 7d229d5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
7 changes: 7 additions & 0 deletions internal/actions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 %s", 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 {
Expand Down
45 changes: 32 additions & 13 deletions internal/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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)
}

for _, overlayFile := range overlayFiles {
if err := cli.ApplyOverlay(overlayFile, filePath, outPath); err != nil {
outPathAbs, err := filepath.Abs(outPath)
if err != nil {
return "", fmt.Errorf("failed to get absolute path for openapi overlay file: %w", err)
}

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) {
Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 7d229d5

Please sign in to comment.