Skip to content

Commit

Permalink
check if machine is already exists in the project
Browse files Browse the repository at this point in the history
  • Loading branch information
defektive committed Oct 11, 2024
1 parent d50d9cb commit 8fa05a4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46
github.com/alecthomas/chroma/v2 v2.14.0/go.mod h1:QolEbTfmUHIMVpBqxeDnNBj2uoeI4EbYP4i6n68SG4I=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/analog-substance/util v1.0.3 h1:qMlqWtJDvrREv1ZKkJ0qvDFZ+VwpwTeN1UBLc5h0bP8=
github.com/analog-substance/util v1.0.3/go.mod h1:J0ESO2Rd8bhJgEdn/U7CIlNGR72GaH7FcMJXBDqfKsk=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/project_add_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var projectAddCmd = &cobra.Command{
vmName, _ := cmd.Flags().GetString("name")
serviceProvider, _ := cmd.Flags().GetString("service")
vmImage, _ := cmd.Flags().GetString("image")
noApply, _ := cmd.Flags().GetBool("no-apply")

project, err := carbonObj.GetProject(projectName)
if err != nil {
Expand All @@ -27,7 +28,7 @@ var projectAddCmd = &cobra.Command{
Name: vmName,
Provider: serviceProvider,
Image: vmImage,
})
}, noApply)
if err != nil {
fmt.Printf("Failed to save project: %s\n", err)
}
Expand All @@ -38,6 +39,7 @@ func init() {
projectCmd.AddCommand(projectAddCmd)
projectAddCmd.PersistentFlags().StringP("name", "n", "", "Name of the VM to add.")
projectAddCmd.PersistentFlags().StringP("image", "i", "", "Name of the image to use.")
projectAddCmd.PersistentFlags().BoolP("no-apply", "N", false, "Do not run terraform apply.")

addServiceProviderFlag(projectAddCmd)

Expand Down
9 changes: 8 additions & 1 deletion pkg/models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ func (d *Project) TerraformApply() error {
return builder.Cmd(terraformPath, args...).Dir(d.buildPath).Interactive().Run()
}

func (d *Project) AddMachine(machine *types.ProjectMachine) error {
func (d *Project) AddMachine(machine *types.ProjectMachine, noApply bool) error {
_, err := d.GetConfig()
if err != nil {
return err
}

for _, existingMachine := range d.config.Machines {
if existingMachine.Name == machine.Name {
return fmt.Errorf("machine '%s' already exists", machine.Name)
}
}

d.config.Machines = append(d.config.Machines, machine)
err = d.SaveConfig()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package types
type Project interface {
Name() string
TerraformApply() error
AddMachine(machine *ProjectMachine) error
AddMachine(machine *ProjectMachine, noApply bool) error
}

0 comments on commit 8fa05a4

Please sign in to comment.