package cmdLists import ( "fmt" "io" "kode-creator/config" "kode-creator/utils" "net/http" "github.com/gookit/color" "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 Gitea flag is set if CfgGlobal.GetTeaFlag() { // if -o flag is set, list projects for that org if CfgGlobal.GetListOrganizationFlag() { if !CfgGlobal.GetJsonFlag() { 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)) } projects, err := ListGitTeaProjects(CfgGlobal.GetListOrganization()) if err != nil { fmt.Println("Error:", err) return } for _, project := range projects { fmt.Print(project) } color.Yellow.Println(utils.CreateLine(15)) } else { orgs, err := ListGitTeaOrganization() if err != nil { fmt.Println("Error:", err) return } if !CfgGlobal.GetJsonFlag() { 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)) } for _, org := range orgs { fmt.Print(org) } color.Yellow.Println(utils.CreateLine(15)) } // if Github flag is set } else if CfgGlobal.GetGitFlag() { // if user flag is set, list projects for that user if CfgGlobal.GetListUserFlag() { 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)) 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() { 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)) 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++ } } // for the rest of the cases print help } else { // print help if no flag is set cmd.Help() color.Yellow.Println(utils.CreateLine(15)) } } // 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 }