2023-06-21 11:50:12 -04:00
|
|
|
package cmdLists
|
2023-06-12 22:40:22 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-06-17 14:17:02 -04:00
|
|
|
"io"
|
2023-06-30 15:25:39 -04:00
|
|
|
"kode-creator/config"
|
|
|
|
"kode-creator/utils"
|
2023-06-12 22:40:22 -04:00
|
|
|
"net/http"
|
|
|
|
|
2023-11-29 12:57:49 -05:00
|
|
|
"github.com/gookit/color"
|
2023-06-12 22:40:22 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
// 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.
|
2023-06-12 22:40:22 -04:00
|
|
|
var ListCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
2023-07-08 00:45:18 -04:00
|
|
|
Short: "LIST Github, Gitea orgs, project",
|
2023-06-12 22:40:22 -04:00
|
|
|
Long: `A simple CLI app to list Github, Gitea orgs, project`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2023-07-08 00:45:18 -04:00
|
|
|
// if Gitea flag is set
|
2023-06-30 10:09:01 -04:00
|
|
|
if CfgGlobal.GetTeaFlag() {
|
2023-06-12 22:40:22 -04:00
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
// if -o flag is set, list projects for that org
|
2023-06-30 10:09:01 -04:00
|
|
|
if CfgGlobal.GetListOrganizationFlag() {
|
|
|
|
if !CfgGlobal.GetJsonFlag() {
|
2023-11-29 12:57:49 -05:00
|
|
|
color.Green.Println("\n L I S T P R O J E C T S F O R O R G. ")
|
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
|
|
|
color.Green.Println(" ==> " + CfgGlobal.GetListOrganization() + " <== ")
|
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
2023-06-17 22:49:37 -04:00
|
|
|
}
|
2023-06-30 10:09:01 -04:00
|
|
|
projects, err := ListGitTeaProjects(CfgGlobal.GetListOrganization())
|
2023-06-17 14:17:02 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, project := range projects {
|
2023-11-29 12:57:49 -05:00
|
|
|
fmt.Print(project)
|
2023-06-17 14:17:02 -04:00
|
|
|
}
|
2023-11-29 12:57:49 -05:00
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
2023-06-30 10:09:01 -04:00
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
} else {
|
2023-06-30 10:09:01 -04:00
|
|
|
orgs, err := ListGitTeaOrganization()
|
2023-06-17 14:17:02 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
return
|
|
|
|
}
|
2023-06-30 10:09:01 -04:00
|
|
|
if !CfgGlobal.GetJsonFlag() {
|
2023-11-29 12:57:49 -05:00
|
|
|
color.Green.Println("\n L I S T O R G A N I Z A T I O N S")
|
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
2023-06-17 22:49:37 -04:00
|
|
|
}
|
2023-06-17 14:17:02 -04:00
|
|
|
for _, org := range orgs {
|
2023-06-30 10:09:01 -04:00
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
fmt.Print(org)
|
|
|
|
}
|
2023-11-29 12:57:49 -05:00
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
2023-06-12 22:40:22 -04:00
|
|
|
}
|
2023-07-08 00:45:18 -04:00
|
|
|
|
|
|
|
// if Github flag is set
|
2023-06-30 10:09:01 -04:00
|
|
|
} else if CfgGlobal.GetGitFlag() {
|
2023-07-08 00:45:18 -04:00
|
|
|
|
|
|
|
// if user flag is set, list projects for that user
|
|
|
|
if CfgGlobal.GetListUserFlag() {
|
|
|
|
|
2023-11-29 12:57:49 -05:00
|
|
|
color.Green.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())
|
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
2023-07-08 00:45:18 -04:00
|
|
|
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 if CfgGlobal.GetGitHubSearchFlag() {
|
|
|
|
|
2023-11-29 12:57:49 -05:00
|
|
|
color.Green.Println("\n L I S T G I T H U B P R O J E C T S F R O M S E A R C H : " + CfgGlobal.GetListCmd().GitHubSearch)
|
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
2023-07-08 00:45:18 -04:00
|
|
|
repos, err := SearchGitHubProjectsfromSearch(CfgGlobal.GetListCmd().GitHubSearch, CfgGlobal.GetGithubTokenEnv())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nb := 1
|
|
|
|
for _, repo := range repos {
|
|
|
|
utils.PrintTreeElement(nb+1, *repo.Name, *repo.HTMLURL)
|
|
|
|
// nb++
|
|
|
|
}
|
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
}
|
2023-06-17 14:17:02 -04:00
|
|
|
|
2023-07-08 00:45:18 -04:00
|
|
|
// for the rest of the cases print help
|
2023-06-30 10:09:01 -04:00
|
|
|
} else {
|
2023-06-12 22:40:22 -04:00
|
|
|
|
2023-06-30 10:09:01 -04:00
|
|
|
// print help if no flag is set
|
|
|
|
cmd.Help()
|
2023-11-29 12:57:49 -05:00
|
|
|
color.Yellow.Println(utils.CreateLine(15))
|
2023-06-12 22:40:22 -04:00
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
}
|
2023-06-30 10:09:01 -04:00
|
|
|
|
2023-06-12 22:40:22 -04:00
|
|
|
}
|
|
|
|
|
2023-06-17 14:17:02 -04:00
|
|
|
// 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.
|
2023-06-30 15:25:39 -04:00
|
|
|
func UrlGetBody(url string, token string) []byte {
|
2023-06-12 22:40:22 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating request:", err)
|
|
|
|
|
|
|
|
}
|
2023-06-30 15:25:39 -04:00
|
|
|
req.Header.Set("Authorization", "token "+token)
|
2023-06-12 22:40:22 -04:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2023-06-17 14:17:02 -04:00
|
|
|
// fmt.Println(req.Header.Get("Authorization"))
|
2023-06-12 22:40:22 -04:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error making request:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2023-06-17 14:17:02 -04:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2023-06-12 22:40:22 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading response body:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
return body
|
|
|
|
}
|