Skip to content

Instantly share code, notes, and snippets.

@YagmurOzden
Created December 1, 2022 06:05
Show Gist options
  • Save YagmurOzden/e1007f8129187dff0f29cf32a8385e52 to your computer and use it in GitHub Desktop.
Save YagmurOzden/e1007f8129187dff0f29cf32a8385e52 to your computer and use it in GitHub Desktop.
Read Files From Github using GOLang
func main(){
jsonBody := TakeDataFromgithub("https://"+GetEnv("UserName")+":"+GetEnv("GithubToken")+GetEnv("Path"), GetEnv("FileName2"))
}
// Takes data from github and returns the value as string
func TakeDataFromgithub(URL string, FileName string) string {
log.Printf("%v Started to fetch data from Github", APINAME)
fs := memfs.New()
//Authenticate and clone the repository
repo, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: URL,
})
if err != nil {
log.Printf("%v Repo could not find. Error: %v", APINAME, err.Error())
} else {
log.Printf("%v Fetched Repository : %v", APINAME, repo)
}
//Reads the repository as byte
file, err := fs.Open(FileName)
if err != nil {
log.Printf("%v File could not find. Error: %v", APINAME, err.Error())
} else {
log.Printf("%v Fetched Repositories File : %v", APINAME, file.Name())
}
defer func(file billy.File) {
err := file.Close()
if err != nil {
log.Printf("%v Cannot close file. Error: %v", APINAME, err.Error())
}
}(file)
//turns into a string
return ParseData(file)
}
// For reading files that are bytes
func ParseData(file billy.File) string {
log.Printf("%v Started to parse file data to string", APINAME)
buf := make([]byte, 1)
var data string
for {
n, err := file.Read(buf)
if err == io.EOF {
break
}
if err != nil {
log.Printf("%v Cannot read the file. Error: %v", APINAME, err.Error())
continue
}
if n > 0 {
data += string(buf[:n])
}
}
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment