change linux

This commit is contained in:
2023-07-08 00:45:18 -04:00
parent 71fe930228
commit 7e2e5c85f5
10 changed files with 252 additions and 125 deletions
+40 -1
View File
@@ -18,7 +18,7 @@ func ListGitHubOrganization() ([]string, error) {
func ListGitHubProjectsfromUser(username string, token string) ([]*github.Repository, error) {
// Replace with your access token
// token := "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"
//token = "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"
// Create an oauth2 token source with the access token
tokenSource := oauth2.StaticTokenSource(
@@ -40,3 +40,42 @@ func ListGitHubProjectsfromUser(username string, token string) ([]*github.Reposi
return repos, nil
}
// function that return a list of github projects base on a search string
func SearchGitHubProjectsfromSearch(search string, token string) ([]*github.Repository, error) {
// Create client
tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client := github.NewClient(tc)
// Search repositories
results, _, err := client.Search.Repositories(context.Background(), search, &github.SearchOptions{})
if err != nil {
return nil, err
}
// Return list of repositories
var repos []*github.Repository
// for _, repo := range results.Repositories {
// repos = append(repos, &repo)
// }
fmt.Printf("Got %d results from GitHub\n", len(results.Repositories))
for _, repo := range results.Repositories {
fmt.Printf("%s %s\n", *repo.Name, *repo.HTMLURL)
repos = append(repos, &repo)
}
fmt.Printf("Appended %d repos to slice\n", len(repos))
for i, repo := range repos {
fmt.Printf("%d %s %s\n", i, *repo.Name, *repo.HTMLURL)
}
fmt.Println("Number of repos:", len(repos))
fmt.Println("Number of results:", *results.Total)
return repos, nil
}
+36 -12
View File
@@ -26,7 +26,7 @@ func InitConfig(config *config.Config) {
// 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",
Short: "LIST Github, Gitea orgs, project",
Long: `A simple CLI app to list Github, Gitea orgs, project`,
Run: func(cmd *cobra.Command, args []string) {
@@ -49,6 +49,7 @@ func startList(cmd *cobra.Command, args []string) {
utils.PrintHeader()
}
// if Gitea flag is set
if CfgGlobal.GetTeaFlag() {
// if -o flag is set, list projects for that org
@@ -86,20 +87,43 @@ func startList(cmd *cobra.Command, args []string) {
}
fmt.Println(utils.CreateLine(8))
}
// if Github flag is set
} 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++
// if user flag is set, list projects for that user
if CfgGlobal.GetListUserFlag() {
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 if CfgGlobal.GetGitHubSearchFlag() {
fmt.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)
fmt.Println(utils.CreateLine(11))
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