Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevenson Jean-Pierre committed Jul 27, 2016
1 parent 3c271ac commit 621f79f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
27 changes: 13 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ var (
_ = hosts.Command("list", "List vpn hosts")
_ = hosts.Command("refresh", "Refreshes resources")
//Profile Commands
profiles = kingpin.Command("profile","Commands related to VPN connection profiles")
profiles = kingpin.Command("profile", "Commands related to VPN connection profiles")
_ = profiles.Command("list", "List vpn connection profiles")
addProfilecmd = profiles.Command("add","Add new profile to existing set")
newProfile = addProfilecmd.Arg("profile","Name of profile to add").Required().String()
addProfilecmd = profiles.Command("add", "Add new profile to existing set")
newProfile = addProfilecmd.Arg("profile", "Name of profile to add").Required().String()
//Command Regex Section
connectRegex = regexp.MustCompile(`^connect`)
hostCommadRegex = regexp.MustCompile(`^host`)
profileCommandRegex = regexp.MustCompile(`^profile`)
//Global Vars
resourcePath = path.Join(os.Getenv("HOME") ,".vpn_host_manager")
resourcePath = path.Join(os.Getenv("HOME"), ".vpn_host_manager")
)

func permissionCheck() {
output,_ := exec.Command("id","-u").Output()
trimmedOutput := strings.Trim(string(output),"\n")
output, _ := exec.Command("id", "-u").Output()
trimmedOutput := strings.Trim(string(output), "\n")
if string(trimmedOutput) != "0" {
log.Fatal("Please rerun as root or with sudo")
}
Expand All @@ -50,7 +50,6 @@ func listVpnHosts() {
printVPNHostList()
}


func hostFunctions(hostMethod string) {
switch hostMethod {
case "host list":
Expand All @@ -67,18 +66,18 @@ func profileFunctions(profileMethod string) {
case "profile add":
addProfile(*newProfile)
default:
log.Fatalf("not sure what to do with command: %s",profileMethod)
log.Fatalf("not sure what to do with command: %s", profileMethod)
}

}

func connectVPN(profileName string,vpnIdentifier string) {
startConnection(vpnIdentifier,profileName)
func connectVPN(profileName string, vpnIdentifier string) {
startConnection(vpnIdentifier, profileName)
}

func setupDirectories() {
if _, err := os.Stat(resourcePath); os.IsNotExist(err) {
error := os.Mkdir(resourcePath,0755)
error := os.Mkdir(resourcePath, 0755)
if error != nil {
log.Fatalf("encountered error during setup, %s", error)
}
Expand All @@ -87,19 +86,19 @@ func setupDirectories() {

func main() {
permissionCheck()
setupDirectories()
setupDirectories()
parsedArg := kingpin.Parse()
switch {
case hostCommadRegex.MatchString(parsedArg):
hostFunctions(parsedArg)
case profileCommandRegex.MatchString(parsedArg):
profileFunctions(parsedArg)
case connectRegex.MatchString(parsedArg):
connectVPN(*profile,*vpn)
connectVPN(*profile, *vpn)
default:
//if we are in this error block it is because we have established
//a command for the provided text, but have not specified a regex
//for handling it.
log.Fatalf("Command signature not recognized: %s",parsedArg)
log.Fatalf("Command signature not recognized: %s", parsedArg)
}
}
16 changes: 8 additions & 8 deletions vpn_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func updateManagedVPNHost(vpnHost vpnInstance) {
addManagedVPNHost(vpnHost)
}

func needsDisconnection() bool{
func needsDisconnection() bool {
var disconnect bool
if connectionEstablished() {
if sameConnection {
Expand Down Expand Up @@ -194,7 +194,7 @@ func connectionEstablished() bool {

func updateRouting(vpnHost vpnInstance) {
print("updating route table\n")
_, err := exec.Command("route","-v", "add", "-net", vpnHost.VpcCidr, "-interface ppp0").Output()
_, err := exec.Command("route", "-v", "add", "-net", vpnHost.VpcCidr, "-interface ppp0").Output()
if err != nil {
if weirdRouteExitCodeRegex.MatchString(err.Error()) {
return
Expand All @@ -204,27 +204,27 @@ func updateRouting(vpnHost vpnInstance) {
}
}

func selectVPNHost(identifier string) vpnInstance{
func selectVPNHost(identifier string) vpnInstance {
vpnHostsList := readHostsJSONFile()
if vpcUIDRegex.MatchString(identifier) {
fmt.Println("Connecting to VPN by UID")
for _,host := range vpnHostsList {
for _, host := range vpnHostsList {
if host.VpcID == identifier {
return host
}
}
}
if vpcIndexRegex.MatchString(identifier) {
fmt.Println("Connecting to VPN by ID#")
for index,host := range vpnHostsList {
if strconv.Itoa(index) == identifier {
for index, host := range vpnHostsList {
if strconv.Itoa(index) == identifier {
return host
}
}
}
fmt.Println("Connecting to VPN by instnace Name")
for _,host := range vpnHostsList {
if host.Name == identifier {
for _, host := range vpnHostsList {
if host.Name == identifier {
return host
}
}
Expand Down
24 changes: 12 additions & 12 deletions vpn_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
)

var (
vpnProfileFields = []string{"ID#","Name","Username"}
vpnProfileFields = []string{"ID#", "Name", "Username"}
vpnProfileFilePath = path.Join(resourcePath, "vpn_profiles.json")
vpnProfiles = loadProfileFile()
vpnProfiles = loadProfileFile()
)

type vpnProfile struct {
Expand All @@ -43,7 +43,7 @@ func writeProfileFile(profileList []vpnProfile) {
fmt.Println(err)
return
}
fmt.Printf("Writing profile file to %s\n",vpnProfileFilePath)
fmt.Printf("Writing profile file to %s\n", vpnProfileFilePath)
writeError := ioutil.WriteFile(vpnProfileFilePath, profileJSON, 0755)
if writeError != nil {
fmt.Print("Could not write profile file\n")
Expand Down Expand Up @@ -80,16 +80,16 @@ func selectVPNProfileDetails(profileName string) vpnProfile {
}

func detectDuplicateName(providedName string) {
for _,profile := range vpnProfiles {
for _, profile := range vpnProfiles {
if profile.Name == providedName {
log.Fatalf("profile name %s: Already present, please select another name",providedName)
log.Fatalf("profile name %s: Already present, please select another name", providedName)
}
}
}

func detailCapture(attr string) string{
func detailCapture(attr string) string {
var response string
fmt.Printf("%s ",attr)
fmt.Printf("%s ", attr)
_, err := fmt.Scanln(&response)
if err != nil {
if err.Error() == "unexpected newline" {
Expand All @@ -100,7 +100,7 @@ func detailCapture(attr string) string{
return response
}

func confirm(username string,password string,psk string) bool{
func confirm(username string, password string, psk string) bool {
var returnvar bool
confirmation := detailCapture("Save Profile? [y/n]:")
switch confirmation {
Expand All @@ -109,20 +109,20 @@ func confirm(username string,password string,psk string) bool{
case "n":
main()
default:
confirm(username,password,psk)
confirm(username, password, psk)
}
return returnvar
}

func addProfile(profileName string) {
detectDuplicateName(profileName)
fmt.Printf("Please enter the following values to configure VPN profile %s\n",profileName)
fmt.Printf("Please enter the following values to configure VPN profile %s\n", profileName)
username := detailCapture("USERNAME:")
password := detailCapture("PASSWORD:")
psk := detailCapture("PSK:")
if confirm(username,password,psk) {
if confirm(username, password, psk) {
var updated = []vpnProfile{}
updated = append(vpnProfiles,vpnProfile{Name:profileName,UserName:username,PassWord:password,Psk:psk})
updated = append(vpnProfiles, vpnProfile{Name:profileName, UserName:username, PassWord:password, Psk:psk})
writeProfileFile(updated)
}
}
Expand Down

0 comments on commit 621f79f

Please sign in to comment.