@@ -15,6 +15,9 @@ package do
1515
1616import (
1717 "context"
18+ "encoding/json"
19+ "fmt"
20+ "strings"
1821
1922 "github.com/digitalocean/godo"
2023)
@@ -26,6 +29,7 @@ type AppsService interface {
2629 List () ([]* godo.App , error )
2730 Update (appID string , req * godo.AppUpdateRequest ) (* godo.App , error )
2831 Delete (appID string ) error
32+ Detect (source string , sha string , name string , branch string , autoDeploy bool ) (string , error )
2933 Propose (req * godo.AppProposeRequest ) (* godo.AppProposeResponse , error )
3034
3135 CreateDeployment (appID string , forceRebuild bool ) (* godo.Deployment , error )
@@ -119,6 +123,125 @@ func (s *appsService) Delete(appID string) error {
119123 return err
120124}
121125
126+ func (s * appsService ) Detect (source string , sha string , name string , branch string , autoDeploy bool ) (string , error ) {
127+ var dr godo.DetectRequest
128+ if strings .Contains (source , "github" ) {
129+ dr .GitHub = & godo.GitHubSourceSpec {
130+ Repo : verifyGitSource (source , "github" ),
131+ Branch : branch ,
132+ DeployOnPush : autoDeploy ,
133+ }
134+ } else if strings .Contains (source , "gitlab" ) {
135+ dr .GitLab = & godo.GitLabSourceSpec {
136+ Repo : verifyGitSource (source , "gitlab" ),
137+ Branch : branch ,
138+ DeployOnPush : autoDeploy ,
139+ }
140+ } else {
141+ dr .Git = & godo.GitSourceSpec {
142+ RepoCloneURL : source ,
143+ Branch : branch ,
144+ }
145+ }
146+ dr .SourceDir = "/"
147+ dr .CommitSHA = sha
148+
149+ resp , _ , err := s .client .Apps .Detect (context .Background (), & dr )
150+ if err != nil {
151+ return "" , err
152+ }
153+
154+ x , e := json .Marshal (resp .Template .Spec )
155+ if e != nil {
156+ fmt .Println ("Error in stringifying json" )
157+ }
158+ fmt .Printf (`Templace Spec: %s` , string (x ))
159+ fmt .Println ("" )
160+ fmt .Println ("" )
161+ fmt .Println ("" )
162+ x , e = json .Marshal (resp .Components )
163+ if e != nil {
164+ fmt .Println ("Error in stringifying json" )
165+ }
166+ fmt .Printf (`Component Spec: %s` , string (x ))
167+ fmt .Println ("" )
168+ fmt .Println ("" )
169+ fmt .Println ("" )
170+
171+ var appSpec godo.AppSpec
172+
173+ appSpec .Name = name
174+ var funcSpecArray []* godo.AppFunctionsSpec
175+ for _ , component := range resp .Components {
176+
177+ if component .Strategy == "SERVERLESS" {
178+ for _ , serverlessPackage := range component .ServerlessPackages {
179+ var functionSpec godo.AppFunctionsSpec
180+ functionSpec .Name = serverlessPackage .Name
181+ if strings .Contains (source , "github" ) {
182+ functionSpec .GitHub = & godo.GitHubSourceSpec {
183+ Repo : verifyGitSource (source , "github" ),
184+ Branch : branch ,
185+ DeployOnPush : autoDeploy ,
186+ }
187+ } else if strings .Contains (source , "gitlab" ) {
188+ functionSpec .GitLab = & godo.GitLabSourceSpec {
189+ Repo : verifyGitSource (source , "gitlab" ),
190+ Branch : branch ,
191+ DeployOnPush : autoDeploy ,
192+ }
193+ } else {
194+ functionSpec .Git = & godo.GitSourceSpec {
195+ RepoCloneURL : source ,
196+ Branch : branch ,
197+ }
198+ }
199+ functionSpec .SourceDir = "/"
200+ functionSpec .Routes = []* godo.AppRouteSpec {
201+ {
202+ Path : "/" ,
203+ PreservePathPrefix : false ,
204+ },
205+ }
206+ funcSpecArray = append (funcSpecArray , & functionSpec )
207+
208+ fmt .Println ("Function Spec ::" )
209+ x , _ := json .Marshal (functionSpec )
210+ fmt .Println (string (x ))
211+ fmt .Println ("" )
212+ fmt .Println ("=================" )
213+ fmt .Println ("" )
214+ }
215+ }
216+
217+ fmt .Println ("Function Spec Array ::" )
218+ x , _ := json .Marshal (funcSpecArray )
219+ fmt .Println (string (x ))
220+ fmt .Println ("" )
221+ fmt .Println ("=================" )
222+ fmt .Println ("" )
223+ appSpec .Functions = funcSpecArray
224+ }
225+
226+ fmt .Println ("App Spec ::" )
227+ x , _ = json .Marshal (appSpec )
228+ fmt .Println (string (x ))
229+ fmt .Println ("" )
230+ fmt .Println ("=================" )
231+ fmt .Println ("" )
232+
233+ return "" , nil
234+ }
235+
236+ func verifyGitSource (s string , splitter string ) string {
237+ x := strings .Split (s , splitter + ".com/" )
238+ if strings .Contains (x [1 ], ".git" ) {
239+ x = strings .Split (x [1 ], "." )
240+ }
241+ fmt .Println ("Git Soource : " , x [0 ])
242+ return x [0 ]
243+ }
244+
122245func (s * appsService ) Propose (req * godo.AppProposeRequest ) (* godo.AppProposeResponse , error ) {
123246 res , _ , err := s .client .Apps .Propose (s .ctx , req )
124247 if err != nil {
0 commit comments