Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create repository with an initial commit with empty README
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2018 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// The newrepo command utilizes go-github as a cli tool for | ||
// creating new repositories. It takes an auth token as | ||
// an environment variable and creates the new repo under | ||
// the account affiliated with that token. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/google/go-github/v43/github" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var ( | ||
name = flag.String("name", "", "Name of repo to create in authenticated user's GitHub account.") | ||
description = flag.String("description", "", "Description of created repo.") | ||
private = flag.Bool("private", false, "Will created repo be private.") | ||
autoInit = flag.Bool("auto-init", false, "Pass true to create an initial commit with empty README.") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
token := os.Getenv("GITHUB_AUTH_TOKEN") | ||
if token == "" { | ||
log.Fatal("Unauthorized: No token present") | ||
} | ||
if *name == "" { | ||
log.Fatal("No name: New repos must be given a name") | ||
} | ||
ctx := context.Background() | ||
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) | ||
tc := oauth2.NewClient(ctx, ts) | ||
client := github.NewClient(tc) | ||
|
||
r := &github.Repository{Name: name, Private: private, Description: description,AutoInit: autoInit} | ||
repo, _, err := client.Repositories.Create(ctx, "", r) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("Successfully created new repo: %v\n", repo.GetName()) | ||
} |