Skip to content

Commit

Permalink
Implement /devstate/*command
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jun 29, 2023
1 parent 75cf6e3 commit ac9ab1e
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 9 deletions.
74 changes: 74 additions & 0 deletions pkg/apiserver-impl/devstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,77 @@ func (s *DefaultApiService) DevstateResourceResourceNameDelete(ctx context.Conte
}
return openapi.Response(http.StatusOK, newContent), nil
}

func (s *DefaultApiService) DevstateApplyCommandPost(ctx context.Context, command openapi.DevstateApplyCommandPostRequest) (openapi.ImplResponse, error) {
newContent, err := s.devfileState.AddApplyCommand(
command.Name,
command.Component,
)
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
Message: fmt.Sprintf("Error adding the Apply command: %s", err),
}), nil
}
return openapi.Response(http.StatusOK, newContent), nil
}

func (s *DefaultApiService) DevstateApplyCommandCommandNameDelete(ctx context.Context, commandName string) (openapi.ImplResponse, error) {
newContent, err := s.devfileState.DeleteCommand(commandName)
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
Message: fmt.Sprintf("Error deleting the Apply command: %s", err),
}), nil
}
return openapi.Response(http.StatusOK, newContent), nil
}

func (s *DefaultApiService) DevstateCompositeCommandPost(ctx context.Context, command openapi.DevstateCompositeCommandPostRequest) (openapi.ImplResponse, error) {
newContent, err := s.devfileState.AddCompositeCommand(
command.Name,
command.Parallel,
command.Commands,
)
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
Message: fmt.Sprintf("Error adding the Composite command: %s", err),
}), nil
}
return openapi.Response(http.StatusOK, newContent), nil

}

func (s *DefaultApiService) DevstateCompositeCommandCommandNameDelete(ctx context.Context, commandName string) (openapi.ImplResponse, error) {
newContent, err := s.devfileState.DeleteCommand(commandName)
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
Message: fmt.Sprintf("Error deleting the Composite command: %s", err),
}), nil
}
return openapi.Response(http.StatusOK, newContent), nil
}

func (s *DefaultApiService) DevstateExecCommandPost(ctx context.Context, command openapi.DevstateExecCommandPostRequest) (openapi.ImplResponse, error) {
newContent, err := s.devfileState.AddExecCommand(
command.Name,
command.Component,
command.CommandLine,
command.WorkingDir,
command.HotReloadCapable,
)
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
Message: fmt.Sprintf("Error adding the Exec command: %s", err),
}), nil
}
return openapi.Response(http.StatusOK, newContent), nil
}

func (s *DefaultApiService) DevstateExecCommandCommandNameDelete(ctx context.Context, commandName string) (openapi.ImplResponse, error) {
newContent, err := s.devfileState.DeleteCommand(commandName)
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
Message: fmt.Sprintf("Error deleting the Exec command: %s", err),
}), nil
}
return openapi.Response(http.StatusOK, newContent), nil
}
91 changes: 91 additions & 0 deletions pkg/apiserver-impl/devstate/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package devstate

import (
"fmt"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
)

func (o *DevfileState) AddExecCommand(name string, component string, commandLine string, workingDir string, hotReloadCapable bool) (DevfileContent, error) {
command := v1alpha2.Command{
Id: name,
CommandUnion: v1alpha2.CommandUnion{
Exec: &v1alpha2.ExecCommand{
Component: component,
CommandLine: commandLine,
WorkingDir: workingDir,
HotReloadCapable: &hotReloadCapable,
},
},
}
err := o.Devfile.Data.AddCommands([]v1alpha2.Command{command})
if err != nil {
return DevfileContent{}, err
}
return o.GetContent()
}

func (o *DevfileState) AddApplyCommand(name string, component string) (DevfileContent, error) {
command := v1alpha2.Command{
Id: name,
CommandUnion: v1alpha2.CommandUnion{
Apply: &v1alpha2.ApplyCommand{
Component: component,
},
},
}
err := o.Devfile.Data.AddCommands([]v1alpha2.Command{command})
if err != nil {
return DevfileContent{}, err
}
return o.GetContent()
}

func (o *DevfileState) AddCompositeCommand(name string, parallel bool, commands []string) (DevfileContent, error) {
command := v1alpha2.Command{
Id: name,
CommandUnion: v1alpha2.CommandUnion{
Composite: &v1alpha2.CompositeCommand{
Parallel: &parallel,
Commands: commands,
},
},
}
err := o.Devfile.Data.AddCommands([]v1alpha2.Command{command})
if err != nil {
return DevfileContent{}, err
}
return o.GetContent()
}

func (o *DevfileState) DeleteCommand(name string) (DevfileContent, error) {
err := o.checkCommandUsed(name)
if err != nil {
return DevfileContent{}, fmt.Errorf("error deleting command %q: %w", name, err)
}
err = o.Devfile.Data.DeleteCommand(name)
if err != nil {
return DevfileContent{}, err
}
return o.GetContent()
}

func (o *DevfileState) checkCommandUsed(name string) error {
commands, err := o.Devfile.Data.GetCommands(common.DevfileOptions{
CommandOptions: common.CommandOptions{
CommandType: v1alpha2.CompositeCommandType,
},
})
if err != nil {
return err
}
for _, command := range commands {
for _, subcommand := range command.Composite.Commands {
if subcommand == name {
return fmt.Errorf("command %q is used by composite command %q", name, command.Id)
}
}
}
return nil
}
18 changes: 9 additions & 9 deletions pkg/apiserver-impl/devstate/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ type Command struct {
Group string
Default bool
Type string
Exec ExecCommand
Apply ApplyCommand
Image ImageCommand
Composite CompositeCommand
Exec *ExecCommand
Apply *ApplyCommand
Image *ImageCommand
Composite *CompositeCommand
}

type ExecCommand struct {
Expand Down Expand Up @@ -118,7 +118,7 @@ func (o *DevfileState) GetContent() (DevfileContent, error) {

commands, err := o.getCommands()
if err != nil {
return DevfileContent{}, errors.New("error getting commands")
return DevfileContent{}, fmt.Errorf("error getting commands: %w", err)
}

containers, err := o.getContainers()
Expand Down Expand Up @@ -188,7 +188,7 @@ func (o *DevfileState) getCommands() ([]Command, error) {

if command.Exec != nil {
newCommand.Type = "exec"
newCommand.Exec = ExecCommand{
newCommand.Exec = &ExecCommand{
Component: command.Exec.Component,
CommandLine: command.Exec.CommandLine,
WorkingDir: command.Exec.WorkingDir,
Expand All @@ -209,21 +209,21 @@ func (o *DevfileState) getCommands() ([]Command, error) {
component := components[0]
if component.Kubernetes != nil || component.Openshift != nil {
newCommand.Type = "apply"
newCommand.Apply = ApplyCommand{
newCommand.Apply = &ApplyCommand{
Component: command.Apply.Component,
}
}
if component.Image != nil {
newCommand.Type = "image"
newCommand.Image = ImageCommand{
newCommand.Image = &ImageCommand{
Component: command.Apply.Component,
}
}
}

if command.Composite != nil {
newCommand.Type = "composite"
newCommand.Composite = CompositeCommand{
newCommand.Composite = &CompositeCommand{
Commands: command.Composite.Commands,
Parallel: pointer.BoolDeref(command.Composite.Parallel, false),
}
Expand Down

0 comments on commit ac9ab1e

Please sign in to comment.