-
Notifications
You must be signed in to change notification settings - Fork 0
/
tigwen.go
195 lines (152 loc) · 4.09 KB
/
tigwen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bufio"
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/google/go-github/v25/github"
"github.com/kevinburke/ssh_config"
"github.com/mitchellh/go-homedir"
"github.com/mlctrez/tigwen/internal/files"
"golang.org/x/oauth2"
)
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func githubToken() (token string, err error) {
var u *user.User
u, err = user.Current()
if err != nil {
return "", err
}
tokenFile := filepath.Join(u.HomeDir, ".github_token")
tokenBytes, err := ioutil.ReadFile(tokenFile)
if err != nil {
return "", err
}
token = strings.TrimSpace(string(tokenBytes))
if !strings.HasPrefix(token, "ghp_") {
return "", fmt.Errorf(".github_token must be new form and start with ghp_")
}
return token, nil
}
func githubClient() (client *github.Client, err error) {
token, err := githubToken()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(context.TODO(), ts)
client = github.NewClient(tc)
return client, err
}
func getUserAndRepo() (user, repo string, err error) {
if _, ferr := os.Stat("go.mod"); os.IsNotExist(ferr) {
var dir string
if dir, err = os.Getwd(); err != nil {
return
}
repo = filepath.Base(dir)
dir = filepath.Dir(dir)
user = filepath.Base(dir)
dir = filepath.Dir(dir)
if filepath.Base(dir) != "github.com" {
err = errors.New("grandparent of current directory is not github.com")
}
return
}
var file *os.File
file, err = os.Open("go.mod")
if err != nil {
return
}
scanner := bufio.NewScanner(file)
r := regexp.MustCompile(`module github.com/(\w*)/(.*)`)
for scanner.Scan() {
match := r.FindStringSubmatch(scanner.Text())
if len(match) == 3 {
user = match[1]
repo = match[2]
break
}
}
err = scanner.Err()
if user == "" || repo == "" {
err = errors.New("unable to determine github user or repo: check go.mod file")
}
return
}
func main() {
repoPath, err := os.Getwd()
checkErr(err)
if _, err = os.Stat(filepath.Join(repoPath, ".git")); err == nil {
log.Fatal("existing .git directory found")
}
userName, repoName, err := getUserAndRepo()
checkErr(err)
client, err := githubClient()
checkErr(err)
repo := &github.Repository{Name: &repoName}
repo.Name = &repoName
repository, response, err := client.Repositories.Create(context.Background(), "", repo)
checkErr(err)
_ = repository
_ = response
goModule := strings.Join([]string{"github.com", userName, repoName}, "/")
checkErr(files.WriteFile("README.md", map[string]string{"RepoName": repoName, "GoModule": goModule}))
checkErr(files.WriteFile("LICENSE", map[string]string{
"LicenseCopyright": fmt.Sprintf("Copyright %d Matt Crawford", time.Now().Year()),
}))
checkErr(files.WriteFile(".gitignore", nil))
gitRepo, err := git.PlainInit(repoPath, false)
checkErr(err)
worktree, err := gitRepo.Worktree()
checkErr(err)
for _, name := range []string{"README.md", ".gitignore", "LICENSE"} {
_, err = worktree.Add(name)
checkErr(err)
}
_, err = worktree.Commit("initial commit", &git.CommitOptions{})
checkErr(err)
err = gitRepo.CreateBranch(&config.Branch{
Name: "master",
Remote: "origin",
Merge: "refs/heads/master",
})
checkErr(err)
_, err = gitRepo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{fmt.Sprintf("[email protected]:%v/%v.git", userName, repoName)},
})
checkErr(err)
identity := ssh_config.Get("github.com", "IdentityFile")
if identity == "" {
identity = "~/.git/id_rsa"
}
expand, err := homedir.Expand(identity)
checkErr(err)
fmt.Println("using identity file", identity)
callback, err := ssh.NewKnownHostsCallback()
checkErr(err)
auth, err := ssh.NewPublicKeysFromFile("git", expand, "")
checkErr(err)
clientConfig, err := auth.ClientConfig()
checkErr(err)
clientConfig.HostKeyCallback = callback
fmt.Println(auth)
err = gitRepo.Push(&git.PushOptions{
Auth: auth,
RemoteName: "origin",
})
checkErr(err)
}