-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add GetConfiguration using yaml w/ test #118
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package common | ||
|
||
import ( | ||
"os/user" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func RelativeToAbsolutePath(rpath string) (string, error) { | ||
if rpath == "" { | ||
return rpath, nil | ||
} | ||
|
||
absolutePath := "" | ||
|
||
// case ./ ../ | ||
if strings.Contains(rpath, "./") { | ||
abs, err := filepath.Abs(rpath) | ||
if err != nil { | ||
return rpath, err | ||
} | ||
return abs, nil | ||
} | ||
|
||
// case ~/ | ||
if strings.Contains(rpath, "~") { | ||
i := strings.Index(rpath, "~") // 처음 나온 ~만 반환 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plz comment using english :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strings package has HasPrefix function detail explain is in upper comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops. not upper but lower |
||
|
||
if i > -1 { | ||
pathRemain := rpath[i+1:] | ||
usr, err := user.Current() | ||
if err != nil { | ||
return rpath, err | ||
} | ||
return path.Join(usr.HomeDir, pathRemain), nil | ||
|
||
} else { | ||
return rpath, nil | ||
} | ||
} | ||
|
||
if string(rpath[0]) == "/" { | ||
return rpath, nil | ||
} | ||
|
||
if string(rpath[0]) != "." && string(rpath[0]) != "/" { | ||
currentPath, err := filepath.Abs(".") | ||
if err != nil { | ||
return rpath, err | ||
} | ||
|
||
return path.Join(currentPath, rpath), nil | ||
} | ||
|
||
return absolutePath, nil | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package common_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"os/user" | ||
"path" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/DE-labtory/swim/cmd/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRelativeToAbsolutePath(t *testing.T) { | ||
|
||
testfile1 := "./util.go" | ||
testabsresult1, err := filepath.Abs(testfile1) | ||
assert.NoError(t, err) | ||
testabs1, err := common.RelativeToAbsolutePath(testfile1) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, testabs1, testabsresult1) | ||
|
||
testfile2 := "../README.md" | ||
testabsresult2, err := filepath.Abs(testfile2) | ||
assert.NoError(t, err) | ||
|
||
testabs2, err := common.RelativeToAbsolutePath(testfile2) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, testabs2, testabsresult2) | ||
|
||
// 남의 홈패스에 뭐가있는지 알길이 없으니 하나 만들었다 지움 | ||
usr, err := user.Current() | ||
assert.NoError(t, err) | ||
|
||
testfile3 := usr.HomeDir + "/test.txt" | ||
|
||
_, err = os.Stat(usr.HomeDir) | ||
if os.IsNotExist(err) { | ||
file, err := os.Create(testfile3) | ||
assert.NoError(t, err) | ||
defer file.Close() | ||
} | ||
|
||
err = ioutil.WriteFile(testfile3, []byte("test"), os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
testfile4 := "~/test.txt" | ||
|
||
testabs3, err := common.RelativeToAbsolutePath(testfile4) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testfile3, testabs3) | ||
|
||
err = os.Remove(testfile3) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestRelativeToAbsolutePath_WhenGivenPathIsAbsolute(t *testing.T) { | ||
sshPath := "/iAmRoot" | ||
|
||
absPath, err := common.RelativeToAbsolutePath(sshPath) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, sshPath, absPath) | ||
} | ||
|
||
func TestRelativeToAbsolutePath_WhenGivenPathWithOnlyName(t *testing.T) { | ||
sshPath := "test-dir" | ||
|
||
absPath, err := common.RelativeToAbsolutePath(sshPath) | ||
currentPath, _ := filepath.Abs(".") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, path.Join(currentPath, sshPath), absPath) | ||
} | ||
|
||
func TestRelativeToAbsolutePath_WhenGivenPathIsEmpty(t *testing.T) { | ||
sshPath := "" | ||
|
||
absPath, err := common.RelativeToAbsolutePath(sshPath) | ||
|
||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "", absPath) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package conf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sync" | ||
|
||
"github.com/DE-labtory/swim" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
var confPath = os.Getenv("GOPATH") + "/src/github.com/DE-labtory/swim/conf/config.yaml" | ||
|
||
type Configuration struct { | ||
SWIMConfig swim.Config | ||
SuspicionConfig swim.SuspicionConfig | ||
MessageEndpointConfig swim.MessageEndpointConfig | ||
Member swim.Member | ||
} | ||
|
||
// config is instance of SWIM configuration | ||
var config = &Configuration{} | ||
|
||
var once = sync.Once{} | ||
|
||
func SetConfigPath(abspath string) { | ||
confPath = abspath | ||
} | ||
|
||
func GetConfiguration() *Configuration { | ||
once.Do(func() { | ||
viper.SetConfigFile(confPath) | ||
if err := viper.ReadInConfig(); err != nil { | ||
panic(fmt.Sprintf("cannot read config from %s", confPath)) | ||
} | ||
err := viper.Unmarshal(&config) | ||
if err != nil { | ||
panic("error in read config") | ||
} | ||
}) | ||
|
||
return config | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package conf_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/DE-labtory/swim" | ||
|
||
"github.com/DE-labtory/swim/conf" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetConfiguration(t *testing.T) { | ||
path := os.Getenv("GOPATH") + "/src/github.com/DE-labtory/swim/conf" | ||
confFileName := "/config-test.yaml" | ||
defer os.Remove(path + confFileName) | ||
|
||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
os.Mkdir(path, os.ModePerm) | ||
} | ||
|
||
// please leave this whitespace as space not tab | ||
ConfJson := []byte(` | ||
swimconfig: | ||
maxlocalcount: 2 | ||
maxnsacounter: 8 | ||
t: 3000 | ||
acktimeout: 3000 | ||
k: 2 | ||
bindaddress: 127.0.0.1 | ||
bindport: 50000 | ||
suspicionconfig: | ||
k: 2 | ||
min: 3000 | ||
max: 10000 | ||
messageendpointconfig: | ||
encryptionenabled: false | ||
sendtimeout: 5000 | ||
callbackcollectinterval: 7000 | ||
member: | ||
id: | ||
id: "hello_id" | ||
`) | ||
|
||
err := ioutil.WriteFile(path+confFileName, ConfJson, os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
conf.SetConfigPath(path + confFileName) | ||
config := conf.GetConfiguration() | ||
assert.Equal(t, config.SWIMConfig.MaxlocalCount, 2) | ||
assert.Equal(t, config.SWIMConfig.MaxNsaCounter, 8) | ||
assert.Equal(t, config.SWIMConfig.T, 3000) | ||
assert.Equal(t, config.SWIMConfig.AckTimeOut, 3000) | ||
assert.Equal(t, config.SWIMConfig.K, 2) | ||
assert.Equal(t, config.SWIMConfig.BindAddress, "127.0.0.1") | ||
assert.Equal(t, config.SWIMConfig.BindPort, 50000) | ||
|
||
assert.Equal(t, config.SuspicionConfig.K, 2) | ||
assert.Equal(t, config.SuspicionConfig.Min.Nanoseconds(), int64(3000)) | ||
assert.Equal(t, config.SuspicionConfig.Max.Nanoseconds(), int64(10000)) | ||
|
||
assert.Equal(t, config.MessageEndpointConfig.EncryptionEnabled, false) | ||
assert.Equal(t, config.MessageEndpointConfig.SendTimeout.Nanoseconds(), int64(5000)) | ||
assert.Equal(t, config.MessageEndpointConfig.CallbackCollectInterval.Nanoseconds(), int64(7000)) | ||
|
||
assert.Equal(t, config.Member.ID, swim.MemberID{ID: "hello_id"}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strings package has
HasPrefix
functionyou can use
string.HasPrefix(rpath,"./") | string.HasPrefix(rpath,"../")
it can prevent path like
..../a/b/c
or like/a/b/./c