Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevenson Jean-Pierre committed Mar 30, 2017
1 parent f1b6822 commit 177f35e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
5 changes: 4 additions & 1 deletion aws_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func readAWSProfileFile() ([]string, error) {
os.Exit(1)
}
var awsProfiles []string
json.Unmarshal(file, &awsProfiles)
err := json.Unmarshal(file, &awsProfiles)
if err != nil {
log.Fatal("could not read AWS profile file")
}
return awsProfiles, nil
}

Expand Down
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"os"
"log"
"path"
"os/exec"
"strings"
"os/user"
"fmt"
)

Expand All @@ -39,14 +38,17 @@ var (
profileCommandRegex = regexp.MustCompile(`^profile`)
disconnectCommandRegex = regexp.MustCompile(`^disconnect`)
//Global Vars
cliVersion = "0.0.5"
cliVersion = "0.0.6"
resourcePath = path.Join(os.Getenv("HOME"), ".vpn_host_manager")
DEBUG = false
)

func permissionCheck() {
output, _ := exec.Command("id", "-u").Output()
trimmedOutput := strings.Trim(string(output), "\n")
if string(trimmedOutput) != "0" {
cu, err := user.Current()
if err != nil {
log.Fatalln("Could not retrieve user information:",err.Error())
}
if cu.Uid != "0" {
log.Fatal("Please rerun as root or with sudo")
}
}
Expand Down Expand Up @@ -87,7 +89,7 @@ func disconnectVPN() {

func setupDirectories() {
if _, err := os.Stat(resourcePath); os.IsNotExist(err) {
error := os.Mkdir(resourcePath, 0755)
error := os.Mkdir(resourcePath, 0700)
if error != nil {
log.Fatalf("encountered error during setup, %s", error)
}
Expand Down
10 changes: 8 additions & 2 deletions vpn_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ func addManagedVPNHost(vpnHost vpnInstance) {
if err != nil {
log.Fatal("Could not read hostfile")
}
hosts.Add(vpnHost.PublicIP, managedHost)
err = hosts.Add(vpnHost.PublicIP, managedHost)
if err != nil {
log.Fatal("Could not add entry to host file")
}
if err := hosts.Flush(); err != nil {
log.Fatalf("Error writing host entry %s", err)
}
Expand All @@ -115,7 +118,10 @@ func removeExistingHost() {
for _, hostLine := range hosts.Lines {
if existingHostRegex.MatchString(hostLine.Raw) {
fmt.Printf("Removing `%s` from hostfile\n", hostLine.Raw)
hosts.Remove(hostLine.IP, hostLine.Hosts[0])
err = hosts.Remove(hostLine.IP, hostLine.Hosts[0])
if err != nil {
log.Fatal("Could not remove old host entry")
}
}
}
if err := hosts.Flush(); err != nil {
Expand Down
10 changes: 6 additions & 4 deletions vpn_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func loadProfileFile() []vpnProfile {
os.Exit(1)
}
var profiles []vpnProfile
json.Unmarshal(file, &profiles)
err := json.Unmarshal(file, &profiles)
if err != nil {
log.Fatal("Could not load vpn profiles")
}
return profiles
}

Expand Down Expand Up @@ -127,14 +130,13 @@ func addProfile(profileName string) {
password := detailCapture("PASSWORD:")
psk := detailCapture("PSK:")
if confirm() {
var updated = []vpnProfile{}
updated = append(vpnProfiles,
vpnProfiles = append(vpnProfiles,
vpnProfile{Name:profileName,
UserName:username,
PassWord:password,
Psk:psk,
})
writeProfileFile(updated)
writeProfileFile(vpnProfiles)
}
}

0 comments on commit 177f35e

Please sign in to comment.