Skip to content

Commit

Permalink
Make guest name be an argument and not a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
anastop committed Oct 30, 2018
1 parent df7222c commit bc489dc
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ virgo makes use of the following utilities:

Provision a new VM called "foo":
```console
$ sudo virgo provision --config guest_config.json --provision-script provision.sh --initd-script initd.sh --guest foo
$ sudo virgo provision foo --config guest_config.json --provision-script provision.sh --initd-script initd.sh
```
"foo" will shutdown after provisioning.

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

Usage:
Expand Down
7 changes: 2 additions & 5 deletions cmd/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ Any previous specification of the VM is overriden by the new launch options.
The available launch options are presented in detail in virgo's main help message.
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
guest, err := cmd.Flags().GetString("guest")
if err != nil {
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
}
guest := args[0]

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

func init() {
launchCmd.Flags().StringP("guest", "g", "", "guest to launch")
launchCmd.Flags().StringP("config", "c", "", "JSON file containing the launch options")
rootCmd.AddCommand(launchCmd)
}
8 changes: 2 additions & 6 deletions cmd/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ var provisionCmd = &cobra.Command{
The available provisioning options are presented in detail in virgo's main help message.
The bash script can be any valid bash script and is executed with root permissions.
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
guest, err := cmd.Flags().GetString("guest")
if err != nil {
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
}
guest := args[0]

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

func init() {
provisionCmd.Flags().StringP("guest", "g", "", "guest to provision")
provisionCmd.Flags().StringP("provision-script", "p", "", "bash script to be used for provisioning")
provisionCmd.Flags().StringP("initd-script", "i", "", "bash script to be used in init.d")
provisionCmd.Flags().StringP("config", "c", "", "JSON file containing the provisioning options")
provisionCmd.MarkFlagRequired("config")
provisionCmd.MarkFlagRequired("guest")
rootCmd.AddCommand(provisionCmd)
}
8 changes: 2 additions & 6 deletions cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ var purgeCmd = &cobra.Command{
Use: "purge",
Short: "Fully destroy a VM by undefining it and removing its image",
Long: `Fully destroy a domain by undefining it and removing its image`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
guest, err := cmd.Flags().GetString("guest")
if err != nil {
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
}
guest := args[0]

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

func init() {
purgeCmd.Flags().StringP("guest", "g", "", "guest to purge")
purgeCmd.MarkFlagRequired("guest")
rootCmd.AddCommand(purgeCmd)
}
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var rootCmd = &cobra.Command{
Short: "virgo enables easy provisioning, configuration and management of Libvirt guests",
Long: `virgo enables easy provisioning, configuration and management of Libvirt guests.
All virgo commands accept a single argument, the name of the VM they act upon. Every command
has its own flags.
For provisioning a new VM image, you should specify a JSON config file with provisioning
options, along with a provisioning script to be executed on image's first boot.
Expand Down
2 changes: 1 addition & 1 deletion cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("ssh called")
fmt.Printf("ssh called, args %+v", args)
},
}

Expand Down
8 changes: 2 additions & 6 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ var startCmd = &cobra.Command{
Long: `Create a new VM instance from an already existing specification.
This implies that the VM should have been already launched at least once in the past,
either via 'provision' or 'launch'`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
guest, err := cmd.Flags().GetString("guest")
if err != nil {
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
}
guest := args[0]

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

func init() {
startCmd.Flags().StringP("guest", "g", "", "guest to start")
startCmd.MarkFlagRequired("guest")
rootCmd.AddCommand(startCmd)
}
8 changes: 2 additions & 6 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ var stopCmd = &cobra.Command{
Use: "stop",
Short: "Shut down a running VM instance",
Long: `Shut down a running VM instance. Keep its current definition intact.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
guest, err := cmd.Flags().GetString("guest")
if err != nil {
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
}
guest := args[0]

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

func init() {
stopCmd.Flags().StringP("guest", "g", "", "guest to stop")
stopCmd.MarkFlagRequired("guest")
rootCmd.AddCommand(stopCmd)
}
8 changes: 2 additions & 6 deletions cmd/undefine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ var undefineCmd = &cobra.Command{
Short: "Undefine a VM by removing its specification",
Long: `Undefine a VM by removing its specification. Its image is not affected.
If it's running, the domain is first stopped.'`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
guest, err := cmd.Flags().GetString("guest")
if err != nil {
return fmt.Errorf("failed to parse 'guest' argument: %v", err)
}
guest := args[0]

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

func init() {
undefineCmd.Flags().StringP("guest", "g", "", "guest to undefine")
undefineCmd.MarkFlagRequired("guest")
rootCmd.AddCommand(undefineCmd)
}

0 comments on commit bc489dc

Please sign in to comment.