Skip to content

Commit 85e768c

Browse files
add apps detect command
1 parent 51c4f6b commit 85e768c

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

args.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,13 @@ const (
406406

407407
// ArgAlertPolicySlackURLs are the Slack URLs to send alerts to.
408408
ArgAlertPolicySlackURLs = "slack-urls"
409+
410+
// ArgCommitHash are the Git commit hash.
411+
ArgCommitHash = "sha"
412+
// ArgProjectSource is either git, github or gitlab.
413+
ArgProjectSource = "source"
414+
// ArgDeployOnPush allow auto deploy on project update.
415+
ArgDeployOnPush = "deployonpush"
416+
// ArgProjectBrach is git project branch.
417+
ArgProjectBrach = "branch"
409418
)

commands/apps.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"net/url"
2424
"os"
25+
"strconv"
2526
"strings"
2627
"time"
2728

@@ -113,6 +114,15 @@ This permanently deletes the app and all its associated deployments.`,
113114
)
114115
AddBoolFlag(deleteApp, doctl.ArgForce, doctl.ArgShortForce, false, "Delete the App without a confirmation prompt")
115116

117+
// output is global flag
118+
detect := CmdBuilder(cmd,
119+
RunAppsDetect, "detect", "Detect functions", "Detect functions project and convert it into apps project by adding the AppSpec.", Writer, aliasOpt("dt"))
120+
AddStringFlag(detect, doctl.ArgProjectSource, "", "", `Project source.`)
121+
AddStringFlag(detect, doctl.ArgCommitHash, "", "", `Git commit hash`) // not sure if need to support commit hash?
122+
AddStringFlag(detect, doctl.ArgProjectName, "", "", `App name to be used`)
123+
AddStringFlag(detect, doctl.ArgProjectBrach, "", "", `Project branch to be used`)
124+
AddBoolFlag(detect, doctl.ArgDeployOnPush, "", *boolPtr(true), `Auto deploy on project update.`)
125+
116126
deploymentCreate := CmdBuilder(
117127
cmd,
118128
RunAppsCreateDeployment,
@@ -393,6 +403,78 @@ func RunAppsDelete(c *CmdConfig) error {
393403
return nil
394404
}
395405

406+
// RunAppsDetect detects an function project and converts it into apps project.
407+
func RunAppsDetect(c *CmdConfig) error {
408+
source, err := c.Doit.GetString(c.NS, doctl.ArgProjectSource)
409+
if err != nil {
410+
return err
411+
}
412+
if len(source) == 0 {
413+
return fmt.Errorf("source cannot be empty")
414+
}
415+
416+
sha, err := c.Doit.GetString(c.NS, doctl.ArgCommitHash)
417+
if err != nil {
418+
return err
419+
}
420+
421+
name, err := c.Doit.GetString(c.NS, doctl.ArgProjectName)
422+
if err != nil {
423+
return err
424+
}
425+
if len(name) == 0 {
426+
return fmt.Errorf("name cannot be empty")
427+
}
428+
429+
branch, err := c.Doit.GetString(c.NS, doctl.ArgProjectBrach)
430+
if err != nil {
431+
return err
432+
}
433+
if len(branch) == 0 {
434+
return fmt.Errorf("branch cannot be empty")
435+
}
436+
437+
// Need to check, How user value will be overrided
438+
autoDeploy, err := c.Doit.GetBool(c.NS, doctl.ArgDeployOnPush)
439+
if err != nil {
440+
return err
441+
}
442+
if len(c.Args) > 0 {
443+
fmt.Println(c.Args[0])
444+
x, err := strconv.ParseBool(c.Args[0])
445+
if err == nil {
446+
autoDeploy = x
447+
} else {
448+
return fmt.Errorf("expected true/false for deployonpush, received : %s", c.Args[0])
449+
}
450+
}
451+
452+
// Need to Fix
453+
output, err := c.Doit.GetString(c.NS, doctl.ArgOutput)
454+
if err != nil {
455+
return err
456+
}
457+
458+
fmt.Println("1 : ", source)
459+
fmt.Println("2 : ", sha)
460+
fmt.Println("3 : ", name)
461+
fmt.Println("4 : ", branch)
462+
fmt.Println("5 : ", autoDeploy)
463+
fmt.Println("6 : ", output)
464+
fmt.Println("7 : ", c.NS)
465+
466+
_, err2 := c.Apps().Detect(source, sha, name, branch, autoDeploy)
467+
if err2 != nil {
468+
fmt.Println(err2)
469+
}
470+
471+
// TRANSALATE THE AUTO DETECTED CCONFIG TO APPSPEC.
472+
473+
// var appSpec godo.AppSpec
474+
475+
return nil
476+
}
477+
396478
// RunAppsCreateDeployment creates a deployment for an app.
397479
func RunAppsCreateDeployment(c *CmdConfig) error {
398480
if len(c.Args) < 1 {

do/apps.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ package do
1515

1616
import (
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+
122245
func (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

Comments
 (0)