first commit
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package lists
|
||||
|
||||
import (
|
||||
"kode-starter/config"
|
||||
"kode-starter/utils"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var token string
|
||||
|
||||
var ListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List Github, Gitea orgs, project",
|
||||
Long: `A simple CLI app to list Github, Gitea orgs, project`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
config.SetInformations(cmd, args, token)
|
||||
|
||||
orgs, err := ListItems()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("List of ")
|
||||
for _, org := range orgs {
|
||||
fmt.Print(org)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func ListItems() ([]string, error) {
|
||||
godotenv.Load()
|
||||
|
||||
if config.GetVerboseFlag() {
|
||||
fmt.Println("execute list items...")
|
||||
fmt.Println("URL: " + config.GetUrlApiOrgs())
|
||||
fmt.Println("Token: " + config.GetToken())
|
||||
}
|
||||
body := UrlGetGithub(config.GetUrlApiOrgs(), config.GetToken())
|
||||
if config.GetJsonFlag() {
|
||||
return utils.BytesToStrings(body), nil
|
||||
} else {
|
||||
|
||||
var orgs []map[string]interface{}
|
||||
err := json.Unmarshal(body, &orgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
var orgNames []string
|
||||
for i, org := range orgs {
|
||||
orgNames = append(orgNames, fmt.Sprintf("%d. %s\n", i+1, org["username"]))
|
||||
}
|
||||
|
||||
return orgNames, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func UrlGetGithub(url string, token string) []byte {
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating request:", err)
|
||||
|
||||
}
|
||||
req.Header.Set("Authorization", "token "+config.GetToken())
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// req.Header.Add("Accept", "application/vnd.github.v3+json")
|
||||
fmt.Println(req.Header.Get("Authorization"))
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Error making request:", err)
|
||||
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error reading response body:", err)
|
||||
|
||||
}
|
||||
|
||||
return body
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user