-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wifi config formatter. Add hooks to API
- Loading branch information
1 parent
e762d93
commit 7a22678
Showing
6 changed files
with
120 additions
and
9 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
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,40 @@ | ||
package network | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
) | ||
|
||
var wifiSupplicant = []byte(`country=US | ||
network={ | ||
ssid="{{.SSID}}" | ||
psk="{{.Passphrase}}" | ||
key_mgmt=WPA-PSK | ||
} | ||
`) | ||
|
||
// WifiInfo describes the information needed to configure the wifi client | ||
type WifiInfo struct { | ||
SSID string | ||
Passphrase string | ||
} | ||
|
||
// FormatWifiCredentials formats the given credentials as a new wifi supplicant config file | ||
func FormatWifiCredentials(ssid, password string) (string, error) { | ||
|
||
credentials := WifiInfo{ssid, password} | ||
tmpl, err := template.New("wifisupp").Parse(string(wifiSupplicant)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var tpl bytes.Buffer | ||
err = tmpl.Execute(&tpl, credentials) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return tpl.String(), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package network_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/danesparza/appliance-monitor/network" | ||
) | ||
|
||
// The formatting utility should format the wifi supplicant file | ||
func TestFormatWifiCredentials_WithValidParams_ShouldReturnProperFormat(t *testing.T) { | ||
|
||
// Arrange | ||
ssid := "testssid" | ||
passphrase := "testpassphrase" | ||
expectedconfig := `country=US | ||
network={ | ||
ssid="testssid" | ||
psk="testpassphrase" | ||
key_mgmt=WPA-PSK | ||
} | ||
` | ||
|
||
// Act | ||
retval, err := network.FormatWifiCredentials(ssid, passphrase) | ||
|
||
// Assert | ||
if err != nil { | ||
t.Errorf("An error occured while formatting the wifi config: %v", err) | ||
} | ||
|
||
if retval != expectedconfig { | ||
t.Errorf("Configuration doesn't match what we expect. Here's what we got:\n%v", retval) | ||
} | ||
} |
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