Skip to content

Commit

Permalink
normalize vm name to allow "^vm-[0-9]+$" and "^[0-9]+$"
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Nov 29, 2020
1 parent f56934e commit 6cfa72d
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion command/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $ vermin commit vm_01 elk/elastic
`,
Run: func(cmd *cobra.Command, args []string) {

vmName := args[0]
vmName := normalizeVmName(args[0])
imageName := args[1]
override, _ := cmd.Flags().GetBool("override")

Expand Down
2 changes: 1 addition & 1 deletion command/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Execute remote command:
$ vermin exec vm_09 cat /etc/passwd
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
vmName := normalizeVmName(args[0])
command := strings.Join(args[1:], " ")

err := vms.Exec(vmName, command)
Expand Down
2 changes: 1 addition & 1 deletion command/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var guiCmd = &cobra.Command{
$ vermin gui vm_02
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
vmName := normalizeVmName(args[0])
nocheck, _ := cmd.Flags().GetBool("nocheck")
if err := vms.GUI(vmName, nocheck); err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion command/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ To purge the IP cache:
$ vermin ip vm_05 -p
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
vmName := normalizeVmName(args[0])
purge, _ := cmd.Flags().GetBool("purge")
global, _ := cmd.Flags().GetBool("global")

Expand Down
2 changes: 1 addition & 1 deletion command/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Forward vm port 4040 to local port 40040 and ports in range (8080 to 8088) to ra
$ vermin port vm_01 40040:4040 9080-9088:8080-8088
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
vmName := normalizeVmName(args[0])
command := strings.Join(args[1:], " ")

err := vms.PortForward(vmName, command)
Expand Down
2 changes: 1 addition & 1 deletion command/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var removeCmd = &cobra.Command{
for _, vmName := range args {
force, _ := cmd.Flags().GetBool("force")

err := vms.Remove(vmName, force)
err := vms.Remove(normalizeVmName(vmName), force)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions command/remove_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ var removeImageCmd = &cobra.Command{
Short: "Remove one or more Image",
Long: `Remove one or more Image`,
Run: func(cmd *cobra.Command, args []string) {
for _, vmName := range args {
for _, imageName := range args {
//force, _ := cmd.Flags().GetBool("force")

err := images.Remove(vmName)
err := images.Remove(imageName)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions command/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ var restartCmd = &cobra.Command{
Long: `Restart one or more VMs`,
Run: func(cmd *cobra.Command, args []string) {
for _, vmName := range args {
if err := vms.Stop(vmName); err != nil {
if err := vms.Stop(normalizeVmName(vmName)); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := vms.Start(vmName); err != nil {
if err := vms.Start(normalizeVmName(vmName)); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion command/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var sshCmd = &cobra.Command{
$ vermin ssh vm_02
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
vmName := normalizeVmName(args[0])
if err := vms.SecureShell(vmName); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var startCmd = &cobra.Command{
Long: `Start one or more stopped VMs`,
Run: func(cmd *cobra.Command, args []string) {
for _, vmName := range args {
err := vms.Start(vmName)
err := vms.Start(normalizeVmName(vmName))
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion command/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var stopCmd = &cobra.Command{
Long: `Stop one or more running VMs`,
Run: func(cmd *cobra.Command, args []string) {
for _, vmName := range args {
err := vms.Stop(vmName)
err := vms.Stop(normalizeVmName(vmName))
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion command/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var tagCmd = &cobra.Command{
You can tag a VM as many times as you want
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
vmName := normalizeVmName(args[0])
tag := args[1]

remove, _ := cmd.Flags().GetBool("remove")
Expand Down
2 changes: 1 addition & 1 deletion command/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To change the VM to use 2 cores and 512MB memory
$ vermin update vm_01 --cpus 2 --mem 512
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
vmName := normalizeVmName(args[0])
var script string
if len(args) > 1 {
script = args[1]
Expand Down
18 changes: 18 additions & 0 deletions command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import (
"github.com/mhewedy/vermin/config"
"github.com/spf13/cobra"
"os"
"regexp"
"strings"
)

var vmNameRegexDash = regexp.MustCompile("^vm-[0-9]+$")
var vmNameRegexNoVM = regexp.MustCompile("^[0-9]+$")

func checkFilePath(path string) {
if _, err := os.Stat(path); err != nil {
fmt.Println("file not found", path)
Expand All @@ -24,3 +29,16 @@ func exitOnError(err error) {
os.Exit(1)
}
}

func normalizeVmName(vmName string) string {

if vmNameRegexDash.MatchString(vmName) {
return strings.ReplaceAll(vmName, "-", "_")
}

if vmNameRegexNoVM.MatchString(vmName) {
return fmt.Sprintf("vm_%s", vmName)
}

return vmName
}

0 comments on commit 6cfa72d

Please sign in to comment.