-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from coherenceplatform/zz/deploy-cmd-0717
add deploy command to environments
- Loading branch information
Showing
6 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.2 | ||
1.0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters