Skip to content

Commit

Permalink
Merge pull request #1970 from meshery/revert-1954-catalogFix
Browse files Browse the repository at this point in the history
Revert "fix catalog w/signoff"
  • Loading branch information
vishalvivekm authored Nov 27, 2024
2 parents 07c3dd9 + 74ca366 commit 20ccaaf
Showing 1 changed file with 18 additions and 58 deletions.
76 changes: 18 additions & 58 deletions assets/artifact-hub-pkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,21 @@ func main() {
os.Exit(1)
}

patterns, err := fetchCatalogPatterns()
catalogPatterns, err := fetchCatalogPatterns()
if err != nil {
log.Error(err)
return
}

for _, pattern := range patterns {
var patterns struct {
Patterns []CatalogPattern `json:"patterns"`
}
if err := json.Unmarshal(catalogPatterns, &patterns); err != nil {
log.Error(utils.ErrUnmarshal(err))
return
}

for _, pattern := range patterns.Patterns {
if err := processPattern(pattern, token); err != nil {
log.Error(meshkitErrors.New(ErrProcessPatternCode, meshkitErrors.Alert,
[]string{"unable to process catalog pattern"},
Expand All @@ -99,46 +107,18 @@ func slugify(name string) string {

return s
}
func fetchCatalogPatterns() ([]CatalogPattern, error) {
// First request to get total count
resp, err := http.Get(fmt.Sprintf("%s/api/catalog/content/pattern?page=0&pagesize=10", mesheryCloudBaseURL))
func fetchCatalogPatterns() ([]byte, error) {
resp, err := http.Get(fmt.Sprintf("%s/api/catalog/content/pattern", mesheryCloudBaseURL))
if err != nil {
return nil, ErrHTTPGetRequest(err, fmt.Sprintf("%s/api/catalog/content/pattern?page=0&pagesize=10", mesheryCloudBaseURL))
return nil, ErrHTTPGetRequest(err, fmt.Sprintf("%s/api/catalog/content/pattern?trim=true", mesheryCloudBaseURL))
}
defer resp.Body.Close()

var initialResponse struct {
TotalCount int `json:"total_count"`
Patterns []CatalogPattern `json:"patterns"`
}
if err := json.NewDecoder(resp.Body).Decode(&initialResponse); err != nil {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, ErrReadRespBody(err)
}

totalPages := (initialResponse.TotalCount + 9) / 10 // Calculate number of pages (rounding up)

// Store all patterns from all pages
allPatterns := initialResponse.Patterns

// Fetch patterns from remaining pages
for page := 1; page <= totalPages; page++ {
resp, err := http.Get(fmt.Sprintf("%s/api/catalog/content/pattern?page=%d&pagesize=10", mesheryCloudBaseURL, page))
if err != nil {
return nil, ErrHTTPGetRequest(err, fmt.Sprintf("%s/api/catalog/content/pattern?page=%d&pagesize=10", mesheryCloudBaseURL, page))
}
defer resp.Body.Close()

var pageResponse struct {
Patterns []CatalogPattern `json:"patterns"`
}
if err := json.NewDecoder(resp.Body).Decode(&pageResponse); err != nil {
return nil, ErrReadRespBody(err)
}

allPatterns = append(allPatterns, pageResponse.Patterns...)
}

return allPatterns, nil
return body, nil
}

func processPattern(pattern CatalogPattern, token string) error {
Expand Down Expand Up @@ -229,30 +209,10 @@ func decodeURIComponent(encodedURI string) (string, error) {

func writePatternFile(pattern CatalogPattern, versionDir, patternType, patternInfo, patternCaveats, compatibility, patternImageURL string) error {
designFilePath := filepath.Join(versionDir, "design.yml")

var dataJson map[string]interface{}
if err := json.Unmarshal([]byte(pattern.PatternFile), &dataJson); err != nil {
// If JSON unmarshalling fails, try YAML unmarshalling
if yamlErr := yaml.Unmarshal([]byte(pattern.PatternFile), &dataJson); yamlErr != nil {
// Write faulty content to a file for debugging if both unmarshalling attempts fail
faultyFilePath := filepath.Join(versionDir, "faulty_pattern.yaml")
if writeErr := ioutil.WriteFile(faultyFilePath, []byte(pattern.PatternFile), 0644); writeErr != nil {
return fmt.Errorf("failed to write faulty data to file: %v", writeErr)
}
return fmt.Errorf("failed to unmarshal as JSON or YAML: %v; faulty data saved to %s", yamlErr, faultyFilePath)
}
}

// Step 2: Marshal the data into YAML format
yamlData, err := yaml.Marshal(dataJson)
if err != nil {
return fmt.Errorf("failed to marshal data to YAML: %v", err)
}

// Step 3: Write the YAML data to the file
if err := ioutil.WriteFile(designFilePath, yamlData, 0644); err != nil {
if err := ioutil.WriteFile(designFilePath, []byte(pattern.PatternFile), 0644); err != nil {
return utils.ErrWriteFile(err, designFilePath)
}

contenttemp, err := ioutil.ReadFile(designFilePath)
if err != nil {
return utils.ErrReadFile(err, designFilePath)
Expand Down

0 comments on commit 20ccaaf

Please sign in to comment.