Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SynAcktraa committed May 29, 2022
1 parent eebbc97 commit a0f0682
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
24 changes: 15 additions & 9 deletions hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
)

// Hostcode
type HostCode int

const (
Expand All @@ -14,10 +15,12 @@ const (
gitea
)

// Host struct which will mapped accordingly
type Host struct {
Hosts map[string]HostCode
}

// MapHosts maps hosts to their respective hostcode
func (host *Host) MapHosts() {
host.Hosts = map[string]HostCode{
"github.com": github,
Expand All @@ -32,10 +35,13 @@ func (host *Host) MapHosts() {
}
}

var hostcode HostCode
var choice string
var (
hostcode HostCode
choice string
)

func (host *Host) generateUrl(site string, arg []string) string {
// Returns url according to host and host info
func (host *Host) generateURL(site string, arg []string) string {

var url string
if len(arg) == 1 {
Expand Down Expand Up @@ -64,6 +70,7 @@ func (host *Host) generateUrl(site string, arg []string) string {
return url
}

// User struct to store the user json values
type User struct {
Github struct {
Username string `json:"login"`
Expand Down Expand Up @@ -94,6 +101,7 @@ type User struct {
Gitea struct{}
}

// Repository struct to store the repository json values
type Repository struct {
Github struct {
Name string `json:"full_name"`
Expand Down Expand Up @@ -126,12 +134,9 @@ type Repository struct {
Gitea struct{}
}

type Requestor interface {
Request(url string)
}

// Request user info
func (user *User) Request(url string) {
jsonDump := validateRequestAndFetchJson(url)
jsonDump := Request(url)
switch hostcode {
case github:
json.Unmarshal(jsonDump, &user.Github)
Expand All @@ -142,8 +147,9 @@ func (user *User) Request(url string) {
}
}

// Request repository info
func (repo *Repository) Request(url string) {
jsonDump := validateRequestAndFetchJson(url)
jsonDump := Request(url)
switch hostcode {
case github:
json.Unmarshal(jsonDump, &repo.Github)
Expand Down
12 changes: 7 additions & 5 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gookit/color"
)

// Final user info struct
type UserInstance struct {
Username string
Company string
Expand All @@ -28,6 +29,7 @@ type UserInstance struct {
Created string
}

// Final repo info struct
type RepoInstance struct {
Name string
Repository string
Expand All @@ -43,7 +45,8 @@ type RepoInstance struct {
}
}

func validateRequestAndFetchJson(url string) []byte {
// Request function checks status code and evalualtes accordingly
func Request(url string) []byte {
res, err := http.Get(url)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -67,16 +70,14 @@ func validateRequestAndFetchJson(url string) []byte {
return body
}

// global variables
var (
setcolor uint8
text = color.C256(231)
wg sync.WaitGroup
)

type Printer interface {
Print(host HostCode, url string)
}

// Print user stats
func (user *User) Print(host HostCode, url string) {

instance := new(UserInstance)
Expand Down Expand Up @@ -129,6 +130,7 @@ func (user *User) Print(host HostCode, url string) {
}
}

// Print repo stats
func (repo *Repository) Print(host HostCode, url string) {

var commits map[string]int
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
hostcode = host.Hosts[host_]
}
}
return host.generateUrl(host_, arg)
return host.generateURL(host_, arg)
}()

switch choice {
Expand Down
13 changes: 12 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<a href="https://github.com/synacktraa/gitfetch/network/members">
<img src="https://img.shields.io/github/stars/synacktraa/gitfetch.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABmJLR0QA/wD/AP+gvaeTAAAGHElEQVR4nO2d3asVVRiHn9fPUgstPZIaJCf8OpVIIal9WJZFYR9GEkQdigjKpH9BIerCu6ACLwu6kCxCBEHNi5Ii80Iok2OpcY6mqahodo5fvy7WbBTUs2dmr1kzc/Z6YGDPZuZ9fzPvrFlr1nrXDEQikUgkEolEIpFIpK2xsgW0gqSRwMvAouSv74ENZnahPFVtiqQOSTt1LT9LmlS2vrZD0tfXCUaDr8rW11ZImjdIMBo8ULbOPAwrW0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az5lZ4Q2WYBkjkqbigjI3lE9P7ME1aw+GcBY6UW487q1vdZk3/iuuJXUqlMOgSWvJgdXp3SPDQgYDwpeQ23EJZHVJrhPQkafXNi+h0zoXUZ9ggNO6IKTDMgJSN4JqDh2QhwL780FQzSGbvaOB07g3vdWJAWB83gGnrIQsIfOpXzDAaQ72htOQAanj7apBMO0hA1LHCr1BMO2h+rIMN4mlrvNLTgITzexy0Y5ClZAu6hsMcNPxgiQHhgpIneuPBkGOIVRA6lx/NAhyDLGEpCfIMYQYMZzC0Jn0c2fRLxoIUUJCXFnfJEvRLCzaQd0D8iduNG+5mS0HlgC/F+iv/nWhpF0FZH+ck7Ra0k3X8TdSLtvlTAF+fynjHHpD0jhJFzyflI1yE4ea+Z4mab1n3xflXupcTyQt9XgyeiW9nkPDMmX7xEUzCn3VbNF1iI977gVcFuRsM/s8685mthHXU7AGl1vVKvWtRyRtbfFq3CbJW5eFpE5Jm1rUtMWXnqBIGqH8FWufctyeMmhbJulATm1nJY0oSlthSJqV42AHJH0oaWwAfWMTXwM5dPp49XlYJM3PeJBbyjhQSTMT31m4P7TOlpE0Ru7Tdc3ok7SiAnpXJFqa8Z+kNF+Jqx6SPhrkwM5LWitpXNk6G8g9N61NtN2ID8rWmRu5z9x9pmu/HfWdpK6y9d0ISV2Jxqu5JOlTFfwZvlBDuF3AYmAE8IOZ7Qrht1Xkvqe7CDfleXsyjzISiUQikUgkEolEIpFIJFIO/wOlD3Lf1a3c8QAAAABJRU5ErkJggg==">
</a>
<img src="https://app.codacy.com/project/badge/Grade/575bab95b90d4088b13737e7e945992a"/>
<img src="https://app.codacy.com/project/badge/Grade/0802c0113c774485b23ce12ef744a9f5"/>

</p>

Expand Down Expand Up @@ -47,6 +47,12 @@ Build:
go build .
```

Help:

```sh
gitfetch help
```

Run:

```sh
Expand All @@ -73,6 +79,11 @@ Gitea | ``gt/<author>/<repo>`` | ``gitea/<author>/<repo>`` | ``gitea.com/
- [ ] Display user profile picture in terminal
- [ ] Add rest of the language color code

<br>

---
> A tool inspired from <a href="https://github.com/iddev5/scafetch">iddev5/scafetch</a>
---

## **License**

Expand Down
10 changes: 7 additions & 3 deletions repoutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"
)

//get color code according to repo language
func (repo *Repository) getColorCode(lang string) uint8 {

code := map[string]uint8{}
Expand All @@ -31,13 +32,15 @@ func (repo *Repository) getColorCode(lang string) uint8 {
return code[lang]
}

// Branches struct to store the branch name
type Branches []struct {
Name string `json:"name"`
}

func (storage *Branches) get_branch(url string) []string {
// get all available branches for specified repository
func (storage *Branches) getBranch(url string) []string {
branchUrl := fmt.Sprintf("%s/branches", url)
branchJson := validateRequestAndFetchJson(branchUrl)
branchJson := Request(branchUrl)

branches := func() []string {
json.Unmarshal(branchJson, &storage)
Expand All @@ -51,10 +54,11 @@ func (storage *Branches) get_branch(url string) []string {
return branches
}

// get total commits of all the branches
func (repo *Repository) branchCommits(url string) map[string]int {

br := new(Branches)
branches := br.get_branch(url)
branches := br.getBranch(url)
commitMap := make(map[string]int, len(branches))

for _, branch := range branches {
Expand Down

0 comments on commit a0f0682

Please sign in to comment.