Skip to content

Instantly share code, notes, and snippets.

@bl4ckcontact
Created July 9, 2015 05:59
Show Gist options
  • Save bl4ckcontact/7872928c7240cf52ec34 to your computer and use it in GitHub Desktop.
Save bl4ckcontact/7872928c7240cf52ec34 to your computer and use it in GitHub Desktop.
package command
import (
"flag"
"fmt"
"log"
"net"
"os"
"code.google.com/p/go-uuid/uuid"
"github.com/mitchellh/cli"
)
// Connection contains the session connection information
type Connection struct {
sessionID string
hostname string
serverAddress string
}
// NewConnection method
func NewConnection() cli.CommandFactory {
return func() (cli.Command, error) {
return &Connection{
sessionID: "",
hostname: "",
}, nil
}
}
// Run method initiates a new session with the scattermux server.
func (i *Connection) Run(args []string) int {
// Implements flags for the 'start' option
cmdFlags := flag.NewFlagSet("server", flag.ContinueOnError)
cmdFlags.Usage = func() { i.Help() }
cmdFlags.StringVar(&i.serverAddress, "server", "0.0.0.0", "The scattermux server address.")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
// Getting new UUID
u := uuid.NewUUID().String()
if u == "" {
log.Println("UUID could not be generated. Something went wrong.")
}
i.sessionID = u
// Getting hostname
n, err := os.Hostname()
if err != nil {
log.Println("Error: ", err)
return 1
}
i.hostname = n
// Verify server flag is valid
ip := net.ParseIP(i.serverAddress)
if ip == nil {
fmt.Println("ERROR: ", i.serverAddress, "is not a valid IP.")
return 1
}
fmt.Println("UUID Generated: ", i.sessionID) //just to verify the UUID
fmt.Println("Hostname: ", i.hostname)
fmt.Println(ip)
return 0
}
// Help provides a full description of the command.
func (i *Connection) Help() string {
return "Initiates a new session."
}
// Synopsis returns a brief description of the command.
func (i *Connection) Synopsis() string {
return "Initiates a new session."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment