Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
working on vmware client
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Mar 24, 2018
1 parent f743a7c commit 7ac750f
Show file tree
Hide file tree
Showing 6 changed files with 905 additions and 801 deletions.
67 changes: 58 additions & 9 deletions clients/vmware/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,73 @@
package cmd

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"path/filepath"

homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
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.`,
Short: "List all running VMs",
Run: func(cmd *cobra.Command, args []string) {
// TODO: Work your own magic here
fmt.Println("list called")
// d := virtualbox.NewDriver("", "")
// outList, err := d.List()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Print(outList)

host := viper.GetString("server.host")
port := viper.GetString("server.port")

home, err := homedir.Dir()
if err != nil {
log.Fatal(errors.Wrap(err, "could not detect users home directory"))
}
// Create client
caCert, err := ioutil.ReadFile(filepath.Join(home, ".vmproxy", "cert.pem"))
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

// cert, err := tls.LoadX509KeyPair("client.crt", "client.key")
// if err != nil {
// log.Fatal(err)
// }

client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
// Certificates: []tls.Certificate{cert},
},
},
}

// Create request
req, err := http.NewRequest("GET", "https://"+host+":"+port+"/vmware/list", nil)

// Fetch Request
resp, err := client.Do(req)
assert(err)

// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)

// Display Results
fmt.Print(string(respBody))
},
}

Expand Down
21 changes: 14 additions & 7 deletions clients/vmware/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"
Expand All @@ -34,9 +35,9 @@ examples and usage of using your application. For example:
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.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand All @@ -48,14 +49,20 @@ func Execute() {
}
}

func assert(err error) {
if err != nil {
log.Fatal(err)
}
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.vmrun.yaml)")
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.vmware.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
Expand All @@ -67,9 +74,9 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
}

viper.SetConfigName(".vmrun") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
viper.SetConfigName(".vmware") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
Expand Down
67 changes: 58 additions & 9 deletions clients/vmware/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,73 @@
package cmd

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"path/filepath"

homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
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.`,
Short: "List all running VMs",
Run: func(cmd *cobra.Command, args []string) {
// TODO: Work your own magic here
fmt.Println("start called")
// d := virtualbox.NewDriver("", "")
// outList, err := d.List()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Print(outList)

host := viper.GetString("server.host")
port := viper.GetString("server.port")

home, err := homedir.Dir()
if err != nil {
log.Fatal(errors.Wrap(err, "could not detect users home directory"))
}
// Create client
caCert, err := ioutil.ReadFile(filepath.Join(home, ".vmproxy", "cert.pem"))
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

// cert, err := tls.LoadX509KeyPair("client.crt", "client.key")
// if err != nil {
// log.Fatal(err)
// }

client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
// Certificates: []tls.Certificate{cert},
},
},
}

// Create request
req, err := http.NewRequest("GET", "https://"+host+":"+port+"/vmware/start", nil)

// Fetch Request
resp, err := client.Do(req)
assert(err)

// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)

// Display Results
fmt.Print(string(respBody))
},
}

Expand Down
Loading

0 comments on commit 7ac750f

Please sign in to comment.