Skip to content

Commit

Permalink
chore: support env variables in workflow locations (#54)
Browse files Browse the repository at this point in the history
* chore: support env variables in workflow locations

* chore: fix tests
  • Loading branch information
ThomasRooney authored Sep 13, 2024
1 parent 59f2036 commit fb92750
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
28 changes: 21 additions & 7 deletions workflow/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,22 @@ type FallbackCodeSamples struct {
FallbackCodeSamplesLanguage string `yaml:"fallbackCodeSamplesLanguage,omitempty"`
}

type LocationString string

func (l LocationString) Resolve() string {
if strings.HasPrefix(string(l), "$") {
return os.ExpandEnv(string(l))
}

return string(l)
}

func (l LocationString) Reference() string {
return string(l)
}

type Document struct {
Location string `yaml:"location"`
Location LocationString `yaml:"location"`
Auth *Auth `yaml:",inline"`
}

Expand Down Expand Up @@ -144,7 +158,7 @@ func (s Source) GetOutputLocation() (string, error) {
}

func (s Source) handleSingleInput() (string, error) {
input := s.Inputs[0].Location
input := s.Inputs[0].Location.Resolve()
switch getFileStatus(input) {
case fileStatusLocal:
return input, nil
Expand All @@ -170,7 +184,7 @@ func (s Source) generateOutputPath() (string, error) {
hashInputs := func() string {
var combined string
for _, input := range s.Inputs {
combined += input.Location
combined += input.Location.Resolve()
}
hash := sha256.Sum256([]byte(combined))
return fmt.Sprintf("%x", hash)[:6]
Expand Down Expand Up @@ -204,7 +218,7 @@ func (d Document) Validate() error {
}

if d.Auth != nil {
if getFileStatus(d.Location) != fileStatusRemote {
if getFileStatus(d.Location.Resolve()) != fileStatusRemote {
return fmt.Errorf("auth is only supported for remote documents")
}

Expand All @@ -217,11 +231,11 @@ func (d Document) Validate() error {
}

func (d Document) IsRemote() bool {
return getFileStatus(d.Location) == fileStatusRemote
return getFileStatus(d.Location.Resolve()) == fileStatusRemote
}

func (d Document) IsSpeakeasyRegistry() bool {
return strings.Contains(d.Location, "registry.speakeasyapi.dev")
return strings.Contains(d.Location.Resolve(), "registry.speakeasyapi.dev")
}

func (f FallbackCodeSamples) Validate() error {
Expand Down Expand Up @@ -300,7 +314,7 @@ func ParseSpeakeasyRegistryReference(location string) *SpeakeasyRegistryDocument
}

func (d Document) GetTempDownloadPath(tempDir string) string {
return filepath.Join(tempDir, fmt.Sprintf("downloaded_%s%s", randStringBytes(10), filepath.Ext(d.Location)))
return filepath.Join(tempDir, fmt.Sprintf("downloaded_%s%s", randStringBytes(10), filepath.Ext(d.Location.Resolve())))
}

func (d Document) GetTempRegistryDir(tempDir string) string {
Expand Down
12 changes: 6 additions & 6 deletions workflow/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,12 @@ func createLocalFiles(s workflow.Source) (string, error) {

for _, input := range s.Inputs {
var filePath string
if strings.HasPrefix(input.Location, "~/") {
filePath = workflow.SanitizeFilePath(input.Location)
if strings.HasPrefix(input.Location.Resolve(), "~/") {
filePath = workflow.SanitizeFilePath(input.Location.Resolve())
} else {
filePath = filepath.Join(tmpDir, input.Location)
filePath = filepath.Join(tmpDir, input.Location.Resolve())
}
_, err := url.ParseRequestURI(input.Location)
_, err := url.ParseRequestURI(input.Location.Resolve())
if err != nil {
if err := createEmptyFile(filePath); err != nil {
return "", err
Expand All @@ -591,9 +591,9 @@ func createLocalFiles(s workflow.Source) (string, error) {

for _, overlay := range s.Overlays {
if overlay.Document != nil {
_, err := url.ParseRequestURI(overlay.Document.Location)
_, err := url.ParseRequestURI(overlay.Document.Location.Resolve())
if err != nil {
if err := createEmptyFile(filepath.Join(tmpDir, overlay.Document.Location)); err != nil {
if err := createEmptyFile(filepath.Join(tmpDir, overlay.Document.Location.Resolve())); err != nil {
return "", err
}
}
Expand Down

0 comments on commit fb92750

Please sign in to comment.