build 0.0.0.25
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package cmdLists
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/github"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func ListGitHubOrganization() ([]string, error) {
|
||||
|
||||
var orgs []string
|
||||
|
||||
return orgs, nil
|
||||
}
|
||||
|
||||
func ListGitHubProjectsfromUser(username string) ([]*github.Repository, error) {
|
||||
|
||||
// Replace with your access token
|
||||
token := "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"
|
||||
|
||||
// Create an oauth2 token source with the access token
|
||||
tokenSource := oauth2.StaticTokenSource(
|
||||
&oauth2.Token{AccessToken: token},
|
||||
)
|
||||
|
||||
// Create an oauth2 http client with the token source
|
||||
oauth2Client := oauth2.NewClient(context.Background(), tokenSource)
|
||||
|
||||
// Create a new github client with the oauth2 http client
|
||||
client := github.NewClient(oauth2Client)
|
||||
|
||||
// List the repositories of the user
|
||||
repos, _, err := client.Repositories.List(context.Background(), username, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return repos, nil
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package cmdLists
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"kode-starter/utils"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// The ListOrganization function retrieves a list of organizations from a specified URL and returns
|
||||
// their names and URLs.
|
||||
func ListGitTeaOrganization() ([]string, error) {
|
||||
godotenv.Load()
|
||||
|
||||
body := UrlGetBody(CfgGlobal.GetUrlApiOrgs())
|
||||
if CfgGlobal.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 {
|
||||
// nb := "[ " + strconv.FormatInt(int64(i+1), 10) + " ] "
|
||||
nb := fmt.Sprintf("[ %02d ] ", int64(i+1))
|
||||
orgName := fmt.Sprintf("%s", org["username"])
|
||||
orgUrl := fmt.Sprintf("[ %s/%s ]\n", CfgGlobal.GetUrlBase(), org["username"])
|
||||
orgNames = append(
|
||||
orgNames,
|
||||
utils.PadString(nb, 4),
|
||||
utils.PadDotString(orgName, 30),
|
||||
orgUrl)
|
||||
}
|
||||
return orgNames, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This function lists the projects of a given organization and returns their names and clone URLs.
|
||||
func ListGitTeaProjects(org string) ([]string, error) {
|
||||
godotenv.Load()
|
||||
|
||||
body := UrlGetBody(CfgGlobal.GetUrlApiOrgs() + "/" + org + "/repos")
|
||||
if CfgGlobal.GetJsonFlag() {
|
||||
return utils.BytesToStrings(body), nil
|
||||
} else {
|
||||
var prjs []map[string]interface{}
|
||||
err := json.Unmarshal(body, &prjs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
var orgNames []string
|
||||
|
||||
for i, prj := range prjs {
|
||||
nb := fmt.Sprintf("[ %02d ] ", int64(i+1))
|
||||
projectName := fmt.Sprintf("%s", prj["name"])
|
||||
cloneUrl := fmt.Sprintf("[ %s ]\n", prj["clone_url"])
|
||||
orgNames = append(
|
||||
orgNames,
|
||||
utils.PadString(nb, 4),
|
||||
utils.PadString(projectName, 40),
|
||||
cloneUrl,
|
||||
)
|
||||
}
|
||||
return orgNames, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
-85
@@ -1,18 +1,23 @@
|
||||
package cmdLists
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"kode-starter/config"
|
||||
"kode-starter/utils"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// initailize global config
|
||||
var CfgGlobal *config.Config // Declare a global variable for config
|
||||
|
||||
// init configuration for this package
|
||||
func InitConfig(config *config.Config) {
|
||||
CfgGlobal = config // Assign the passed config to the global variable
|
||||
}
|
||||
|
||||
// This is a variable declaration that creates a new Cobra command called `ListCmd`. The command has a
|
||||
// `Use` field that specifies the name of the command, a `Short` field that provides a brief
|
||||
// description of the command, and a `Long` field that provides a more detailed description of the
|
||||
@@ -25,112 +30,84 @@ var ListCmd = &cobra.Command{
|
||||
Long: `A simple CLI app to list Github, Gitea orgs, project`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
config.SetInformations(cmd, args)
|
||||
// Start list command
|
||||
startList(cmd, args)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
// The startList function sets some configuration information based on the command line arguments.
|
||||
func startList(cmd *cobra.Command, args []string) {
|
||||
|
||||
CfgGlobal.DebugPrintConfig("Function startList")
|
||||
|
||||
// initialize global config for command list
|
||||
CfgGlobal.InitCmdList(cmd, args)
|
||||
|
||||
// print header if json flag is not set
|
||||
if !CfgGlobal.GetJsonFlag() {
|
||||
utils.PrintHeader()
|
||||
}
|
||||
|
||||
if CfgGlobal.GetTeaFlag() {
|
||||
|
||||
// if -o flag is set, list projects for that org
|
||||
if config.GetListOrganisationFlag() {
|
||||
if !config.GetJsonFlag() {
|
||||
// utils.PrintHeader()
|
||||
fmt.Println("\nList projects for org: " + config.GetListOrganisation() + "\n")
|
||||
fmt.Println(utils.CreateLine(6))
|
||||
if CfgGlobal.GetListOrganizationFlag() {
|
||||
if !CfgGlobal.GetJsonFlag() {
|
||||
fmt.Println("\n L I S T P R O J E C T S F O R O R G. ")
|
||||
fmt.Println(utils.CreateLine(8))
|
||||
fmt.Println(" ==> " + CfgGlobal.GetListOrganization() + " <== ")
|
||||
fmt.Println(utils.CreateLine(8))
|
||||
}
|
||||
projects, err := ListProjects(config.GetListOrganisation())
|
||||
projects, err := ListGitTeaProjects(CfgGlobal.GetListOrganization())
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
for _, project := range projects {
|
||||
fmt.Print(project)
|
||||
fmt.Println(project)
|
||||
// fmt.Print(project)
|
||||
}
|
||||
fmt.Println(utils.CreateLine(8))
|
||||
|
||||
} else {
|
||||
orgs, err := ListOrganization()
|
||||
orgs, err := ListGitTeaOrganization()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
if !config.GetJsonFlag() {
|
||||
fmt.Println("\nList of Organizations :")
|
||||
fmt.Println(utils.CreateLine(4))
|
||||
if !CfgGlobal.GetJsonFlag() {
|
||||
fmt.Println("\n L I S T O R G A N I Z A T I O N S")
|
||||
fmt.Println(utils.CreateLine(8))
|
||||
}
|
||||
for _, org := range orgs {
|
||||
|
||||
fmt.Print(org)
|
||||
}
|
||||
fmt.Println(utils.CreateLine(8))
|
||||
}
|
||||
} else if CfgGlobal.GetGitFlag() {
|
||||
fmt.Println("\n L I S T G I T H U B P R O J E C T S F O R : " + CfgGlobal.GetListUser())
|
||||
fmt.Println(utils.CreateLine(11))
|
||||
repos, err := ListGitHubProjectsfromUser(CfgGlobal.GetListUser())
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
nb := 1
|
||||
for _, repo := range repos {
|
||||
utils.PrintTreeElement(nb, *repo.Name, *repo.HTMLURL)
|
||||
nb++
|
||||
}
|
||||
fmt.Println(utils.CreateLine(11))
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
// The ListOrganization function retrieves a list of organizations from a specified URL and returns
|
||||
// their names and URLs.
|
||||
func ListOrganization() ([]string, error) {
|
||||
godotenv.Load()
|
||||
|
||||
if config.GetVerboseFlag() {
|
||||
fmt.Println("execute ListOrganization ...")
|
||||
fmt.Println("URL: " + config.GetUrlApiOrgs())
|
||||
fmt.Println("Token: " + config.GetToken())
|
||||
}
|
||||
body := UrlGetBody(config.GetUrlApiOrgs())
|
||||
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 {
|
||||
nb := strconv.FormatInt(int64(i+1), 10)
|
||||
orgName := fmt.Sprintf("%s", org["username"])
|
||||
orgUrl := fmt.Sprintf("[ %s/%s ]\n", config.GetUrlBase(), org["username"])
|
||||
orgNames = append(
|
||||
orgNames,
|
||||
utils.PadString(nb, 4),
|
||||
utils.PadString(orgName, 30),
|
||||
orgUrl)
|
||||
}
|
||||
return orgNames, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
// print help if no flag is set
|
||||
cmd.Help()
|
||||
|
||||
// This function lists the projects of a given organization and returns their names and clone URLs.
|
||||
func ListProjects(org string) ([]string, error) {
|
||||
godotenv.Load()
|
||||
|
||||
if config.GetVerboseFlag() {
|
||||
fmt.Println("execute ListProjects ...")
|
||||
fmt.Println("URL: " + config.GetUrlApiOrgs())
|
||||
fmt.Println("Token: " + config.GetToken())
|
||||
}
|
||||
|
||||
body := UrlGetBody(config.GetUrlApiOrgs() + "/" + org + "/repos")
|
||||
if config.GetJsonFlag() {
|
||||
return utils.BytesToStrings(body), nil
|
||||
} else {
|
||||
var prjs []map[string]interface{}
|
||||
err := json.Unmarshal(body, &prjs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
var orgNames []string
|
||||
|
||||
for i, prj := range prjs {
|
||||
nb := strconv.FormatInt(int64(i+1), 10)
|
||||
projectName := fmt.Sprintf("%s", prj["name"])
|
||||
cloneUrl := fmt.Sprintf("[ %s ]\n", prj["clone_url"])
|
||||
orgNames = append(
|
||||
orgNames,
|
||||
utils.PadString(nb, 4),
|
||||
utils.PadString(projectName, 40),
|
||||
cloneUrl,
|
||||
)
|
||||
}
|
||||
return orgNames, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This function sends a GET request to a specified URL with authorization and content-type headers,
|
||||
@@ -142,7 +119,7 @@ func UrlGetBody(url string) []byte {
|
||||
fmt.Println("Error creating request:", err)
|
||||
|
||||
}
|
||||
req.Header.Set("Authorization", "token "+config.GetToken())
|
||||
req.Header.Set("Authorization", "token "+CfgGlobal.GetTokenEnv())
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// fmt.Println(req.Header.Get("Authorization"))
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
Reference in New Issue
Block a user