Skip to content

Commit 401649d

Browse files
committed
go: Split code into internal modules
This way I'll be able to reuse this code more easily.
1 parent 090360d commit 401649d

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/gitjournal.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
git "github.com/gitjournal/go-git-dart/internal/git"
5+
keygen "github.com/gitjournal/go-git-dart/internal/keygen"
46

57
/*
68
#include <stdlib.h>
@@ -11,7 +13,7 @@ import (
1113

1214
//export GitClone
1315
func GitClone(url *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
14-
err := gitClone(C.GoString(url), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
16+
err := git.Clone(C.GoString(url), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
1517
if err != nil {
1618
return C.CString(err.Error())
1719
}
@@ -21,7 +23,7 @@ func GitClone(url *C.char, directory *C.char, privateKey *C.char, privateKeyLen
2123

2224
//export GitFetch
2325
func GitFetch(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
24-
err := gitFetch(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
26+
err := git.Fetch(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
2527
if err != nil {
2628
return C.CString(err.Error())
2729
}
@@ -31,7 +33,7 @@ func GitFetch(remote *C.char, directory *C.char, privateKey *C.char, privateKeyL
3133

3234
//export GitPush
3335
func GitPush(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char) *C.char {
34-
err := gitPush(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
36+
err := git.Push(C.GoString(remote), C.GoString(directory), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
3537
if err != nil {
3638
return C.CString(err.Error())
3739
}
@@ -41,7 +43,7 @@ func GitPush(remote *C.char, directory *C.char, privateKey *C.char, privateKeyLe
4143

4244
//export GitDefaultBranch
4345
func GitDefaultBranch(remoteUrl *C.char, privateKey *C.char, privateKeyLen C.int, password *C.char, outputBranchName **C.char) *C.char {
44-
val, err := gitDefaultBranch(C.GoString(remoteUrl), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
46+
val, err := git.DefaultBranch(C.GoString(remoteUrl), C.GoBytes(unsafe.Pointer(privateKey), privateKeyLen), C.GoString(password))
4547
if err != nil {
4648
return C.CString(err.Error())
4749
}
@@ -52,7 +54,7 @@ func GitDefaultBranch(remoteUrl *C.char, privateKey *C.char, privateKeyLen C.int
5254

5355
//export GJGenerateRSAKeys
5456
func GJGenerateRSAKeys(publicKey **C.char, privateKey **C.char) *C.char {
55-
publicKeyVal, privateKeyVal, err := generateRSAKeys()
57+
publicKeyVal, privateKeyVal, err := keygen.GenerateRSAKeys()
5658
if err != nil {
5759
return C.CString(err.Error())
5860
}

src/git.go renamed to src/internal/git/git.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package git
22

33
import (
44
"fmt"
@@ -27,7 +27,7 @@ func buildAuth(url string, privateKey []byte, password string) (transport.AuthMe
2727
return publicKeys, nil
2828
}
2929

30-
func gitClone(url string, directory string, privateKey []byte, password string) error {
30+
func Clone(url string, directory string, privateKey []byte, password string) error {
3131
auth, err := buildAuth(url, privateKey, password)
3232
if err != nil {
3333
return err
@@ -67,7 +67,7 @@ func buildAuthForRemote(repo *git.Repository, remoteName string, privateKey []by
6767
return buildAuth(urls[0], privateKey, password)
6868
}
6969

70-
func gitFetch(remote string, directory string, privateKey []byte, password string) error {
70+
func Fetch(remote string, directory string, privateKey []byte, password string) error {
7171
r, err := git.PlainOpen(directory)
7272
if err != nil {
7373
return err
@@ -90,7 +90,7 @@ func gitFetch(remote string, directory string, privateKey []byte, password strin
9090
return nil
9191
}
9292

93-
func gitPush(remote string, directory string, privateKey []byte, password string) error {
93+
func Push(remote string, directory string, privateKey []byte, password string) error {
9494
r, err := git.PlainOpen(directory)
9595
if err != nil {
9696
return err
@@ -113,7 +113,7 @@ func gitPush(remote string, directory string, privateKey []byte, password string
113113
return nil
114114
}
115115

116-
func gitDefaultBranch(remoteUrl string, privateKey []byte, password string) (string, error) {
116+
func DefaultBranch(remoteUrl string, privateKey []byte, password string) (string, error) {
117117
auth, err := buildAuth(remoteUrl, privateKey, password)
118118
if err != nil {
119119
return "", err

src/keygen.go renamed to src/internal/keygen/keygen.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package keygen
22

33
import (
44
"crypto/rand"
@@ -15,7 +15,7 @@ import (
1515

1616
// generateRSAKeys generates an RSA public/private key pair
1717
// and returns them as PEM encoded strings.
18-
func generateRSAKeys() (string, string, error) {
18+
func GenerateRSAKeys() (string, string, error) {
1919
// Generate the private key
2020
privateKey, err := rsa.GenerateKey(rand.Reader, 2048*2)
2121
if err != nil {
@@ -47,7 +47,7 @@ func generateRSAKeys() (string, string, error) {
4747
Bytes: publicKeyDER,
4848
}
4949
publicKeyPEM := string(pem.EncodeToMemory(publicKeyBlock))
50-
publicKeyOpenSSH, err := PublicPEMtoOpenSSH([]byte(publicKeyPEM))
50+
publicKeyOpenSSH, err := publicPEMtoOpenSSH([]byte(publicKeyPEM))
5151
if err != nil {
5252
return "", "", err
5353
}
@@ -57,7 +57,7 @@ func generateRSAKeys() (string, string, error) {
5757

5858
// Converts PEM public key to OpenSSH format to be used in authorized_keys file
5959
// Similar to: "ssh-keygen", "-i", "-m", "pkcs8", "-f", auth_keys_new_path
60-
func PublicPEMtoOpenSSH(pemBytes []byte) (string, error) {
60+
func publicPEMtoOpenSSH(pemBytes []byte) (string, error) {
6161
// Decode and get the first block in the PEM file.
6262
// In our case it should be the Public key block.
6363
pemBlock, rest := pem.Decode(pemBytes)

0 commit comments

Comments
 (0)