Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only regenerate when neccesary #2319

Open
trajan0x opened this issue Mar 20, 2024 · 1 comment
Open

only regenerate when neccesary #2319

trajan0x opened this issue Mar 20, 2024 · 1 comment

Comments

@trajan0x
Copy link
Contributor

No description provided.

Copy link

greptile-apps bot commented Jun 24, 2024

To ensure the Swagger collection is only regenerated when necessary, modify /services/omnirpc/swagger/generator/main.go to include a condition that checks for changes before executing the generation logic. Here are the steps:

  1. Compute the hash of the current collection.json file.
  2. Generate the new collection and compute its hash.
  3. Compare the hashes and only write to collection.json if they differ.
package main

import (
	"crypto/sha256"
	"github.com/synapsecns/sanguine/services/omnirpc/collection"
	"io/ioutil"
	"os"
)

func hashFile(filePath string) ([]byte, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	hash := sha256.New()
	if _, err := io.Copy(hash, file); err != nil {
		return nil, err
	}

	return hash.Sum(nil), nil
}

func main() {
	oldHash, err := hashFile("collection.json")
	if err != nil && !os.IsNotExist(err) {
		panic(err)
	}

	res, err := collection.CreateCollection()
	if err != nil {
		panic(err)
	}

	newHash := sha256.Sum256(res)

	if bytes.Equal(oldHash, newHash[:]) {
		return // No changes, skip writing
	}

	file, err := os.Create("collection.json")
	if err != nil {
		panic(err)
	}

	_, err = file.Write(res)
	if err != nil {
		panic(err)
	}
}

References

/services/omnirpc/swagger/generator/main.go
/services/omnirpc/swagger

Ask Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant