139 lines
3.8 KiB
Go
139 lines
3.8 KiB
Go
package cmdLists
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"kode-creator/config"
|
|
"kode-creator/utils"
|
|
"net/http"
|
|
|
|
"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
|
|
// command. The `Run` field is a function that is executed when the command is run. It sets some
|
|
// configuration information based on the command line arguments, and then either lists the projects
|
|
// for a specified organization or lists all organizations, depending on the command line arguments.
|
|
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) {
|
|
|
|
// 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 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 := ListGitTeaProjects(CfgGlobal.GetListOrganization())
|
|
if err != nil {
|
|
fmt.Println("Error:", err)
|
|
return
|
|
}
|
|
for _, project := range projects {
|
|
fmt.Println(project)
|
|
// fmt.Print(project)
|
|
}
|
|
fmt.Println(utils.CreateLine(8))
|
|
|
|
} else {
|
|
orgs, err := ListGitTeaOrganization()
|
|
if err != nil {
|
|
fmt.Println("Error:", err)
|
|
return
|
|
}
|
|
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(), CfgGlobal.GetGithubTokenEnv())
|
|
if err != nil {
|
|
fmt.Println("Error:", err)
|
|
return
|
|
}
|
|
nb := 1
|
|
for _, repo := range repos {
|
|
utils.PrintTreeElement(nb, *repo.Name, *repo.HTMLURL)
|
|
nb++
|
|
}
|
|
|
|
} else {
|
|
|
|
// print help if no flag is set
|
|
cmd.Help()
|
|
fmt.Println(utils.CreateLine(11))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This function sends a GET request to a specified URL with authorization and content-type headers,
|
|
// and returns the response body as a byte array.
|
|
func UrlGetBody(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 "+token)
|
|
req.Header.Set("Content-Type", "application/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 := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error reading response body:", err)
|
|
|
|
}
|
|
return body
|
|
}
|