@@ -5,10 +5,13 @@ package struct_markdown
55
66import (
77 _ "embed"
8+ "encoding/json"
89 "go/ast"
910 "go/parser"
1011 "go/token"
12+ "io"
1113 "log"
14+ "net/http"
1215 "os"
1316 "path/filepath"
1417 "strings"
@@ -31,6 +34,16 @@ func (cmd *Command) Help() string {
3134` + readme
3235}
3336
37+ type Version struct {
38+ Version string
39+ ReleaseStage string
40+ IsLatest bool
41+ }
42+
43+ type VersionResult struct {
44+ Result []Version
45+ }
46+
3447func (cmd * Command ) Run (args []string ) int {
3548 if len (args ) == 0 {
3649 // Default: process the file
@@ -48,10 +61,41 @@ func (cmd *Command) Run(args []string) int {
4861 for dir := filepath .Dir (absFilePath ); len (dir ) > 0 && projectRoot == "" ; dir = filepath .Dir (dir ) {
4962 base := filepath .Base (dir )
5063 if base == "packer" || base == "packer-internal" {
51- projectRoot = dir
52- filePath , _ = filepath .Rel (projectRoot , absFilePath )
53- docsFolder = filepath .Join ("website" , "content" , "partials" )
54- break
64+ if strings .Contains (dir , "web-unified-docs" ) {
65+ var newDir , oldDir , versionMetadataURL string
66+ versionMetadataURL = "https://web-unified-docs-hashicorp.vercel.app/api/content/packer/version-metadata"
67+ newDir , _ , _ = strings .Cut (dir , "/.content-source-repos" )
68+ oldDir = dir
69+ projectRoot = newDir
70+
71+ filePath , _ = filepath .Rel (oldDir , absFilePath )
72+
73+ // Get version metadata from UDR to determine latest version folder
74+ resp , err := http .Get (versionMetadataURL )
75+ if err != nil {
76+ log .Fatalf ("Get: %+v" , err )
77+ }
78+ b , err := io .ReadAll (resp .Body )
79+ if err != nil {
80+ log .Fatalf ("ReadAll: %+v" , err )
81+ }
82+ var result VersionResult
83+ json .Unmarshal (b , & result )
84+ var latestVersion string
85+ for _ , v := range result .Result {
86+ if v .IsLatest {
87+ latestVersion = v .Version
88+ break
89+ }
90+ }
91+ docsFolder = filepath .Join ("content" , "packer" , latestVersion , "content" , "partials" )
92+ break
93+ } else {
94+ projectRoot = dir
95+ filePath , _ = filepath .Rel (projectRoot , absFilePath )
96+ docsFolder = filepath .Join ("website" , "content" , "partials" )
97+ break
98+ }
5599 }
56100 if base == "packer-plugin-sdk" {
57101 projectRoot = dir
0 commit comments