Skip to content

Commit

Permalink
add apps detect command
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkit-khullar committed Sep 8, 2022
1 parent 51c4f6b commit 85e768c
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 0 deletions.
9 changes: 9 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,13 @@ const (

// ArgAlertPolicySlackURLs are the Slack URLs to send alerts to.
ArgAlertPolicySlackURLs = "slack-urls"

// ArgCommitHash are the Git commit hash.
ArgCommitHash = "sha"
// ArgProjectSource is either git, github or gitlab.
ArgProjectSource = "source"
// ArgDeployOnPush allow auto deploy on project update.
ArgDeployOnPush = "deployonpush"
// ArgProjectBrach is git project branch.
ArgProjectBrach = "branch"
)
82 changes: 82 additions & 0 deletions commands/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

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

// output is global flag
detect := CmdBuilder(cmd,
RunAppsDetect, "detect", "Detect functions", "Detect functions project and convert it into apps project by adding the AppSpec.", Writer, aliasOpt("dt"))
AddStringFlag(detect, doctl.ArgProjectSource, "", "", `Project source.`)
AddStringFlag(detect, doctl.ArgCommitHash, "", "", `Git commit hash`) // not sure if need to support commit hash?
AddStringFlag(detect, doctl.ArgProjectName, "", "", `App name to be used`)
AddStringFlag(detect, doctl.ArgProjectBrach, "", "", `Project branch to be used`)
AddBoolFlag(detect, doctl.ArgDeployOnPush, "", *boolPtr(true), `Auto deploy on project update.`)

deploymentCreate := CmdBuilder(
cmd,
RunAppsCreateDeployment,
Expand Down Expand Up @@ -393,6 +403,78 @@ func RunAppsDelete(c *CmdConfig) error {
return nil
}

// RunAppsDetect detects an function project and converts it into apps project.
func RunAppsDetect(c *CmdConfig) error {
source, err := c.Doit.GetString(c.NS, doctl.ArgProjectSource)
if err != nil {
return err
}
if len(source) == 0 {
return fmt.Errorf("source cannot be empty")
}

sha, err := c.Doit.GetString(c.NS, doctl.ArgCommitHash)
if err != nil {
return err
}

name, err := c.Doit.GetString(c.NS, doctl.ArgProjectName)
if err != nil {
return err
}
if len(name) == 0 {
return fmt.Errorf("name cannot be empty")
}

branch, err := c.Doit.GetString(c.NS, doctl.ArgProjectBrach)
if err != nil {
return err
}
if len(branch) == 0 {
return fmt.Errorf("branch cannot be empty")
}

// Need to check, How user value will be overrided
autoDeploy, err := c.Doit.GetBool(c.NS, doctl.ArgDeployOnPush)
if err != nil {
return err
}
if len(c.Args) > 0 {
fmt.Println(c.Args[0])
x, err := strconv.ParseBool(c.Args[0])
if err == nil {
autoDeploy = x
} else {
return fmt.Errorf("expected true/false for deployonpush, received : %s", c.Args[0])
}
}

// Need to Fix
output, err := c.Doit.GetString(c.NS, doctl.ArgOutput)
if err != nil {
return err
}

fmt.Println("1 : ", source)
fmt.Println("2 : ", sha)
fmt.Println("3 : ", name)
fmt.Println("4 : ", branch)
fmt.Println("5 : ", autoDeploy)
fmt.Println("6 : ", output)
fmt.Println("7 : ", c.NS)

_, err2 := c.Apps().Detect(source, sha, name, branch, autoDeploy)
if err2 != nil {
fmt.Println(err2)
}

// TRANSALATE THE AUTO DETECTED CCONFIG TO APPSPEC.

// var appSpec godo.AppSpec

return nil
}

// RunAppsCreateDeployment creates a deployment for an app.
func RunAppsCreateDeployment(c *CmdConfig) error {
if len(c.Args) < 1 {
Expand Down
123 changes: 123 additions & 0 deletions do/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ package do

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/digitalocean/godo"
)
Expand All @@ -26,6 +29,7 @@ type AppsService interface {
List() ([]*godo.App, error)
Update(appID string, req *godo.AppUpdateRequest) (*godo.App, error)
Delete(appID string) error
Detect(source string, sha string, name string, branch string, autoDeploy bool) (string, error)
Propose(req *godo.AppProposeRequest) (*godo.AppProposeResponse, error)

CreateDeployment(appID string, forceRebuild bool) (*godo.Deployment, error)
Expand Down Expand Up @@ -119,6 +123,125 @@ func (s *appsService) Delete(appID string) error {
return err
}

func (s *appsService) Detect(source string, sha string, name string, branch string, autoDeploy bool) (string, error) {
var dr godo.DetectRequest
if strings.Contains(source, "github") {
dr.GitHub = &godo.GitHubSourceSpec{
Repo: verifyGitSource(source, "github"),
Branch: branch,
DeployOnPush: autoDeploy,
}
} else if strings.Contains(source, "gitlab") {
dr.GitLab = &godo.GitLabSourceSpec{
Repo: verifyGitSource(source, "gitlab"),
Branch: branch,
DeployOnPush: autoDeploy,
}
} else {
dr.Git = &godo.GitSourceSpec{
RepoCloneURL: source,
Branch: branch,
}
}
dr.SourceDir = "/"
dr.CommitSHA = sha

resp, _, err := s.client.Apps.Detect(context.Background(), &dr)
if err != nil {
return "", err
}

x, e := json.Marshal(resp.Template.Spec)
if e != nil {
fmt.Println("Error in stringifying json")
}
fmt.Printf(`Templace Spec: %s`, string(x))
fmt.Println("")
fmt.Println("")
fmt.Println("")
x, e = json.Marshal(resp.Components)
if e != nil {
fmt.Println("Error in stringifying json")
}
fmt.Printf(`Component Spec: %s`, string(x))
fmt.Println("")
fmt.Println("")
fmt.Println("")

var appSpec godo.AppSpec

appSpec.Name = name
var funcSpecArray []*godo.AppFunctionsSpec
for _, component := range resp.Components {

if component.Strategy == "SERVERLESS" {
for _, serverlessPackage := range component.ServerlessPackages {
var functionSpec godo.AppFunctionsSpec
functionSpec.Name = serverlessPackage.Name
if strings.Contains(source, "github") {
functionSpec.GitHub = &godo.GitHubSourceSpec{
Repo: verifyGitSource(source, "github"),
Branch: branch,
DeployOnPush: autoDeploy,
}
} else if strings.Contains(source, "gitlab") {
functionSpec.GitLab = &godo.GitLabSourceSpec{
Repo: verifyGitSource(source, "gitlab"),
Branch: branch,
DeployOnPush: autoDeploy,
}
} else {
functionSpec.Git = &godo.GitSourceSpec{
RepoCloneURL: source,
Branch: branch,
}
}
functionSpec.SourceDir = "/"
functionSpec.Routes = []*godo.AppRouteSpec{
{
Path: "/",
PreservePathPrefix: false,
},
}
funcSpecArray = append(funcSpecArray, &functionSpec)

fmt.Println("Function Spec ::")
x, _ := json.Marshal(functionSpec)
fmt.Println(string(x))
fmt.Println("")
fmt.Println("=================")
fmt.Println("")
}
}

fmt.Println("Function Spec Array ::")
x, _ := json.Marshal(funcSpecArray)
fmt.Println(string(x))
fmt.Println("")
fmt.Println("=================")
fmt.Println("")
appSpec.Functions = funcSpecArray
}

fmt.Println("App Spec ::")
x, _ = json.Marshal(appSpec)
fmt.Println(string(x))
fmt.Println("")
fmt.Println("=================")
fmt.Println("")

return "", nil
}

func verifyGitSource(s string, splitter string) string {
x := strings.Split(s, splitter+".com/")
if strings.Contains(x[1], ".git") {
x = strings.Split(x[1], ".")
}
fmt.Println("Git Soource : ", x[0])
return x[0]
}

func (s *appsService) Propose(req *godo.AppProposeRequest) (*godo.AppProposeResponse, error) {
res, _, err := s.client.Apps.Propose(s.ctx, req)
if err != nil {
Expand Down

0 comments on commit 85e768c

Please sign in to comment.