Skip to content

Commit

Permalink
Merge pull request #13 from coherenceplatform/zz/deploy-cmd-0717
Browse files Browse the repository at this point in the history
add deploy command to environments
  • Loading branch information
zach-withcoherence authored Jul 17, 2024
2 parents 58d7ac7 + 23fde00 commit 16d6cb9
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
4 changes: 4 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

`go run main.go`

## Running in docker

docker run --rm -it -v "$(pwd)":/app -w /app golang:1.23rc2 bash

## Versioning

To update the cocli version it needs to be set in 2 places:
Expand Down
95 changes: 95 additions & 0 deletions cmd/cocli/deployEnvironment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cocli

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strconv"

"github.com/coherenceplatform/cocli/pkg/cocli"
"github.com/spf13/cobra"
)

type EnvironmentServiceDeploy struct {
Name string `json:"name"`
BranchName string `json:"branch_name,omitempty"`
CommitSHA string `json:"commit_sha,omitempty"`
}

type EnvironmentDeploy struct {
Services []EnvironmentServiceDeploy `json:"services"`
ConfigureInfra bool `json:"configure_infra,omitempty"`
}

var deployCmd = &cobra.Command{
Use: "deploy <environment_id> <json_file_or_string>",
Short: "Deploy to a specific environment",
Long: "Deploy services to a specific Coherence environment using a JSON configuration.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
environmentID, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("Invalid environment ID")
return
}

var deployData EnvironmentDeploy
jsonInput := args[1]

// Check if the input is a file
if _, err := os.Stat(jsonInput); err == nil {
jsonFile, err := os.ReadFile(jsonInput)
if err != nil {
fmt.Println("Error reading JSON file:", err)
return
}
err = json.Unmarshal(jsonFile, &deployData)
} else {
// Treat input as a JSON string
err = json.Unmarshal([]byte(jsonInput), &deployData)
}

if err != nil {
fmt.Println("Error parsing JSON:", err)
return
}

if len(deployData.Services) == 0 {
fmt.Println("At least one service must be specified")
return
}

jsonData, err := json.Marshal(deployData)
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}

deployURL := fmt.Sprintf(
"%s/api/v1/environments/%d/deploy",
cocli.GetCoherenceDomain(),
environmentID,
)

res, err := cocli.CoherenceApiRequest(
"PUT",
deployURL,
bytes.NewBuffer(jsonData),
)
if err != nil {
fmt.Println("Error making request:", err)
return
}
defer res.Body.Close()

bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}

fmt.Println(cocli.FormatJSONOutput(bodyBytes))
},
}
14 changes: 14 additions & 0 deletions cmd/cocli/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cocli

import (
"github.com/spf13/cobra"
)

var environmentCmd = &cobra.Command{
Use: "environments",
Short: "Coherence environment management commands",
}

func init() {
environmentCmd.AddCommand(deployCmd)
}
1 change: 1 addition & 0 deletions cmd/cocli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func init() {
rootCmd.AddCommand(applicationCmd)
rootCmd.AddCommand(collectionCmd)
rootCmd.AddCommand(cncCmd)
rootCmd.AddCommand(environmentCmd)
cocli.RunCliVersionCheck()
}
2 changes: 1 addition & 1 deletion cocli_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.2
1.0.3
2 changes: 1 addition & 1 deletion pkg/cocli/cocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var prodConfig = &CocliConfig{
CoherenceDomain: "https://beta.withcoherence.com",
}

const cliVersion = "1.0.2"
const cliVersion = "1.0.3"

func GetCliConfig() CocliConfig {
if strings.ToLower(os.Getenv("COHERENCE_ENVIRONMENT")) == "review" {
Expand Down

0 comments on commit 16d6cb9

Please sign in to comment.