Code maintenance: Use Unset() option for params

This commit is contained in:
Quentin McGaw
2020-12-29 18:29:21 +00:00
parent a921f9848c
commit ef40f2f91b
4 changed files with 6 additions and 39 deletions

View File

@@ -105,17 +105,3 @@ func (mr *MockOSMockRecorder) Stat(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper() mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stat", reflect.TypeOf((*MockOS)(nil).Stat), arg0) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stat", reflect.TypeOf((*MockOS)(nil).Stat), arg0)
} }
// Unsetenv mocks base method
func (m *MockOS) Unsetenv(arg0 string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Unsetenv", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// Unsetenv indicates an expected call of Unsetenv
func (mr *MockOSMockRecorder) Unsetenv(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Unsetenv", reflect.TypeOf((*MockOS)(nil).Unsetenv), arg0)
}

View File

@@ -9,7 +9,6 @@ type OS interface {
MkdirAll(name string, perm FileMode) error MkdirAll(name string, perm FileMode) error
Remove(name string) error Remove(name string) error
Chown(name string, uid int, gid int) error Chown(name string, uid int, gid int) error
Unsetenv(key string) error
Stat(name string) (nativeos.FileInfo, error) Stat(name string) (nativeos.FileInfo, error)
} }
@@ -31,9 +30,6 @@ func (o *os) Remove(name string) error {
func (o *os) Chown(name string, uid, gid int) error { func (o *os) Chown(name string, uid, gid int) error {
return nativeos.Chown(name, uid, gid) return nativeos.Chown(name, uid, gid)
} }
func (o *os) Unsetenv(key string) error {
return nativeos.Unsetenv(key)
}
func (o *os) Stat(name string) (nativeos.FileInfo, error) { func (o *os) Stat(name string) (nativeos.FileInfo, error) {
return nativeos.Stat(name) return nativeos.Stat(name)
} }

View File

@@ -10,24 +10,15 @@ import (
// GetUser obtains the user to use to connect to the VPN servers. // GetUser obtains the user to use to connect to the VPN servers.
func (r *reader) GetUser() (s string, err error) { func (r *reader) GetUser() (s string, err error) {
defer func() { return r.envParams.GetEnv("USER",
unsetenvErr := r.os.Unsetenv("USER") libparams.CaseSensitiveValue(),
if err == nil { libparams.Compulsory(),
err = unsetenvErr libparams.Unset())
}
}()
return r.envParams.GetEnv("USER", libparams.CaseSensitiveValue(), libparams.Compulsory())
} }
// GetPassword obtains the password to use to connect to the VPN servers. // GetPassword obtains the password to use to connect to the VPN servers.
func (r *reader) GetPassword(required bool) (s string, err error) { func (r *reader) GetPassword(required bool) (s string, err error) {
defer func() { options := []libparams.GetEnvSetter{libparams.CaseSensitiveValue(), libparams.Unset()}
unsetenvErr := r.os.Unsetenv("PASSWORD")
if err == nil {
err = unsetenvErr
}
}()
options := []libparams.GetEnvSetter{libparams.CaseSensitiveValue()}
if required { if required {
options = append(options, libparams.Compulsory()) options = append(options, libparams.Compulsory())
} }

View File

@@ -35,13 +35,7 @@ func (r *reader) GetShadowSocksPort() (port uint16, err error) {
// GetShadowSocksPassword obtains the ShadowSocks server password from the environment variable // GetShadowSocksPassword obtains the ShadowSocks server password from the environment variable
// SHADOWSOCKS_PASSWORD. // SHADOWSOCKS_PASSWORD.
func (r *reader) GetShadowSocksPassword() (password string, err error) { func (r *reader) GetShadowSocksPassword() (password string, err error) {
defer func() { return r.envParams.GetEnv("SHADOWSOCKS_PASSWORD", libparams.CaseSensitiveValue(), libparams.Unset())
unsetErr := r.os.Unsetenv("SHADOWSOCKS_PASSWORD")
if err == nil {
err = unsetErr
}
}()
return r.envParams.GetEnv("SHADOWSOCKS_PASSWORD", libparams.CaseSensitiveValue())
} }
// GetShadowSocksMethod obtains the ShadowSocks method to use from the environment variable // GetShadowSocksMethod obtains the ShadowSocks method to use from the environment variable