MCPcopy
hub / github.com/github/github-mcp-server / writeSnap

Function writeSnap

internal/toolsnaps/toolsnaps.go:69–89  ·  view source on GitHub ↗
(snapPath string, contents []byte)

Source from the content-addressed store, hash-verified

67}
68
69func writeSnap(snapPath string, contents []byte) error {
70 // Sort the JSON keys recursively to ensure consistent output.
71 // We do this by unmarshaling and remarshaling, which ensures Go's JSON encoder
72 // sorts all map keys alphabetically at every level.
73 sortedJSON, err := sortJSONKeys(contents)
74 if err != nil {
75 return fmt.Errorf("failed to sort JSON keys: %w", err)
76 }
77
78 // Ensure the directory exists
79 if err := os.MkdirAll(filepath.Dir(snapPath), 0700); err != nil {
80 return fmt.Errorf("failed to create snapshot directory: %w", err)
81 }
82
83 // Write the snapshot file
84 if err := os.WriteFile(snapPath, sortedJSON, 0600); err != nil {
85 return fmt.Errorf("failed to write snapshot file: %w", err)
86 }
87
88 return nil
89}
90
91// sortJSONKeys recursively sorts all object keys in a JSON byte array by
92// unmarshaling to map[string]any and remarshaling. Go's JSON encoder

Callers 1

TestFunction · 0.85

Calls 1

sortJSONKeysFunction · 0.85

Tested by 1

TestFunction · 0.68