Skip to content

Commit

Permalink
Add print all context option
Browse files Browse the repository at this point in the history
  • Loading branch information
rk0cc committed Apr 28, 2022
1 parent 29fec71 commit 9ec92bb
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 additions & 14 deletions gafmysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ import (
type PrintContext struct {
LastUpdate string `json:"last_update"`
Context []structure.GitHubRepositoryStructure `json:"context"`
HasPrev bool `json:"has_prev"`
HasNext bool `json:"has_next"`
}

type PrintContextPaged struct {
PrintContext
HasPrev bool `json:"has_prev"`
HasNext bool `json:"has_next"`
}

func main() {
setmode := flag.Bool("set", false, "Archive current data of GitHub repository API")
getmode := flag.Bool("get", false, "Get recent archived data of GitHub repository API")
page := flag.Int64("page", 1, "Display which page of the result returned, default is `1` (`-get` only)")
ppi := flag.Int64("ppi", 10, "How many repositories will be displayed in a single page, default is `10` (only accept 10 - 100 and can be divided by 10, `-get` only)")
printall := flag.Bool("all", false, "Print all repositories data as once")

flag.Parse()

Expand All @@ -32,24 +37,39 @@ func main() {
panic(serr)
}
} else if *getmode && !*setmode {
getContext(*page, *ppi)
if *printall {
getAllContext()
} else {
getContext(*page, *ppi)
}
} else {
flag.PrintDefaults()
}
}

func rangedRepo(page int64, ppi int64) (*PrintContext, error) {
func load_context() ([]structure.GitHubRepositoryStructure, *string, error) {
ctx, lu, gerr := gafmysql.GetArchivedRepositoryAPI()
if gerr != nil {
return nil, nil, gerr
}

return ctx, lu, nil
}

func rangedRepo(page int64, ppi int64) (*PrintContextPaged, error) {
ctx, lu, gerr := load_context()
if gerr != nil {
return nil, gerr
}

if len(ctx) == 0 && page == 1 {
return &PrintContext{
Context: []structure.GitHubRepositoryStructure{},
LastUpdate: *lu,
HasPrev: false,
HasNext: false,
return &PrintContextPaged{
PrintContext: PrintContext{
Context: []structure.GitHubRepositoryStructure{},
LastUpdate: *lu,
},
HasPrev: false,
HasNext: false,
}, nil
}

Expand All @@ -64,14 +84,34 @@ func rangedRepo(page int64, ppi int64) (*PrintContext, error) {
end = int64(len(ctx))
}

return &PrintContext{
Context: ctx[start:end],
LastUpdate: *lu,
HasPrev: page > 1,
HasNext: !last_page,
return &PrintContextPaged{
PrintContext: PrintContext{
Context: ctx[start:end],
LastUpdate: *lu,
},
HasPrev: page > 1,
HasNext: !last_page,
}, nil
}

func getAllContext() {
ctx, lu, gerr := load_context()
if gerr != nil {
panic(gerr)
}

paj, pajerr := json.Marshal(PrintContext{
Context: ctx,
LastUpdate: *lu,
})

if pajerr != nil {
printEmptyJson()
}

fmt.Println(string(paj))
}

func getContext(page int64, ppi int64) {
if ppi%10 != 0 {
panic("unaccepted page per items value: " + strconv.FormatInt(page, 10))
Expand Down

0 comments on commit 9ec92bb

Please sign in to comment.