Skip to content

Commit

Permalink
Merge pull request #18 from juev/22092022
Browse files Browse the repository at this point in the history
feat: memory optimization by using custom struct
  • Loading branch information
juev authored Sep 22, 2022
2 parents b5fd89f + 95ac9f6 commit 02dd721
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
25 changes: 19 additions & 6 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ type Github struct {
client *github.Client
}

// Repository struct for storing parameters from Repository
type Repository struct {
FullName string
HTMLURL string
Language string
Description string
}

// NewGithub creates new github client
func NewGithub(ctx context.Context, token string) (client *Github) {
var tc *http.Client
Expand All @@ -36,7 +44,7 @@ func NewGithub(ctx context.Context, token string) (client *Github) {
}

// GetRepositories getting repositories from Github
func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][]github.Repository, repositories []github.Repository) {
func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][]Repository, repositories []Repository) {
opt := &github.ActivityListStarredOptions{}
opt.ListOptions.PerPage = perPage

Expand All @@ -49,7 +57,12 @@ func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][]
log.Fatalln("Error: cannot fetch starred:", err)
}
for _, r := range reps {
repositories = append(repositories, *r.Repository)
repositories = append(repositories, Repository{
FullName: r.Repository.GetFullName(),
HTMLURL: r.Repository.GetHTMLURL(),
Language: r.Repository.GetLanguage(),
Description: r.Repository.GetDescription(),
})
}

if len(reps) != perPage {
Expand All @@ -63,16 +76,16 @@ func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][]
return nil, repositories
}

langRepoMap = make(map[string][]github.Repository)
langRepoMap = make(map[string][]Repository)
for _, r := range repositories {
lang := "Others"
if r.Language != nil {
lang = capitalize(*r.Language)
if r.Language != "" {
lang = capitalize(r.Language)
}

langList, ok := langRepoMap[lang]
if !ok {
langList = []github.Repository{}
langList = []Repository{}
}
langList = append(langList, r)
langRepoMap[lang] = langList
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

_ "embed"

"github.com/google/go-github/github"
flag "github.com/spf13/pflag"
)

Expand Down Expand Up @@ -80,9 +79,9 @@ func main() {

r := struct {
SortCmd bool
LangRepoMap map[string][]github.Repository
LangRepoMap map[string][]Repository
UserName string
Repositories []github.Repository
Repositories []Repository
}{
SortCmd: sortCmd,
LangRepoMap: langRepoMap,
Expand Down
4 changes: 1 addition & 3 deletions templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
## {{ $lang }}

{{ range $langMap -}}
{{ if ne .Description nil -}}
- [{{ .FullName }}]({{ .HTMLURL }}) – {{ .Description }}{{ else -}}
- [{{ .FullName }}]({{ .HTMLURL }}){{- end }}
- [{{ .FullName }}]({{ .HTMLURL }}){{ if ne .Description "" }} – {{ .Description }}{{- end }}
{{ end }}
{{- end }}
{{- else }}
Expand Down

0 comments on commit 02dd721

Please sign in to comment.