MCPcopy Create free account
hub / github.com/SwishHQ/spread / getHash

Function getHash

cli/bundle_cli.go:110–134  ·  view source on GitHub ↗
(path string)

Source from the content-addressed store, hash-verified

108}
109
110func getHash(path string) (string, error) {
111 // First, we need to find all the files in the given path.
112 files, err := getAllFiles(path)
113 if err != nil {
114 return "", err
115 }
116 // We create a list to hold the names and special codes (hashes) of the files.
117 var fileHash []string
118 // Now, we go through each file we found.
119 for _, table1 := range files {
120 fb, _ := os.ReadFile(table1)
121 // We use a special tool (sha256) to create a unique code (hash) for the file content.
122 h := sha256.New()
123 h.Write(fb)
124 hash := h.Sum(nil)
125 fileName := strings.TrimPrefix(table1, path)
126 fileHash = append(fileHash, fileName+":"+fmt.Sprintf("%x", hash))
127 }
128 j, _ := json.Marshal(fileHash)
129 jStr := strings.ReplaceAll(string(j), "\\/", "/")
130 h := sha256.New()
131 h.Write([]byte(jStr))
132 hash := h.Sum(nil)
133 return fmt.Sprintf("%x", hash), nil
134}
135
136// This function helps us find all the files in a given directory and its subdirectories.
137func getAllFiles(dirPth string) (files []string, err error) {

Callers 1

PushBundleFunction · 0.85

Calls 1

getAllFilesFunction · 0.85

Tested by

no test coverage detected