-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
144 lines (121 loc) · 3.2 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/YianAndCode/github-deploy-keys-manager/utils"
)
var bitSize int
var force bool
var keyPath string
var repoUrl string
var repoAlias string
func init() {
flag.IntVar(&bitSize, "bits", 4096, "RSA key bits")
flag.BoolVar(&force, "f", false, "Generate key anyway")
flag.StringVar(&keyPath, "key-path", filepath.Join(os.Getenv("HOME"), ".ssh", "deploy"), "Key path, default is ~/.ssh/deploy/")
flag.StringVar(&repoUrl, "repo", "", "Repo url(ssh)")
flag.StringVar(&repoAlias, "alias", "", "Repo alias")
}
func exitWithMessage(message string) {
fmt.Println(message)
os.Exit(1)
}
func exitWithTips(errMsgs ...string) {
flag.PrintDefaults()
fmt.Println()
if len(errMsgs) > 0 {
for _, msg := range errMsgs {
fmt.Println("\033[31m" + msg + "\033[0m")
}
}
os.Exit(1)
}
func main() {
flag.Parse()
if repoUrl == "" {
exitWithTips("Repo url is required")
}
var repo utils.Repo
err := repo.ParseFromUrl(repoUrl)
if err != nil {
exitWithMessage(err.Error())
}
if repoAlias == "" {
repoAlias = repo.GetAlias(true)
}
generateKey(keyPath, repoAlias, force)
sshConfig := fmt.Sprintf(
"Host %s\n Hostname %s\n IdentityFile=%s\n\n",
repoAlias, repo.Host, getKeyFileName(keyPath, repoAlias)+".id_rsa",
)
err = updateSSHConfig(sshConfig)
if err != nil {
fmt.Printf("Key generated, buy error occur while update ssh config: %s", err.Error())
return
}
fmt.Printf(
"Deploy key generated, the public key is stored in %s\n\nYour new repo url is: %s@%s:%s/%s.git\n",
getKeyFileName(keyPath, repoAlias)+".id_rsa.pub", repo.SshUser, repoAlias, repo.Owner, repo.Name,
)
}
func file_exist(filename string) (bool, error) {
if _, err := os.Stat(filename); err == nil {
// file exist
return true, nil
} else if os.IsNotExist(err) {
// file not exist
return false, nil
} else {
return false, err
}
}
func getKeyFileName(_keyPath, _repoAlias string) string {
return filepath.Join(_keyPath, _repoAlias)
}
func generateKey(_keyPath, _repoAlias string, _force bool) {
keypath_ex, err := file_exist(_keyPath)
if err != nil {
exitWithMessage(err.Error())
}
if !keypath_ex {
os.Mkdir(_keyPath, 0700)
}
privateKeyFile := getKeyFileName(_keyPath, _repoAlias) + ".id_rsa"
publicKeyFile := privateKeyFile + ".pub"
prikey_ex, err := file_exist(privateKeyFile)
if err != nil {
exitWithMessage(err.Error())
}
if prikey_ex && !_force {
exitWithMessage("Private key exist. You can use -f to overwrite it")
}
pubkey_ex, err := file_exist(publicKeyFile)
if err != nil {
exitWithMessage(err.Error())
}
if pubkey_ex && !_force {
exitWithMessage("Public key exist. You can use -f to overwrite it")
}
kp, err := utils.NewKeyPair(bitSize)
if err != nil {
exitWithMessage(err.Error())
}
err = kp.WriteToFile(privateKeyFile)
if err != nil {
exitWithMessage("Error occur while saving key file: " + err.Error())
}
}
func updateSSHConfig(config string) error {
sshConfigFile := filepath.Join(os.Getenv("HOME"), ".ssh", "config")
f, err := os.OpenFile(sshConfigFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(config); err != nil {
return err
}
return nil
}