Skip to content

Commit

Permalink
Create repo from template
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmainguy committed Oct 28, 2021
1 parent 86a4294 commit 721878c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,33 @@ Golang Github Template Helper

## Usage
```/bin/bash
ggth
Usage of ./ggth:
-description string
Description of repo you wish to create
-githubOwner string
Github owner
-githubRepo string
Github repo to source, owned by githubOwner
-templateName string
Name of template, if you wish to create a new repo
-templateOwner string
Owner of template, if you wish to create a new repo
```

If -templateName, and -templateOwner are defined, it will attempt to create the repo on Github from this template.

-githubOwner and -githubRepo must be defined for it to know where to put it.

-description should be defined if you want the repo to have a description

## Env Variables
```/bin/bash
export ghToken=ghp_FAKETOKEN
export ghUsername=Jmainguy
```

You should export these variables for it to be able to auth to github.

## PreBuilt Binaries
Grab Binaries from [The Releases Page](https://github.com/Jmainguy/ggth/releases)

Expand Down
43 changes: 39 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"flag"
"time"

git "github.com/go-git/go-git/v5"
"github.com/google/go-github/v39/github"
Expand All @@ -15,7 +16,10 @@ import (
func main() {
githubOwnerPtr := flag.String("githubOwner", "", "Github owner")
githubRepoPtr := flag.String("githubRepo", "", "Github repo to source, owned by githubOwner")
//create
templateOwnerPtr := flag.String("templateOwner", "", "Owner of template, if you wish to create a new repo")
templateNamePtr := flag.String("templateName", "", "Name of template, if you wish to create a new repo")
descriptionPtr := flag.String("description", "", "Description of repo you wish to create")

flag.Parse()

githubUsername := os.Getenv("ghUsername")
Expand All @@ -37,12 +41,32 @@ func main() {
if *githubOwnerPtr != "" {
if *githubRepoPtr != "" {
workDir = "/tmp/source/"
// Clone template repo to a temporary directory
// Check if Repo exists

remoteSourceRepo, _, err := client.Repositories.Get(ctx, githubOwner, githubRepo)
if err != nil {
panic(err)
// Create Repo if asked to
if *templateOwnerPtr != "" {
if *templateNamePtr != "" {
templateRepoRequest := &github.TemplateRepoRequest{
Name: githubRepoPtr,
Owner: githubOwnerPtr,
Description: descriptionPtr,
Private: github.Bool(false),
}
clonedRemoteSourceRepo, _, err := client.Repositories.CreateFromTemplate(ctx, *templateOwnerPtr, *templateNamePtr, templateRepoRequest)
if err != nil {
panic(err)
}
remoteSourceRepo = clonedRemoteSourceRepo
} else {
panic(err)
}
}
}

// Clone template repo to a temporary directory
// Takes time from repo creation, to actually be able to clone it
time.Sleep(time.Second * 5)
cloneRepo(remoteSourceRepo, workDir, githubUsername, githubPassword)
sourceRepo = remoteSourceRepo
}
Expand All @@ -66,6 +90,17 @@ func main() {
}
// Retrieve templateRepo from currentRepo so we can clone it
templateRepo := sourceRepo.GetTemplateRepository()
if templateRepo == nil {
for templateRepo == nil {
fmt.Println("Waiting for repo get its template source updated")
sourceRepo, _, err = client.Repositories.Get(ctx, githubOwner, githubRepo)
if err != nil {
panic(err)
}
time.Sleep(time.Second * 1)
templateRepo = sourceRepo.GetTemplateRepository()
}
}
// Clone template repo to a temporary directory
tempTemplateDir := "/tmp/foo/"
cloneRepo(templateRepo, tempTemplateDir, githubUsername, githubPassword)
Expand Down

0 comments on commit 721878c

Please sign in to comment.