-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generation of MAC addresses when not specified in the configurati…
…on file for a TAP net type. Move pidFile() and uuidFile() to a new file internal/config/file.go along with a new hwaddrFile() function
- Loading branch information
Showing
4 changed files
with
118 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
func pidFile(path string) (int, error) { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return 0, fmt.Errorf("pid file not found") | ||
} | ||
pidTxt, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return 0, fmt.Errorf("pid file cannot be read") | ||
} | ||
|
||
pid, err := strconv.Atoi(string(pidTxt)) | ||
if err != nil { | ||
return 0, fmt.Errorf("pid file does not contain an integer") | ||
} | ||
|
||
return pid, nil | ||
} | ||
|
||
func uuidFile(path string) (uuid.UUID, error) { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return [16]byte{}, fmt.Errorf("uuid file not found") | ||
} | ||
uuidTxt, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return [16]byte{}, fmt.Errorf("uuid file cannot be read") | ||
} | ||
|
||
uuid, err := uuid.Parse(string(uuidTxt)) | ||
if err != nil { | ||
return [16]byte{}, fmt.Errorf("uuid file does not contain an UUID") | ||
} | ||
|
||
return uuid, nil | ||
} | ||
|
||
func hwaddrFile(path string) (net.HardwareAddr, error) { | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("mac addr file not found") | ||
} | ||
hwaddrTxt, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("mac addr file cannot be read") | ||
} | ||
|
||
hwaddr, err := net.ParseMAC(string(hwaddrTxt)) | ||
if err != nil { | ||
return nil, fmt.Errorf("mac addr file does not contain a hardware address") | ||
} | ||
|
||
return hwaddr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters