git

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2023 License: MIT Imports: 9 Imported by: 0

README

Git

A Git package for Go. Handles Git operations and file synchronization with a remote repository.

tag Go Version GoDoc Lint Scan Build Status Go Report Card Contributors License

Usage

go get github.com/zcubbs/git

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneRepository

func CloneRepository(repoURL, destination string, auth *gitHttp.BasicAuth) error

CloneRepository clones a Git repository into a given directory with optional authentication.

func ExecuteCmdWithOutput

func ExecuteCmdWithOutput(command string, args ...string) (string, error)

ExecuteCmdWithOutput executes a command and returns the output

func FileHasChanges

func FileHasChanges(repoPath, file, lastCommit, currentCommit string) (bool, error)

func GetLatestCommit

func GetLatestCommit(gitRepoPath string) (string, error)

func PullRepository

func PullRepository(repoPath string, auth *gitHttp.BasicAuth) (bool, error)

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

type FileSyncMapping struct {
	Source      string
	Destination string
}

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

type Manager struct {
	Auth *gitHttp.BasicAuth
}

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

func NewGitOpsManager(username, password string) *Manager

NewGitOpsManager creates a new instance of GitOpsManager with optional authentication.

func (*Manager) CloneRepository

func (m *Manager) CloneRepository(repoURL, destination string) error

CloneRepository clones a Git repository into a given directory.

func (*Manager) PullRepository

func (m *Manager) PullRepository(repoPath string) (bool, error)

PullRepository updates the local copy of a Git repository and returns true if there were changes.

func (*Manager) SyncFiles

func (m *Manager) SyncFiles(repoPath string, mappings []FileSyncMapping) error

SyncFiles synchronizes files from the repository to the local file system based on the provided mappings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL