Skip to content
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
71 changes: 70 additions & 1 deletion pkg/cloudprovider/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"

"github.com/aws/aws-sdk-go-v2/aws"

awscfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
Expand All @@ -20,24 +19,54 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sqs"
)

// AWSAdapter defines an interface for creating various AWS service clients.
type AWSAdapter interface {
// NewSNS creates a new Simple Notification Service (SNS) client.
NewSNS() *sns.Client

// NewSQS creates a new Simple Queue Service (SQS) client.
NewSQS() *sqs.Client

// NewS3 creates a new Simple Storage Service (S3) client.
NewS3() *s3.Client

// NewRDS creates a new Relational Database Service (RDS) client.
NewRDS() *rds.Client

// NewEC2 creates a new Elastic Compute Cloud (EC2) client.
NewEC2() *ec2.Client

// NewIAM creates a new Identity and Access Management (IAM) client.
NewIAM() *iam.Client

// NewDynamoDB creates a new DynamoDB client.
NewDynamoDB() *dynamodb.Client

// NewAutoScaling creates a new Auto Scaling client.
NewAutoScaling() *autoscaling.Client

// NewECS creates a new Elastic Container Service (ECS) client.
NewECS() *ecs.Client

// NewEKS creates a new Elastic Kubernetes Service (EKS) client.
NewEKS() *eks.Client
}

// AWS implements the AWSAdapter interface and holds the configuration for AWS services.
type AWS struct {
Region string
cfg aws.Config
}

// NewAWS creates a new instance of AWS with the specified region.
// It requires AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables to be set.
//
// Parameters:
// - region: The AWS region to use.
//
// Returns:
// - AWSAdapter: An interface for creating AWS service clients.
// - error: An error if the AWS configuration could not be loaded.
func NewAWS(region string) (AWSAdapter, error) {
if os.Getenv("AWS_ACCESS_KEY_ID") == "" || os.Getenv("AWS_SECRET_ACCESS_KEY") == "" {
return nil, fmt.Errorf("AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be set")
Expand All @@ -54,42 +83,82 @@ func NewAWS(region string) (AWSAdapter, error) {
return &AWS{Region: region, cfg: cfg}, nil
}

// NewSNS creates a new Simple Notification Service (SNS) client.
//
// Returns:
// - *sns.Client: A new SNS client.
func (a *AWS) NewSNS() *sns.Client {
return sns.NewFromConfig(a.cfg)
}

// NewSQS creates a new Simple Queue Service (SQS) client.
//
// Returns:
// - *sqs.Client: A new SQS client.
func (a *AWS) NewSQS() *sqs.Client {
return sqs.NewFromConfig(a.cfg)
}

// NewS3 creates a new Simple Storage Service (S3) client.
//
// Returns:
// - *s3.Client: A new S3 client.
func (a *AWS) NewS3() *s3.Client {
return s3.NewFromConfig(a.cfg)
}

// NewRDS creates a new Relational Database Service (RDS) client.
//
// Returns:
// - *rds.Client: A new RDS client.
func (a *AWS) NewRDS() *rds.Client {
return rds.NewFromConfig(a.cfg)
}

// NewEC2 creates a new Elastic Compute Cloud (EC2) client.
//
// Returns:
// - *ec2.Client: A new EC2 client.
func (a *AWS) NewEC2() *ec2.Client {
return ec2.NewFromConfig(a.cfg)
}

// NewIAM creates a new Identity and Access Management (IAM) client.
//
// Returns:
// - *iam.Client: A new IAM client.
func (a *AWS) NewIAM() *iam.Client {
return iam.NewFromConfig(a.cfg)
}

// NewDynamoDB creates a new DynamoDB client.
//
// Returns:
// - *dynamodb.Client: A new DynamoDB client.
func (a *AWS) NewDynamoDB() *dynamodb.Client {
return dynamodb.NewFromConfig(a.cfg)
}

// NewAutoScaling creates a new Auto Scaling client.
//
// Returns:
// - *autoscaling.Client: A new Auto Scaling client.
func (a *AWS) NewAutoScaling() *autoscaling.Client {
return autoscaling.NewFromConfig(a.cfg)
}

// NewECS creates a new Elastic Container Service (ECS) client.
//
// Returns:
// - *ecs.Client: A new ECS client.
func (a *AWS) NewECS() *ecs.Client {
return ecs.NewFromConfig(a.cfg)
}

// NewEKS creates a new Elastic Kubernetes Service (EKS) client.
//
// Returns:
// - *eks.Client: A new EKS client.
func (a *AWS) NewEKS() *eks.Client {
return eks.NewFromConfig(a.cfg)
}
41 changes: 37 additions & 4 deletions pkg/gh/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,26 @@ import (
"golang.org/x/oauth2"
)

// FetchReleases fetches GitHub releases for a given repo URL.
// If `onlyLatest` is true, it returns the most recent release.
// Otherwise, it returns all releases.
// FetchReleases fetches GitHub releases for a given repository URL.
// If `onlyLatest` is true, it returns the most recent release. Otherwise, it returns all releases.
//
// Parameters:
// - repoURL: The URL of the GitHub repository (e.g., "https://github.com/owner/repo").
// - onlyLatest: A boolean flag indicating whether to fetch only the latest release.
//
// Returns:
// - []*github.RepositoryRelease: A slice of GitHub repository releases.
// - error: An error if the releases could not be fetched.
//
// Example:
//
// releases, err := FetchReleases("https://github.com/owner/repo", true)
// if err != nil {
// log.Fatalf("Error fetching releases: %v", err)
// }
// for _, release := range releases {
// fmt.Printf("Release: %s\n", *release.TagName)
// }
func FetchReleases(repoURL string, onlyLatest bool) ([]*github.RepositoryRelease, error) {
ctx := context.Background()

Expand Down Expand Up @@ -44,7 +61,23 @@ func FetchReleases(repoURL string, onlyLatest bool) ([]*github.RepositoryRelease
return releases, nil
}

// GetOwnerAndRepoFromURL extracts the owner and repo name from a GitHub URL
// GetOwnerAndRepoFromURL extracts the owner and repository name from a GitHub URL.
//
// Parameters:
// - repoURL: The URL of the GitHub repository (e.g., "https://github.com/owner/repo").
//
// Returns:
// - owner: The owner of the repository.
// - repo: The name of the repository.
// - err: An error if the URL could not be parsed or is in an invalid format.
//
// Example:
//
// owner, repo, err := GetOwnerAndRepoFromURL("https://github.com/owner/repo")
// if err != nil {
// log.Fatalf("Error parsing URL: %v", err)
// }
// fmt.Printf("Owner: %s, Repo: %s\n", owner, repo)
func GetOwnerAndRepoFromURL(repoURL string) (owner, repo string, err error) {
u, err := url.Parse(repoURL)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions pkg/git_tools/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import (
"strings"
)

// FindGitRepoRootUsingGit finds the root of a Git repository using the `git` command.
//
// Parameters:
// - dir: The directory to start the search from.
//
// Returns:
// - string: The absolute path to the root of the Git repository.
// - error: An error if the Git repository root could not be found.
//
// Example:
//
// root, err := FindGitRepoRootUsingGit("/path/to/start")
// if err != nil {
// log.Fatalf("Error finding Git repo root: %v", err)
// }
// fmt.Printf("Git repository root: %s\n", root)
func FindGitRepoRootUsingGit(dir string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Dir = dir
Expand All @@ -19,6 +35,21 @@ func FindGitRepoRootUsingGit(dir string) (string, error) {
}

// FindGitRepoRootByTraversal finds the Git repository root for the given directory by manually checking for a .git directory.
//
// Parameters:
// - dir: The directory to start the search from.
//
// Returns:
// - string: The absolute path to the root of the Git repository.
// - error: An error if the Git repository root could not be found.
//
// Example:
//
// root, err := FindGitRepoRootByTraversal("/path/to/start")
// if err != nil {
// log.Fatalf("Error finding Git repo root: %v", err)
// }
// fmt.Printf("Git repository root: %s\n", root)
func FindGitRepoRootByTraversal(dir string) (string, error) {
currentPath, err := filepath.Abs(dir)
if err != nil {
Expand Down
22 changes: 20 additions & 2 deletions pkg/git_tools/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@ package git_tools

import (
"fmt"
"github.com/Excoriate/tftest/pkg/utils"
"os"
"path/filepath"

"github.com/Excoriate/tftest/pkg/utils"
)

// IsAGitRepository checks if the given directory or any of its parent directories up to `levels` is a git repository.
// IsAGitRepository checks if the given directory or any of its parent directories up to `levels` is a Git repository.
// It returns the git root directory, the subdirectory passed relative to the git root, and any error encountered.
//
// Parameters:
// - repoRoot: The directory to start the search from.
// - levels: The number of parent directories to check upwards.
//
// Returns:
// - gitRoot: The absolute path to the root of the Git repository.
// - subDir: The relative path from the Git root to the original directory.
// - err: An error if the Git repository root could not be found or if any other error occurred.
//
// Example:
//
// gitRoot, subDir, err := IsAGitRepository("/path/to/start", 5)
// if err != nil {
// log.Fatalf("Error finding Git repository: %v", err)
// }
// fmt.Printf("Git repository root: %s, Subdirectory: %s\n", gitRoot, subDir)
func IsAGitRepository(repoRoot string, levels int) (gitRoot, subDir string, err error) {
if repoRoot == "" {
return "", "", fmt.Errorf("directory path cannot be empty")
Expand Down
Loading