11package plugin
22
33import (
4+ "io/ioutil"
5+ "strings"
6+ "time"
7+
48 "github.com/go-git/go-git/v5/plumbing/object"
59 "github.com/grafana/grafana-plugin-sdk-go/backend/log"
610 ssh2 "golang.org/x/crypto/ssh"
@@ -10,41 +14,37 @@ import (
1014 object2 "gopkg.in/src-d/go-git.v4/plumbing/object"
1115 "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
1216 "gopkg.in/src-d/go-git.v4/storage/memory"
13- "io/ioutil"
14- "os"
15- "strings"
16- "time"
1717)
1818
1919// GitApi access to git api
2020type GitApi struct {
21- gitUrl string
22- authenticator * ssh.PublicKeys
23- inMemoryStore memory.Storage
21+ gitUrl string
22+ authenticator * ssh.PublicKeys
23+ inMemoryStore memory.Storage
2424 inMemoryFileSystem billy.Filesystem
2525}
2626
2727// NewGitApi creates a new NewGitApi instance
28- func NewGitApi (gitUrl string , privateKeyFilePath string ) GitApi {
29- authenticator , err := createAuthenticator (privateKeyFilePath )
28+ func NewGitApi (gitUrl string , privateKey [] byte ) GitApi {
29+ authenticator , err := createAuthenticator (privateKey )
3030 if err != nil {
3131 log .DefaultLogger .Error ("authentication failed" , "error" , err .Error ())
3232 }
3333 inMemoryStore , inMemoryFileSystem := createInMemory ()
34- gitApi := GitApi {gitUrl ,authenticator , * inMemoryStore , inMemoryFileSystem }
34+ gitApi := GitApi {gitUrl , authenticator , * inMemoryStore , inMemoryFileSystem }
3535
3636 return gitApi
3737}
3838
3939// helper function to create the git authenticator
40- func createAuthenticator (privateKeyFilePath string ) (* ssh.PublicKeys , error ) {
40+ func createAuthenticator (privateKey [] byte ) (* ssh.PublicKeys , error ) {
4141 // git authentication with ssh
42- _ , err := os .Stat (privateKeyFilePath )
43- if err != nil {
44- log .DefaultLogger .Error ("read file %s failed" , privateKeyFilePath , err .Error ())
45- return nil , err
46- }
47- authenticator , err := ssh .NewPublicKeysFromFile ("git" , privateKeyFilePath , "" )
42+ // _, err := os.Stat(privateKeyFilePath)
43+ // if err != nil {
44+ // log.DefaultLogger.Error("read file failed", privateKeyFilePath, err.Error())
45+ // return nil, err
46+ // }
47+ authenticator , err := ssh .NewPublicKeys ("git" , privateKey , "" )
4848 if err != nil {
4949 log .DefaultLogger .Error ("generate public keys failed" , "error" , err .Error ())
5050 return nil , err
@@ -57,7 +57,7 @@ func createAuthenticator(privateKeyFilePath string) (*ssh.PublicKeys, error) {
5757}
5858
5959// helper function to create the in memory storage and filesystem
60- func createInMemory () (* memory.Storage , billy.Filesystem ){
60+ func createInMemory () (* memory.Storage , billy.Filesystem ) {
6161 // prepare in memory
6262 store := memory .NewStorage ()
6363 var fs billy.Filesystem
@@ -67,14 +67,14 @@ func createInMemory() (*memory.Storage, billy.Filesystem){
6767}
6868
6969// CloneRepo clones the gitApi.gitUrls repository
70- func (gitApi GitApi ) CloneRepo () (* git.Repository , error ){
70+ func (gitApi GitApi ) CloneRepo () (* git.Repository , error ) {
7171 // git clone
7272 r , err := git .Clone (& gitApi .inMemoryStore , gitApi .inMemoryFileSystem , & git.CloneOptions {
73- URL : gitApi .gitUrl ,
73+ URL : gitApi .gitUrl ,
7474 Auth : gitApi .authenticator ,
7575 })
7676 if err != nil {
77- log .DefaultLogger .Error ("clone error" , "error" , err )
77+ log .DefaultLogger .Error ("clone error" , "error" , err )
7878 return nil , err
7979 } else {
8080 log .DefaultLogger .Info ("repo cloned" )
@@ -89,7 +89,7 @@ func (gitApi GitApi) FetchRepo(repository git.Repository) {
8989 log .DefaultLogger .Info ("fetching repo" )
9090 err := repository .Fetch (& git.FetchOptions {
9191 RemoteName : "origin" ,
92- Auth : gitApi .authenticator ,
92+ Auth : gitApi .authenticator ,
9393 })
9494 if strings .Contains (err .Error (), "already up-to-date" ) {
9595 log .DefaultLogger .Info ("fetching completed" , "message" , err .Error ())
@@ -116,21 +116,21 @@ func (gitApi GitApi) CommitWorktree(repository git.Repository, tag string) {
116116 // get worktree and commit
117117 w , err := repository .Worktree ()
118118 if err != nil {
119- log .DefaultLogger .Error ("worktree error" , "error" , err )
119+ log .DefaultLogger .Error ("worktree error" , "error" , err )
120120 return
121121 } else {
122122 w .Add ("./" )
123123 wStatus , _ := w .Status ()
124- log .DefaultLogger .Debug ("worktree status" , "status" , wStatus )
124+ log .DefaultLogger .Debug ("worktree status" , "status" , wStatus )
125125
126- _ , err := w .Commit ("Synchronized Dashboards with tag <" + tag + ">" , & git.CommitOptions {
126+ _ , err := w .Commit ("Synchronized Dashboards with tag <" + tag + ">" , & git.CommitOptions {
127127 Author : (* object2 .Signature )(& object.Signature {
128- Name : "grafana-dashboard-sync-plugin" ,
129- When : time .Now (),
128+ Name : "grafana-dashboard-sync-plugin" ,
129+ When : time .Now (),
130130 }),
131131 })
132132 if err != nil {
133- log .DefaultLogger .Error ("worktree commit error" , "error" , err .Error ())
133+ log .DefaultLogger .Error ("worktree commit error" , "error" , err .Error ())
134134 return
135135 }
136136 }
@@ -141,7 +141,7 @@ func (gitApi GitApi) PushRepo(repository git.Repository) {
141141 // push repo
142142 err := repository .Push (& git.PushOptions {
143143 RemoteName : "origin" ,
144- Auth : gitApi .authenticator ,
144+ Auth : gitApi .authenticator ,
145145 })
146146 if err != nil {
147147 log .DefaultLogger .Error ("push error" , "error" , err .Error ())
@@ -153,12 +153,12 @@ func (gitApi GitApi) PullRepo(repository git.Repository) {
153153 // pull repo
154154 w , err := repository .Worktree ()
155155 if err != nil {
156- log .DefaultLogger .Error ("worktree error" , "error" , err )
156+ log .DefaultLogger .Error ("worktree error" , "error" , err )
157157 } else {
158158 log .DefaultLogger .Debug ("Pulling from Repo" )
159159 err := w .Pull (& git.PullOptions {
160160 RemoteName : "origin" ,
161- Auth : gitApi .authenticator ,
161+ Auth : gitApi .authenticator ,
162162 })
163163 if err != nil {
164164 if strings .Contains (err .Error (), "already up-to-date" ) {
@@ -175,7 +175,7 @@ func (gitApi GitApi) GetFileContent() map[string]map[string][]byte {
175175 // read current in memory filesystem to get dirs
176176 filesOrDirs , err := gitApi .inMemoryFileSystem .ReadDir ("./" )
177177 if err != nil {
178- log .DefaultLogger .Error ("inMemoryFileSystem error" , "error" , err )
178+ log .DefaultLogger .Error ("inMemoryFileSystem error" , "error" , err )
179179 return nil
180180 }
181181
@@ -225,4 +225,4 @@ func (gitApi GitApi) GetFileContent() map[string]map[string][]byte {
225225 }
226226 }
227227 return fileMap
228- }
228+ }
0 commit comments