go-KodeStarter/cmdLists/lists.go

139 lines
3.8 KiB
Go
Raw Normal View History

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"
"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",
Short: "List Github, Gitea orgs, project",
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()
}
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() {
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))
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-06-30 10:09:01 -04:00
fmt.Println(project)
// fmt.Print(project)
2023-06-17 14:17:02 -04:00
}
2023-06-30 10:09:01 -04:00
fmt.Println(utils.CreateLine(8))
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() {
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))
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-06-30 10:09:01 -04:00
fmt.Println(utils.CreateLine(8))
2023-06-12 22:40:22 -04:00
}
2023-06-30 10:09:01 -04:00
} 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))
2023-06-30 15:25:39 -04:00
repos, err := ListGitHubProjectsfromUser(CfgGlobal.GetListUser(), CfgGlobal.GetGithubTokenEnv())
2023-06-12 22:40:22 -04:00
if err != nil {
2023-06-30 10:09:01 -04:00
fmt.Println("Error:", err)
return
2023-06-12 22:40:22 -04:00
}
2023-06-30 10:09:01 -04:00
nb := 1
for _, repo := range repos {
utils.PrintTreeElement(nb, *repo.Name, *repo.HTMLURL)
nb++
}
2023-06-17 14:17:02 -04:00
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-06-30 15:25:39 -04:00
fmt.Println(utils.CreateLine(11))
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
}