Skip to content

Commit 5c93e22

Browse files
committed
Make guest name be an argument and not a flag
1 parent 654ba6b commit 5c93e22

File tree

9 files changed

+18
-38
lines changed

9 files changed

+18
-38
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ virgo makes use of the following utilities:
4444

4545
Provision a new VM called "foo":
4646
```console
47-
$ sudo virgo provision --config guest_config.json --provision-script provision.sh --initd-script initd.sh --guest foo
47+
$ sudo virgo provision foo --config guest_config.json --provision-script provision.sh --initd-script initd.sh
4848
```
4949
"foo" will shutdown after provisioning.
5050

5151
Edit `guest_config.json` to change VM's parameters (e.g. #vCPUs), and launch "foo":
5252
```console
53-
$ sudo virgo --config guest_config.json --guest foo
53+
$ sudo virgo launch foo --config guest_config.json
5454
```
5555

5656
Usage:

cmd/launch.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ Any previous specification of the VM is overriden by the new launch options.
1818
1919
The available launch options are presented in detail in virgo's main help message.
2020
`,
21+
Args: cobra.ExactArgs(1),
2122
RunE: func(cmd *cobra.Command, args []string) error {
22-
guest, err := cmd.Flags().GetString("guest")
23-
if err != nil {
24-
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
25-
}
23+
guest := args[0]
2624

2725
conf, err := cmd.Flags().GetString("config")
2826
if err != nil {
@@ -64,7 +62,6 @@ The available launch options are presented in detail in virgo's main help messag
6462
}
6563

6664
func init() {
67-
launchCmd.Flags().StringP("guest", "g", "", "guest to launch")
6865
launchCmd.Flags().StringP("config", "c", "", "JSON file containing the launch options")
6966
rootCmd.AddCommand(launchCmd)
7067
}

cmd/provision.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ var provisionCmd = &cobra.Command{
1818
The available provisioning options are presented in detail in virgo's main help message.
1919
The bash script can be any valid bash script and is executed with root permissions.
2020
`,
21+
Args: cobra.ExactArgs(1),
2122
RunE: func(cmd *cobra.Command, args []string) error {
22-
guest, err := cmd.Flags().GetString("guest")
23-
if err != nil {
24-
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
25-
}
23+
guest := args[0]
2624

2725
provisionScript, err := cmd.Flags().GetString("provision-script")
2826
if err != nil {
@@ -90,11 +88,9 @@ The bash script can be any valid bash script and is executed with root permissio
9088
}
9189

9290
func init() {
93-
provisionCmd.Flags().StringP("guest", "g", "", "guest to provision")
9491
provisionCmd.Flags().StringP("provision-script", "p", "", "bash script to be used for provisioning")
9592
provisionCmd.Flags().StringP("initd-script", "i", "", "bash script to be used in init.d")
9693
provisionCmd.Flags().StringP("config", "c", "", "JSON file containing the provisioning options")
9794
provisionCmd.MarkFlagRequired("config")
98-
provisionCmd.MarkFlagRequired("guest")
9995
rootCmd.AddCommand(provisionCmd)
10096
}

cmd/purge.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ var purgeCmd = &cobra.Command{
1212
Use: "purge",
1313
Short: "Fully destroy a VM by undefining it and removing its image",
1414
Long: `Fully destroy a domain by undefining it and removing its image`,
15+
Args: cobra.ExactArgs(1),
1516
RunE: func(cmd *cobra.Command, args []string) error {
16-
guest, err := cmd.Flags().GetString("guest")
17-
if err != nil {
18-
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
19-
}
17+
guest := args[0]
2018

2119
l, err := virgo.NewLibvirtConn()
2220
if err != nil {
@@ -40,7 +38,5 @@ var purgeCmd = &cobra.Command{
4038
}
4139

4240
func init() {
43-
purgeCmd.Flags().StringP("guest", "g", "", "guest to purge")
44-
purgeCmd.MarkFlagRequired("guest")
4541
rootCmd.AddCommand(purgeCmd)
4642
}

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ var rootCmd = &cobra.Command{
1313
Short: "virgo enables easy provisioning, configuration and management of Libvirt guests",
1414
Long: `virgo enables easy provisioning, configuration and management of Libvirt guests.
1515
16+
All virgo commands accept a single argument, the name of the VM they act upon. Every command
17+
has its own flags.
18+
1619
For provisioning a new VM image, you should specify a JSON config file with provisioning
1720
options, along with a provisioning script to be executed on image's first boot.
1821

cmd/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Cobra is a CLI library for Go that empowers applications.
1717
This application is a tool to generate the needed files
1818
to quickly create a Cobra application.`,
1919
Run: func(cmd *cobra.Command, args []string) {
20-
fmt.Println("ssh called")
20+
fmt.Printf("ssh called, args %+v", args)
2121
},
2222
}
2323

cmd/start.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ var startCmd = &cobra.Command{
1414
Long: `Create a new VM instance from an already existing specification.
1515
This implies that the VM should have been already launched at least once in the past,
1616
either via 'provision' or 'launch'`,
17+
Args: cobra.ExactArgs(1),
1718
RunE: func(cmd *cobra.Command, args []string) error {
18-
guest, err := cmd.Flags().GetString("guest")
19-
if err != nil {
20-
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
21-
}
19+
guest := args[0]
2220

2321
l, err := virgo.NewLibvirtConn()
2422
if err != nil {
@@ -38,7 +36,5 @@ either via 'provision' or 'launch'`,
3836
}
3937

4038
func init() {
41-
startCmd.Flags().StringP("guest", "g", "", "guest to start")
42-
startCmd.MarkFlagRequired("guest")
4339
rootCmd.AddCommand(startCmd)
4440
}

cmd/stop.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ var stopCmd = &cobra.Command{
1212
Use: "stop",
1313
Short: "Shut down a running VM instance",
1414
Long: `Shut down a running VM instance. Keep its current definition intact.`,
15+
Args: cobra.ExactArgs(1),
1516
RunE: func(cmd *cobra.Command, args []string) error {
16-
guest, err := cmd.Flags().GetString("guest")
17-
if err != nil {
18-
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
19-
}
17+
guest := args[0]
2018

2119
l, err := virgo.NewLibvirtConn()
2220
if err != nil {
@@ -36,7 +34,5 @@ var stopCmd = &cobra.Command{
3634
}
3735

3836
func init() {
39-
stopCmd.Flags().StringP("guest", "g", "", "guest to stop")
40-
stopCmd.MarkFlagRequired("guest")
4137
rootCmd.AddCommand(stopCmd)
4238
}

cmd/undefine.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ var undefineCmd = &cobra.Command{
1414
Short: "Undefine a VM by removing its specification",
1515
Long: `Undefine a VM by removing its specification. Its image is not affected.
1616
If it's running, the domain is first stopped.'`,
17+
Args: cobra.ExactArgs(1),
1718
RunE: func(cmd *cobra.Command, args []string) error {
18-
guest, err := cmd.Flags().GetString("guest")
19-
if err != nil {
20-
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
21-
}
19+
guest := args[0]
2220

2321
l, err := virgo.NewLibvirtConn()
2422
if err != nil {
@@ -38,7 +36,5 @@ If it's running, the domain is first stopped.'`,
3836
}
3937

4038
func init() {
41-
undefineCmd.Flags().StringP("guest", "g", "", "guest to undefine")
42-
undefineCmd.MarkFlagRequired("guest")
4339
rootCmd.AddCommand(undefineCmd)
4440
}

0 commit comments

Comments
 (0)