Cut instead of split when separating pihole url and password

This commit is contained in:
Fredrik Berntsson
2024-08-10 22:58:53 +02:00
parent 7bdae93550
commit a7e2f7a030
5 changed files with 19 additions and 12 deletions
+3 -2
View File
@@ -7,14 +7,15 @@ import (
)
func TestInit_info(t *testing.T) {
t.Setenv("NS_DEBUG", "false")
Init()
assert.Equal(t, zerolog.InfoLevel, zerolog.GlobalLevel())
}
func TestInit_debug(t *testing.T) {
t.Setenv("NS_DEBUG", "true")
Init()
assert.Equal(t, zerolog.DebugLevel, zerolog.GlobalLevel())
}
+6 -5
View File
@@ -25,20 +25,21 @@ func NewPiHole(host, password string) PiHole {
}
func (piHole *PiHole) Decode(value string) error {
split := strings.Split(value, "|")
if len(split) != 2 {
uri, password, found := strings.Cut(value, "|")
if !found {
return fmt.Errorf("invalid pihole format")
}
res, err := url.Parse(split[0])
parsedUrl, err := url.Parse(uri)
if err != nil {
return fmt.Errorf("failed to parse url: %s", err)
}
*piHole = PiHole{
Url: res,
Password: split[1],
Url: parsedUrl,
Password: password,
}
return nil
}
+6 -3
View File
@@ -1,6 +1,7 @@
package model
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/url"
"testing"
@@ -8,14 +9,16 @@ import (
func TestPiHole_Decode(t *testing.T) {
ph := PiHole{}
const uri = "http://localhost:1337"
const pw = "asdfa|sdf"
err := ph.Decode("http://localhost:1337|asdfasdf")
err := ph.Decode(fmt.Sprintf("%s|%s", uri, pw))
assert.NoError(t, err)
expectedUrl, err := url.Parse("http://localhost:1337")
expectedUrl, err := url.Parse(uri)
assert.NoError(t, err)
assert.Equal(t, expectedUrl, ph.Url)
assert.Equal(t, "asdfasdf", ph.Password)
assert.Equal(t, pw, ph.Password)
}
+3 -1
View File
@@ -1,6 +1,8 @@
package main
import "github.com/lovelaze/nebula-sync/cmd"
import (
"github.com/lovelaze/nebula-sync/cmd"
)
func main() {
cmd.Execute()
+1 -1
View File
@@ -1,3 +1,3 @@
package version
const Version = "0.1.0"
const Version = "0.1.1"