-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: search usage type, function and command
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/lpmatos/loli/internal/trace" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// SearchMeCmd represents the search command | ||
var SearchMeCmd = &cobra.Command{ | ||
Use: "usage", | ||
Short: "Check the search quota and limit for your account (with API key) or IP address (without API key)", | ||
Long: ``, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
trace.SearchUsage(searchPretty) | ||
}, | ||
} | ||
|
||
func init() { | ||
SearchCmd.AddCommand(SearchMeCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package trace | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/fatih/color" | ||
"github.com/jedib0t/go-pretty/table" | ||
"github.com/lpmatos/loli/internal/constants" | ||
log "github.com/lpmatos/loli/internal/log" | ||
"github.com/lpmatos/loli/internal/types" | ||
) | ||
|
||
// SearchUsage function | ||
func SearchUsage(pretty bool) { | ||
searchURL := constants.TraceMoeUsage | ||
log.Infoln(searchURL) | ||
|
||
resp, error := http.Get(searchURL) | ||
if error != nil { | ||
log.Fatalln(error) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
log.Fatalf("Bad status code - %d", resp.StatusCode) | ||
} | ||
|
||
content, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
var usageResp types.UsageTraceMoe | ||
json.Unmarshal(content, &usageResp) | ||
|
||
if pretty { | ||
versionTable := table.NewWriter() | ||
versionTable.SetOutputMirror(os.Stdout) | ||
versionTable.AppendHeader(table.Row{"Info", "Content"}) | ||
versionTable.AppendRows([]table.Row{ | ||
{"π» IP", usageResp.ID}, | ||
{"π§Ύ Priority", strconv.Itoa(usageResp.Priority)}, | ||
{"π Concurrency", strconv.Itoa(usageResp.Concurrency)}, | ||
{"π Quota", strconv.Itoa(usageResp.Quota)}, | ||
{"π QuotaUsed", color.MagentaString(strconv.Itoa(usageResp.QuotaUsed))}, | ||
}) | ||
versionTable.SetStyle(table.StyleColoredBlueWhiteOnBlack) | ||
versionTable.Render() | ||
} else { | ||
fmt.Println("π» IP: " + usageResp.ID) | ||
fmt.Println("π§Ύ Priority: " + strconv.Itoa(usageResp.Priority)) | ||
fmt.Println("π Concurrency: " + strconv.Itoa(usageResp.Concurrency)) | ||
fmt.Println("π Quota: " + strconv.Itoa(usageResp.Quota)) | ||
fmt.Println("π QuotaUsed: " + color.MagentaString(strconv.Itoa(usageResp.QuotaUsed))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters