go-KodeStarter/cmdLists/gitTea.go

73 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-06-30 10:09:01 -04:00
package cmdLists
import (
"encoding/json"
"fmt"
2023-06-30 15:25:39 -04:00
"kode-creator/utils"
2023-06-30 10:09:01 -04:00
"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()
2023-06-30 15:25:39 -04:00
body := UrlGetBody(CfgGlobal.GetUrlApiOrgs(), CfgGlobal.GetGitTeaTokenEnv())
2023-06-30 10:09:01 -04:00
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()
2023-06-30 15:25:39 -04:00
body := UrlGetBody(CfgGlobal.GetUrlApiOrgs()+"/"+org+"/repos", CfgGlobal.GetGitTeaTokenEnv())
2023-06-30 10:09:01 -04:00
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
}
}
}