...

Source file src/github.com/google/go-github/v33/example/newrepo/main.go

Documentation: github.com/google/go-github/v33/example/newrepo

     1  // Copyright 2018 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // The newrepo command utilizes go-github as a cli tool for
     7  // creating new repositories. It takes an auth token as
     8  // an environment variable and creates the new repo under
     9  // the account affiliated with that token.
    10  package main
    11  
    12  import (
    13  	"context"
    14  	"flag"
    15  	"fmt"
    16  	"log"
    17  	"os"
    18  
    19  	"github.com/google/go-github/v33/github"
    20  	"golang.org/x/oauth2"
    21  )
    22  
    23  var (
    24  	name        = flag.String("name", "", "Name of repo to create in authenticated user's GitHub account.")
    25  	description = flag.String("description", "", "Description of created repo.")
    26  	private     = flag.Bool("private", false, "Will created repo be private.")
    27  )
    28  
    29  func main() {
    30  	flag.Parse()
    31  	token := os.Getenv("GITHUB_AUTH_TOKEN")
    32  	if token == "" {
    33  		log.Fatal("Unauthorized: No token present")
    34  	}
    35  	if *name == "" {
    36  		log.Fatal("No name: New repos must be given a name")
    37  	}
    38  	ctx := context.Background()
    39  	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
    40  	tc := oauth2.NewClient(ctx, ts)
    41  	client := github.NewClient(tc)
    42  
    43  	r := &github.Repository{Name: name, Private: private, Description: description}
    44  	repo, _, err := client.Repositories.Create(ctx, "", r)
    45  	if err != nil {
    46  		log.Fatal(err)
    47  	}
    48  	fmt.Printf("Successfully created new repo: %v\n", repo.GetName())
    49  }
    50  

View as plain text