Skip to content

Commit

Permalink
v1.1 config file validity check added
Browse files Browse the repository at this point in the history
  • Loading branch information
esperlu committed Jun 30, 2020
1 parent c8bfc8c commit 09b5f14
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions gomount.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Package gomount mounts remote servers on local mount points defined in `fstab`
// Mount points are read from a user config file. Run `$ gomount -h` for more details.
//
package main

import (
Expand Down Expand Up @@ -59,7 +62,7 @@ func main() {
}
defer f.Close()

// validate config file. Iff err, terminate main()
// validate config file. If err, terminate main()
err = validateConf(f)
if err != nil {
return
Expand Down Expand Up @@ -127,7 +130,7 @@ func main() {

// Functions

// goping
// goping http ping to check if a server is up
func goping(protocole string, host string, port string, t time.Duration) error {
t = time.Duration(t * time.Millisecond)
_, err := net.DialTimeout(protocole, host+":"+port, t)
Expand All @@ -141,34 +144,6 @@ func checkErr(err error) {
}
}

// readConfig reads the config file. Returns list of servers to mount and mount points.
func readConfig(f *os.File) []server {
var host []server

// reset file seek head at bebinning of file (f *os.File pointer may be used by other func)
defer f.Seek(0, 0)

// Scan through config file and process the mounts
scanner := bufio.NewScanner(f)
for scanner.Scan() {
confLine := strings.TrimSpace(scanner.Text())
fields := strings.Split(confLine, ",")
// Skip commented lines
if strings.HasPrefix(confLine, "#") || confLine == "" {
continue
}

// Store config file lines into []struc
host = append(host, server{
Name: fields[0],
Mnt: fields[1],
Host: fields[2],
Port: fields[3],
})
}
return host
}

// validateConf validates the config file and print errors
func validateConf(f *os.File) error {

Expand Down Expand Up @@ -230,3 +205,31 @@ func validateConf(f *os.File) error {
}
return nil
}

// readConfig reads the config file. Returns list of servers to mount and mount points.
func readConfig(f *os.File) []server {
var host []server

// reset file seek head at bebinning of file (f *os.File pointer may be used by other func)
defer f.Seek(0, 0)

// Scan through config file and process the mounts
scanner := bufio.NewScanner(f)
for scanner.Scan() {
confLine := strings.TrimSpace(scanner.Text())
fields := strings.Split(confLine, ",")
// Skip commented lines
if strings.HasPrefix(confLine, "#") || confLine == "" {
continue
}

// Store config file lines into []struc
host = append(host, server{
Name: fields[0],
Mnt: fields[1],
Host: fields[2],
Port: fields[3],
})
}
return host
}

0 comments on commit 09b5f14

Please sign in to comment.