-
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 #14 from coherenceplatform/aa/env-item-mgmt
Add environment item management commands
- Loading branch information
Showing
12 changed files
with
393 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package cocli | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/coherenceplatform/cocli/pkg/cocli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var createEnvVarCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a new environment variable", | ||
Long: "Create a new environment variable for a specific environment or collection.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
targetEnvName, _ := cmd.Flags().GetString("target_env_name") | ||
itemType, _ := cmd.Flags().GetString("type") | ||
collectionID, _ := cmd.Flags().GetInt("collection_id") | ||
environmentID, _ := cmd.Flags().GetInt("environment_id") | ||
serviceName, _ := cmd.Flags().GetString("service_name") | ||
value, _ := cmd.Flags().GetString("value") | ||
secretID, _ := cmd.Flags().GetString("secret_id") | ||
outputName, _ := cmd.Flags().GetString("output_name") | ||
alias, _ := cmd.Flags().GetString("alias") | ||
|
||
if targetEnvName == "" { | ||
fmt.Println("Error: target_env_name is required") | ||
return | ||
} | ||
|
||
if collectionID == 0 && environmentID == 0 { | ||
fmt.Println("Error: either collection_id or environment_id is required") | ||
return | ||
} | ||
|
||
payload := map[string]interface{}{ | ||
"target_env_name": targetEnvName, | ||
"type": itemType, | ||
"service_name": serviceName, | ||
"value": value, | ||
"secret_id": secretID, | ||
"output_name": outputName, | ||
"alias": alias, | ||
} | ||
|
||
if collectionID != 0 { | ||
payload["collection_id"] = collectionID | ||
} | ||
if environmentID != 0 { | ||
payload["environment_id"] = environmentID | ||
} | ||
|
||
jsonPayload, err := json.Marshal(payload) | ||
if err != nil { | ||
fmt.Println("Error creating JSON payload:", err) | ||
return | ||
} | ||
|
||
createURL := fmt.Sprintf("%s/api/v1/environment_items", cocli.GetCoherenceDomain()) | ||
|
||
res, err := cocli.CoherenceApiRequest( | ||
"POST", | ||
createURL, | ||
bytes.NewBuffer(jsonPayload), | ||
) | ||
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)) | ||
}, | ||
} | ||
|
||
func init() { | ||
createEnvVarCmd.Flags().String("target_env_name", "", "Name of the environment variable (required)") | ||
createEnvVarCmd.Flags().String("type", "standard", "Type of the environment variable (standard, secret, output, alias)") | ||
createEnvVarCmd.Flags().Int("collection_id", 0, "ID of the collection") | ||
createEnvVarCmd.Flags().Int("environment_id", 0, "ID of the environment") | ||
createEnvVarCmd.Flags().String("service_name", "", "Name of the service") | ||
createEnvVarCmd.Flags().String("value", "", "Value of the environment variable") | ||
createEnvVarCmd.Flags().String("secret_id", "", "ID of the secret (for secret type)") | ||
createEnvVarCmd.Flags().String("output_name", "", "Name of the output (for output type)") | ||
createEnvVarCmd.Flags().String("alias", "", "Alias (for alias type)") | ||
|
||
createEnvVarCmd.MarkFlagRequired("target_env_name") | ||
} |
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,49 @@ | ||
package cocli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/coherenceplatform/cocli/pkg/cocli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var deleteEnvVarCmd = &cobra.Command{ | ||
Use: "delete <environment_item_id>", | ||
Short: "Delete an environment variable", | ||
Long: "Delete an environment variable by its ID.", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
environmentItemID := args[0] | ||
|
||
deleteURL := fmt.Sprintf( | ||
"%s/api/v1/environment_items/%s", | ||
cocli.GetCoherenceDomain(), | ||
environmentItemID, | ||
) | ||
|
||
res, err := cocli.CoherenceApiRequest( | ||
"DELETE", | ||
deleteURL, | ||
nil, | ||
) | ||
if err != nil { | ||
fmt.Println("Error making request:", err) | ||
return | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode == 204 { | ||
fmt.Printf("Environment variable with ID %s has been successfully deleted.\n", environmentItemID) | ||
return | ||
} | ||
|
||
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 |
---|---|---|
|
@@ -92,4 +92,4 @@ var deployCmd = &cobra.Command{ | |
|
||
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,18 @@ | ||
package cocli | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var envVarCmd = &cobra.Command{ | ||
Use: "env-vars", | ||
Short: "Coherence environment variable management commands", | ||
} | ||
|
||
func init() { | ||
envVarCmd.AddCommand(listEnvVarsCmd) | ||
envVarCmd.AddCommand(createEnvVarCmd) | ||
envVarCmd.AddCommand(deleteEnvVarCmd) | ||
envVarCmd.AddCommand(upsertEnvVarsCmd) | ||
envVarCmd.AddCommand(getEnvVarValueCmd) | ||
} |
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,44 @@ | ||
package cocli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/coherenceplatform/cocli/pkg/cocli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var getEnvVarValueCmd = &cobra.Command{ | ||
Use: "get-value <environment_item_id>", | ||
Short: "Get the value of an environment variable", | ||
Long: "Retrieve the value of a specific environment variable by its ID.", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
environmentItemID := args[0] | ||
|
||
getValueURL := fmt.Sprintf( | ||
"%s/api/v1/environment_items/%s/value", | ||
cocli.GetCoherenceDomain(), | ||
environmentItemID, | ||
) | ||
|
||
res, err := cocli.CoherenceApiRequest( | ||
"GET", | ||
getValueURL, | ||
nil, | ||
) | ||
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,80 @@ | ||
package cocli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/coherenceplatform/cocli/pkg/cocli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var listEnvVarsCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List environment variables", | ||
Long: "List all environment variables for a specific environment or collection.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
environmentID, _ := cmd.Flags().GetInt("environment_id") | ||
collectionID, _ := cmd.Flags().GetInt("collection_id") | ||
|
||
if environmentID == 0 && collectionID == 0 { | ||
fmt.Println("Error: Either environment_id or collection_id must be provided") | ||
return | ||
} | ||
|
||
if environmentID != 0 && collectionID != 0 { | ||
fmt.Println("Error: Please provide either environment_id or collection_id, not both") | ||
return | ||
} | ||
|
||
var url string | ||
if environmentID != 0 { | ||
url = fmt.Sprintf("%s/api/v1/environments/%d", cocli.GetCoherenceDomain(), environmentID) | ||
} else { | ||
url = fmt.Sprintf("%s/api/v1/collections/%d", cocli.GetCoherenceDomain(), collectionID) | ||
} | ||
|
||
res, err := cocli.CoherenceApiRequest("GET", url, nil) | ||
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 | ||
} | ||
|
||
var responseData map[string]interface{} | ||
err = json.Unmarshal(bodyBytes, &responseData) | ||
if err != nil { | ||
fmt.Println("Error parsing JSON response:", err) | ||
return | ||
} | ||
|
||
output := make(map[string]interface{}) | ||
|
||
if envItems, ok := responseData["environment_items"]; ok { | ||
output["environment_items"] = envItems | ||
} | ||
|
||
if managedEnvItems, ok := responseData["managed_environment_items"]; ok { | ||
output["managed_environment_items"] = managedEnvItems | ||
} | ||
|
||
outputJSON, err := json.Marshal(output) | ||
if err != nil { | ||
fmt.Println("Error creating JSON output:", err) | ||
return | ||
} | ||
|
||
fmt.Println(cocli.FormatJSONOutput(outputJSON)) | ||
}, | ||
} | ||
|
||
func init() { | ||
listEnvVarsCmd.Flags().Int("environment_id", 0, "ID of the environment") | ||
listEnvVarsCmd.Flags().Int("collection_id", 0, "ID of the collection") | ||
} |
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
Oops, something went wrong.