11package service
22
33import (
4+ "fmt"
45 "io/ioutil"
56 "os"
67 "path/filepath"
@@ -20,34 +21,41 @@ type protoResource struct {
2021
2122type Sync interface {
2223 Resolve (forceUpdate bool , cleanupCache bool ) error
24+
25+ SetHttpsAuthProvider (provider helper.AuthProvider )
26+ SetSshAuthProvider (provider helper.AuthProvider )
2327}
2428
2529type SyncImpl struct {
26- authProvider helper.AuthProvider
27- userHomeDir string
28- targetDir string
29- outputRootDir string
30+ conf * helper.SyncConfig
31+
32+ httpsProvider helper. AuthProvider
33+ sshProvider helper. AuthProvider
3034}
3135
32- func NewSync (authProvider helper.AuthProvider , userHomeDir string , targetDir string , outputRootDir string ) Sync {
33- return & SyncImpl {
34- authProvider : authProvider ,
35- userHomeDir : userHomeDir ,
36- targetDir : targetDir ,
37- outputRootDir : outputRootDir ,
36+ func NewSync (conf * helper.SyncConfig ) (Sync , error ) {
37+ s := & SyncImpl {
38+ conf : conf ,
3839 }
40+
41+ err := s .initAuthProviders ()
42+ if err != nil {
43+ return nil , err
44+ }
45+
46+ return s , nil
3947}
4048
4149func (s * SyncImpl ) Resolve (forceUpdate bool , cleanupCache bool ) error {
4250
43- dep := dependency .NewDependency (s .targetDir , forceUpdate )
51+ dep := dependency .NewDependency (s .conf . TargetDir , forceUpdate )
4452 protodep , err := dep .Load ()
4553 if err != nil {
4654 return err
4755 }
4856
4957 newdeps := make ([]dependency.ProtoDepDependency , 0 , len (protodep .Dependencies ))
50- protodepDir := filepath .Join (s .userHomeDir , ".protodep" )
58+ protodepDir := filepath .Join (s .conf . HomeDir , ".protodep" )
5159
5260 _ , err = os .Stat (protodepDir )
5361 if cleanupCache && err == nil {
@@ -65,13 +73,28 @@ func (s *SyncImpl) Resolve(forceUpdate bool, cleanupCache bool) error {
6573 }
6674 }
6775
68- outdir := filepath .Join (s .outputRootDir , protodep .ProtoOutdir )
76+ outdir := filepath .Join (s .conf . OutputDir , protodep .ProtoOutdir )
6977 if err := os .RemoveAll (outdir ); err != nil {
7078 return err
7179 }
7280
7381 for _ , dep := range protodep .Dependencies {
74- gitrepo := repository .NewGitRepository (protodepDir , dep , s .authProvider )
82+ var authProvider helper.AuthProvider
83+
84+ if s .conf .UseHttps {
85+ authProvider = s .httpsProvider
86+ } else {
87+ switch dep .Protocol {
88+ case "https" :
89+ authProvider = s .httpsProvider
90+ case "ssh" , "" :
91+ authProvider = s .sshProvider
92+ default :
93+ return fmt .Errorf ("%s protocol is not accepted (ssh or https only)" , dep .Protocol )
94+ }
95+ }
96+
97+ gitrepo := repository .NewGitRepository (protodepDir , dep , authProvider )
7598
7699 repo , err := gitrepo .Open ()
77100 if err != nil {
@@ -119,6 +142,7 @@ func (s *SyncImpl) Resolve(forceUpdate bool, cleanupCache bool) error {
119142 Revision : repo .Hash ,
120143 Path : repo .Dep .Path ,
121144 Ignores : repo .Dep .Ignores ,
145+ Protocol : repo .Dep .Protocol ,
122146 })
123147 }
124148
@@ -136,6 +160,39 @@ func (s *SyncImpl) Resolve(forceUpdate bool, cleanupCache bool) error {
136160 return nil
137161}
138162
163+ func (s * SyncImpl ) SetHttpsAuthProvider (provider helper.AuthProvider ) {
164+ s .httpsProvider = provider
165+ }
166+
167+ func (s * SyncImpl ) SetSshAuthProvider (provider helper.AuthProvider ) {
168+ s .sshProvider = provider
169+ }
170+
171+ func (s * SyncImpl ) initAuthProviders () error {
172+ s .httpsProvider = helper .NewAuthProvider (helper .WithHTTPS (s .conf .BasicAuthUsername , s .conf .BasicAuthPassword ))
173+
174+ if s .conf .IdentityFile == "" && s .conf .IdentityPassword == "" {
175+ s .sshProvider = helper .NewAuthProvider ()
176+
177+ return nil
178+ }
179+
180+ identifyPath := filepath .Join (s .conf .HomeDir , ".ssh" , s .conf .IdentityFile )
181+ isSSH , err := helper .IsAvailableSSH (identifyPath )
182+ if err != nil {
183+ return err
184+ }
185+
186+ if isSSH {
187+ s .sshProvider = helper .NewAuthProvider (helper .WithPemFile (identifyPath , s .conf .IdentityPassword ))
188+ } else {
189+ logger .Warn ("The identity file path has been passed but is not available. Falling back to ssh-agent, the default authentication method." )
190+ s .sshProvider = helper .NewAuthProvider ()
191+ }
192+
193+ return nil
194+ }
195+
139196func compileIngoresToGlob (ignores []string ) []glob.Glob {
140197 globIngores := make ([]glob.Glob , len (ignores ))
141198
0 commit comments