Skip to content

Commit

Permalink
refactor to multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmainguy committed Oct 27, 2021
1 parent c4961ea commit dfb4e68
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 193 deletions.
50 changes: 50 additions & 0 deletions files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)

func copyFiles(src, dst string) {
srcFiles := getAllFiles(src)
for _, file := range srcFiles {
destFile := strings.TrimPrefix(file, src)
destFile = dst + "/" + destFile
bytesRead, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(destFile, bytesRead, 0644)

if err != nil {
log.Fatal(err)
}

}
}

// https://golang.cafe/blog/how-to-list-files-in-a-directory-in-go.html
func getAllFiles(dirPath string) (files []string) {
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}

if !info.IsDir() {
// Don't list .git/ files
if !strings.Contains(path, ".git/") && !strings.Contains(path, ".template/") {
files = append(files, path)
}
}
return nil
})
if err != nil {
fmt.Println(err)
}
return files
}
138 changes: 138 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package main

import (
"context"
"fmt"
"os"
"strconv"
"strings"

git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-github/v39/github"
)

func pushBranch(ctx context.Context, newBranchName string, currentRepo *git.Repository) {
// Create Worktree
wt, err := currentRepo.Worktree()
if err != nil {
panic(err)
}
// Check worktree status
wtStatus, err := wt.Status()
if err != nil {
panic(err)
}
// If not clean, new branch, add, commit, push
if !wtStatus.IsClean() {
// Checkout new branch
err := wt.Checkout(&git.CheckoutOptions{
Create: true,
Keep: true,
Force: false,
Branch: plumbing.NewBranchReferenceName(newBranchName),
})
if err != nil {
panic(err)
}
// git add .
_, err = wt.Add(".")
if err != nil {
panic(err)
}
// git commit -m
_, err = wt.Commit("GGTH template updating", &git.CommitOptions{})
if err != nil {
panic(err)
}
// git push origin
err = currentRepo.PushContext(ctx, &git.PushOptions{
RemoteName: "origin",
})
if err != nil {
panic(err)
}

}

}

func getNewBranchName(branches []*github.Branch) string {
var pullRequestIncrement int
for _, branch := range branches {
if strings.HasPrefix(*branch.Name, "ggth-") {
incrementStr := strings.TrimPrefix(*branch.Name, "ggth-")
increment, err := strconv.Atoi(incrementStr)
if err != nil {
panic(err)
}

if increment >= pullRequestIncrement {
pullRequestIncrement = increment + 1
}

}
}
newBranchName := fmt.Sprintf("ggth-%d", pullRequestIncrement)
return newBranchName
}

func cloneRepo(repoDir string, templateRepo *github.Repository) {
if templateRepo != nil {
if _, err := os.Stat(repoDir); !os.IsNotExist(err) {
// Delete it, and clone it fresh
err = os.RemoveAll(repoDir)
if err != nil {
panic(err)
}
}
_, err := git.PlainClone(repoDir, false, &git.CloneOptions{
URL: *templateRepo.CloneURL,
Progress: nil,
})
if err != nil {
panic(err)
}

} else {
fmt.Println("Templated repo could not be found")
os.Exit(1)
}
}

func getRepoNameFromGitConfig(currentRepo *git.Repository) (owner, name string) {
currentRepoConfig, err := currentRepo.Config()
var originURL string
if err != nil {
panic(err)
}
for _, remote := range currentRepoConfig.Remotes {
if remote.Name == "origin" {
for _, URL := range remote.URLs {
originURL = URL
}
}
}

// SSH style
repoName := strings.ReplaceAll(originURL, "git@github.com:", "")
repoName = strings.ReplaceAll(repoName, ".git", "")
// HTTPS style
repoName = strings.ReplaceAll(repoName, "https://github.com/", "")
ownerRepo := strings.Split(repoName, "/")
owner = strings.ToLower(ownerRepo[0])
name = strings.ToLower(ownerRepo[1])

return owner, name
}

func getGitProfile() (gitName, gitEmail string) {
gitConfig, err := config.LoadConfig(config.GlobalScope)
if err != nil {
panic(err)
}
gitName = gitConfig.User.Name
gitEmail = gitConfig.User.Email
return gitName, gitEmail
}

0 comments on commit dfb4e68

Please sign in to comment.