Skip to content

Commit

Permalink
feat: Add command to launchDevly to update the project environment an…
Browse files Browse the repository at this point in the history
…d context (#433)

* Add command to launchDevly to update the project

Add command to launchDevly to update the project

* Update to read response
  • Loading branch information
cdelst authored Sep 16, 2024
1 parent d3622a0 commit 1415e4a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/dev_server/dev_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func NewDevServerCmd(client resources.Client, analyticsTrackerFn analytics.Track
cmd.AddCommand(NewSyncProjectCmd(client))
cmd.AddCommand(NewRemoveProjectCmd(client))
cmd.AddCommand(NewAddProjectCmd(client))
cmd.AddCommand(NewUpdateProjectCmd(client))

cmd.AddGroup(&cobra.Group{ID: "overrides", Title: "Override commands:"})
cmd.AddCommand(NewAddOverrideCmd(client))
Expand Down
6 changes: 6 additions & 0 deletions cmd/dev_server/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev_server

const (
ContextFlag = "context"
SourceEnvironmentFlag = "source"
)
106 changes: 100 additions & 6 deletions cmd/dev_server/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ func NewAddProjectCmd(client resources.Client) *cobra.Command {
_ = cmd.Flags().SetAnnotation(cliflags.ProjectFlag, "required", []string{"true"})
_ = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag))

cmd.Flags().String("source", "", "environment to copy flag values from")
_ = cmd.MarkFlagRequired("source")
_ = cmd.Flags().SetAnnotation("source", "required", []string{"true"})
_ = viper.BindPFlag("source", cmd.Flags().Lookup("source"))
cmd.Flags().String(SourceEnvironmentFlag, "", "environment to copy flag values from")
_ = cmd.MarkFlagRequired(SourceEnvironmentFlag)
_ = cmd.Flags().SetAnnotation(SourceEnvironmentFlag, "required", []string{"true"})
_ = viper.BindPFlag(SourceEnvironmentFlag, cmd.Flags().Lookup(SourceEnvironmentFlag))

cmd.Flags().String("context", "", `Stringified JSON representation of your context object ex. {"user": { "email": "youremail@gmail.com", "username": "foo", "key": "bar"}}`)
_ = viper.BindPFlag("context", cmd.Flags().Lookup("context"))
cmd.Flags().String(ContextFlag, "", `Stringified JSON representation of your context object ex. {"user": { "email": "youremail@gmail.com", "username": "foo", "key": "bar"}}`)
_ = viper.BindPFlag(ContextFlag, cmd.Flags().Lookup(ContextFlag))

return cmd
}
Expand Down Expand Up @@ -227,3 +227,97 @@ func addProject(client resources.Client) func(*cobra.Command, []string) error {
return nil
}
}

func NewUpdateProjectCmd(client resources.Client) *cobra.Command {
cmd := &cobra.Command{
GroupID: "projects",
Args: validators.Validate(),
Long: "update the specified project and its flag configuration with the LaunchDarkly Service",
RunE: updateProject(client),
Short: "update project",
Use: "update-project",
}

cmd.SetUsageTemplate(resourcescmd.SubcommandUsageTemplate())

cmd.Flags().String(cliflags.ProjectFlag, "", "The project key")
_ = cmd.MarkFlagRequired(cliflags.ProjectFlag)
_ = cmd.Flags().SetAnnotation(cliflags.ProjectFlag, "required", []string{"true"})
_ = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag))

cmd.Flags().String(SourceEnvironmentFlag, "", "environment to copy flag values from")
_ = viper.BindPFlag(SourceEnvironmentFlag, cmd.Flags().Lookup(SourceEnvironmentFlag))

cmd.Flags().String(ContextFlag, "", `Stringified JSON representation of your context object ex. {"user": { "email": "test@gmail.com", "username": "foo", "key": "bar"}}`)
_ = viper.BindPFlag(ContextFlag, cmd.Flags().Lookup(ContextFlag))

return cmd
}

type patchBody struct {
SourceEnvironmentKey string `json:"sourceEnvironmentKey,omitempty"`
Context map[string]interface{} `json:"context,omitempty"`
}

type patchResponse struct {
SourceEnvironmentKey string `json:"sourceEnvironmentKey"`
Context map[string]interface{} `json:"context"`
}

func updateProject(client resources.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
body := patchBody{}
if viper.IsSet(ContextFlag) {
contextString := viper.GetString(ContextFlag)
err := json.Unmarshal([]byte(contextString), &body.Context)
if err != nil {
return err
}
}

if viper.IsSet(SourceEnvironmentFlag) {
body.SourceEnvironmentKey = viper.GetString(SourceEnvironmentFlag)
}

jsonData, err := json.Marshal(body)
if err != nil {
return err
}

path := getDevServerUrl() + "/dev/projects/" + viper.GetString(cliflags.ProjectFlag)
res, err := client.MakeUnauthenticatedRequest(
"PATCH",
path,
jsonData,
)
if err != nil {
return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag))
}

var response patchResponse
err = json.Unmarshal(res, &response)
if err != nil {
return err
}

switch true {
case !viper.IsSet(ContextFlag) && !viper.IsSet(SourceEnvironmentFlag):
fmt.Fprint(cmd.OutOrStdout(), "No input given, project synced successfully\n")
case viper.IsSet(SourceEnvironmentFlag):
fmt.Fprintf(cmd.OutOrStdout(), "Source environment updated successfully to '%s'\n", response.SourceEnvironmentKey)
fallthrough
case viper.IsSet(ContextFlag):
// pretty print context
context, err := json.MarshalIndent(response.Context, "", " ")
if err != nil {
return err
}

fmt.Fprintf(cmd.OutOrStdout(), "Context updated successfully to:\n%s\n", string(context))
default:
fmt.Fprint(cmd.OutOrStdout(), "Project updated successfully\n")
}

return nil
}
}

0 comments on commit 1415e4a

Please sign in to comment.