Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add status checks to the cli #4

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pre-commit-config.yaml → .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- id: detect-private-key
- id: detect-private-key
24 changes: 24 additions & 0 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package list

import (
orgs "gh_foundations/cmd/list/orgs"
repos "gh_foundations/cmd/list/repos"

"github.com/spf13/cobra"
)

var ListCmd = &cobra.Command{
Use: "list",
Short: "list managed resources",
Long: `list various resources managed by the tool.\n
Currently supported resources are:\n\n

- repos\n
- orgs\n\n`,
}

func init() {
ListCmd.AddCommand(orgs.OrgsCmd)
ListCmd.AddCommand(repos.ReposCmd)

}
57 changes: 57 additions & 0 deletions cmd/list/orgs/orgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package list

import (
"errors"
"fmt"
"gh_foundations/internal/pkg/functions"
"os"

"github.com/spf13/cobra"
)

var OrgsCmd = &cobra.Command{
Use: "orgs",
Short: "List managed organizations.",
Long: `List managed organizations. This command will list all organizations.\n
found in the state file.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires the path of the \"organizations\" directory")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {

orgsDir := args[0]

orgs, err := functions.FindManagedOrgs(orgsDir)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

var orgOut string
orgOut = ""
for _, org := range orgs {
if orgOut != "" {
orgOut = fmt.Sprintf("%s, '%s'", orgOut, org)
} else {
orgOut = fmt.Sprintf("['%s'", org)
}
}
orgOut = fmt.Sprintf("%s]", orgOut)

fmt.Println(orgOut)

},
}

func init() {
os.Setenv("GCP_SECRET_MANAGER_PROJECT", "blahblah")
os.Setenv("GCP_TF_STATE_BUCKET_PROJECT", "blahblahblah")
os.Setenv("GCP_TF_STATE_BUCKET_NAME", "blahblahblahblah")
os.Setenv("GCP_TF_STATE_BUCKET_LOCATION", "blahblahblahblahblah")
}
83 changes: 83 additions & 0 deletions cmd/list/repos/repos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package list

import (
"errors"
"fmt"
"gh_foundations/internal/pkg/functions"
"gh_foundations/internal/pkg/types/status"
"log"
"os"

"github.com/spf13/cobra"
)

var ghas bool

var ReposCmd = &cobra.Command{
Use: "repos",
Short: "List managed repositories.",
Long: `List managed repositories. This command will list all repositories.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires the path of the \"projects\" directory")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {

reposDir := args[0]

ctx := cmd.Context()

orgSet, err := functions.FindManagedRepos(ctx, reposDir)
if err != nil {
log.Fatalf("Error in findManagedRepos: %s", err)
}

if ghas {
orgSet = orgSet.WithGHASEnabled()
log.Printf("Found %d repositories with GHAS enabled\n", len(flattenRepos(orgSet)))
}
var repos string
repos = ""
for _, repo := range flattenRepos(orgSet) {
if repos != "" {
repos = fmt.Sprintf("%s, '%s'", repos, repo)
} else {
repos = fmt.Sprintf("['%s'", repo)
}
}
repos = fmt.Sprintf("%s]", repos)

fmt.Println(repos)
},
}

func init() {
os.Setenv("GCP_SECRET_MANAGER_PROJECT", "blahblah")
os.Setenv("GCP_TF_STATE_BUCKET_PROJECT", "blahblahblah")
os.Setenv("GCP_TF_STATE_BUCKET_NAME", "blahblahblahblah")
os.Setenv("GCP_TF_STATE_BUCKET_LOCATION", "blahblahblahblahblah")

ReposCmd.Flags().BoolVarP(&ghas, "ghas", "g", false, "List repositories with GHAS enabled")
}

// Return only the names of the repositories managed by the tool
func flattenRepos(org status.OrgSet) []string {
var repoNames []string

for org, projects := range org.OrgProjectSets {
for _, repoSet := range projects.RepositorySets {
for _, repo := range repoSet.PrivateRepositories {
repoNames = append(repoNames, fmt.Sprintf("%s/%s", org, repo.Name))
}
for _, repo := range repoSet.PublicRepositories {
repoNames = append(repoNames, fmt.Sprintf("%s/%s", org, repo.Name))
}
}
}
return repoNames
}
12 changes: 5 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"gh_foundations/cmd/check"
"gh_foundations/cmd/gen"
import_cmd "gh_foundations/cmd/import"
"gh_foundations/cmd/list"
"os"

"github.com/spf13/cobra"
Expand All @@ -12,13 +13,9 @@ import (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gh_foundations",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "The GitHub Foundations CLI tool.",
Long: `The GitHub Foundations CLI tool is a tool to manage GitHub resources
and to read the state of the resources managed by the tool.\n`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand Down Expand Up @@ -47,4 +44,5 @@ func init() {
rootCmd.AddCommand(import_cmd.ImportCmd)
rootCmd.AddCommand(gen.GenCmd)
rootCmd.AddCommand(check.CheckCmd)
rootCmd.AddCommand(list.ListCmd)
}
Loading