readFile reads a file from the Git repository at its latest revision. This method returns the plaintext contents, the SHA associated with the contents If an error occurs, the contents and sha will be nil & empty.
(filename string)
| 105 | // This method returns the plaintext contents, the SHA associated with the contents |
| 106 | // If an error occurs, the contents and sha will be nil & empty. |
| 107 | func (gh *Storage) readFile(filename string) ([]byte, string, error) { |
| 108 | if err := gh.ensureClient(); err != nil { |
| 109 | return nil, "", err |
| 110 | } |
| 111 | |
| 112 | contents, _, resp, err := gh.client.Repositories.GetContents( |
| 113 | context.Background(), |
| 114 | gh.RepositoryOwner, |
| 115 | gh.RepositoryName, |
| 116 | gh.fullPathName(filename), |
| 117 | &github.RepositoryContentGetOptions{Ref: "heads/" + gh.Branch}, |
| 118 | ) |
| 119 | if err != nil { |
| 120 | if resp != nil && resp.StatusCode == http.StatusNotFound { |
| 121 | return nil, "", errFileNotFound |
| 122 | } |
| 123 | return nil, "", err |
| 124 | } |
| 125 | |
| 126 | decoded, err := contents.GetContent() |
| 127 | return []byte(decoded), *contents.SHA, err |
| 128 | } |
| 129 | |
| 130 | // writeFile commits the contents to the Git repo at the given filename & revision. |
| 131 | // If the Git repo does not yet have a file at this filename, it will create the file. |