Skip to content

Commit

Permalink
Add support for reading YAML data from remote URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
a2ush committed May 14, 2024
1 parent 0a20489 commit d13870c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
8 changes: 8 additions & 0 deletions internal/ctl/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"image/color"
"image/png"
"os"
"strings"

"github.com/awslabs/diagram-as-code/internal/cache"
"github.com/awslabs/diagram-as-code/internal/definition"
Expand Down Expand Up @@ -277,3 +278,10 @@ func loadLinks(template *TemplateStruct, resources map[string]types.Node) {
}

}

func IsURL(str string) bool {
if strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://") {
return true
}
return false
}
39 changes: 31 additions & 8 deletions internal/ctl/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package ctl

import (
"io"
"net/http"
"os"

"github.com/awslabs/diagram-as-code/internal/definition"
Expand All @@ -14,17 +16,38 @@ import (

func CreateDiagramFromYAML(inputfile string, outputfile *string) {

log.Infof("input file: %s\n", inputfile)
data, err := os.ReadFile(inputfile)
if err != nil {
log.Fatal(err)
}
log.Infof("input file path: %s\n", inputfile)

var template TemplateStruct

err = yaml.Unmarshal([]byte(data), &template)
if err != nil {
log.Fatal(err)
if IsURL(inputfile) {
// URL from remote
resp, err := http.Get(inputfile)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

data, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

err = yaml.Unmarshal(data, &template)
if err != nil {
log.Fatal(err)
}

} else {
// Local file
data, err := os.ReadFile(inputfile)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal([]byte(data), &template)
if err != nil {
log.Fatal(err)
}
}

var ds definition.DefinitionStructure
Expand Down

0 comments on commit d13870c

Please sign in to comment.