Debian Codegraph @

example/newrepo/main.go

raw
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/v60/github"
20 )
21
22 var (
23 name = flag.String("name", "", "Name of repo to create in authenticated user's GitHub account.")
24 description = flag.String("description", "", "Description of created repo.")
25 private = flag.Bool("private", false, "Will created repo be private.")
26 autoInit = flag.Bool("auto-init", false, "Pass true to create an initial commit with empty README.")
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 client := github.NewClient(nil).WithAuthToken(token)
40
41 r := &github.Repository{Name: name, Private: private, Description: description, AutoInit: autoInit}
42 repo, _, err := client.Repositories.Create(ctx, "", r)
43 if err != nil {
44 log.Fatal(err)
45 }
46 fmt.Printf("Successfully created new repo: %v\n", repo.GetName())
47 }