Documentation
¶
Overview ¶
Package git provides utilities to interact with git repositories.
This package is primarily designed to clone and pull updates from git repositories, specifically with support for optional credentials in the form of username and password from environment variables.
Author: zakaria.elbouwab zcubbs https://github.com/zcubbs
Index ¶
- func CloneRepository(repoURL, destination string, auth *gitHttp.BasicAuth) error
- func ExecuteCmdWithOutput(command string, args ...string) (string, error)
- func FileHasChanges(repoPath, file, lastCommit, currentCommit string) (bool, error)
- func GetLatestCommit(gitRepoPath string) (string, error)
- func PullRepository(repoPath string, auth *gitHttp.BasicAuth) (bool, error)
- func SyncFiles(repoPath string, mappings []FileSyncMapping) error
- type FileSyncMapping
- type Manager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneRepository ¶
CloneRepository clones a Git repository into a given directory with optional authentication.
func ExecuteCmdWithOutput ¶
ExecuteCmdWithOutput executes a command and returns the output
func FileHasChanges ¶
func GetLatestCommit ¶
func PullRepository ¶
PullRepository updates the local copy of a Git repository with optional authentication and returns true if there were changes.
func SyncFiles ¶
func SyncFiles(repoPath string, mappings []FileSyncMapping) error
SyncFiles synchronizes files from the repository to the local file system based on the provided mappings.
Types ¶
type FileSyncMapping ¶
FileSyncMapping defines a mapping of a file from its source location in the repository to a destination path in the local file system.
type Manager ¶
Manager manages Git operations and file synchronization.
Example:
package main
import (
"fmt" "log" "your-module-name/gitops"
)
func main() {
repoURL := "https://github.com/example-user/example-repo.git"
destination := "./example-repo"
// Initialize GitOpsManager with credentials
manager := gitops.NewGitOpsManager("your-username", "your-personal-access-token")
// Clone a repository
if err := manager.CloneRepository(repoURL, destination); err != nil {
log.Fatalf("Clone failed: %s", err)
}
fmt.Println("Repository cloned successfully.")
// Pull the repository
changes, err := manager.PullRepository(destination)
if err != nil {
log.Fatalf("Pull failed: %s", err)
}
if changes {
fmt.Println("Repository updated.")
} else {
fmt.Println("Repository already up to date.")
}
// Define file synchronization mappings
mappings := []gitops.FileSyncMapping{
{Source: "file1.txt", Destination: "/path/to/local/file1.txt"},
// Add more mappings as needed
}
// Check for changes and synchronize files
changes, err := manager.PullRepository(destination)
if err != nil {
log.Fatalf("Pull failed: %s", err)
}
if changes {
fmt.Println("Changes detected, synchronizing files...")
// Synchronize files
if err := manager.SyncFiles(destination, mappings); err != nil {
log.Fatalf("Sync failed: %s", err)
}
fmt.Println("Files synchronized successfully.")
} else {
fmt.Println("No changes detected.")
}
// Additional operations can be added here as needed
}
func NewGitOpsManager ¶
NewGitOpsManager creates a new instance of GitOpsManager with optional authentication.
func (*Manager) CloneRepository ¶
CloneRepository clones a Git repository into a given directory.
func (*Manager) PullRepository ¶
PullRepository updates the local copy of a Git repository and returns true if there were changes.