Code clean up

This commit is contained in:
NI
2022-04-15 18:42:16 +08:00
parent 0c3c4952fc
commit f884d7cf76
19 changed files with 46 additions and 66 deletions

View File

@@ -108,7 +108,7 @@ func (s Server) IsTLS() bool {
// Verify verifies current configuration
func (s Server) Verify() error {
if net.ParseIP(s.ListenInterface) == nil {
return fmt.Errorf("Invalid IP address \"%s\"", s.ListenInterface)
return fmt.Errorf("invalid IP address \"%s\"", s.ListenInterface)
}
if (len(s.TLSCertificateFile) > 0 && len(s.TLSCertificateKeyFile) <= 0) ||
@@ -132,7 +132,7 @@ func (m Meta) Concretize() (map[string]string, error) {
result, err := v.Parse()
if err != nil {
return nil, fmt.Errorf("Unable to parse Meta \"%s\": %s", k, err)
return nil, fmt.Errorf("unable to parse Meta \"%s\": %s", k, err)
}
mm[k] = result
@@ -175,7 +175,7 @@ type Common struct {
// Verify verifies current setting
func (c Configuration) Verify() error {
if len(c.Servers) <= 0 {
return errors.New("Must specify at least one server")
return errors.New("must specify at least one server")
}
for i, c := range c.Servers {
@@ -185,7 +185,7 @@ func (c Configuration) Verify() error {
continue
}
return fmt.Errorf("Invalid setting for server %d: %s", i, vErr)
return fmt.Errorf("invalid setting for server %d: %s", i, vErr)
}
return nil

View File

@@ -42,16 +42,6 @@ func parseEnv(name string) string {
return os.Getenv(v[21:])
}
func parseEnvDef(name string, def string) string {
v := parseEnv(name)
if len(v) > 0 {
return v
}
return def
}
// Enviro creates an environment variable based configuration loader
func Enviro() Loader {
return func(log log.Logger) (string, Configuration, error) {
@@ -75,7 +65,7 @@ func Enviro() Loader {
if cfgErr != nil {
return enviroTypeName, Configuration{}, fmt.Errorf(
"Failed to build the configuration: %s", cfgErr)
"failed to build the configuration: %s", cfgErr)
}
listenIface := parseEnv("SSHWIFTY_LISTENINTERFACE")
@@ -122,7 +112,7 @@ func Enviro() Loader {
if jErr != nil {
return enviroTypeName, Configuration{}, fmt.Errorf(
"Invalid \"SSHWIFTY_PRESETS\": %s", jErr)
"invalid \"SSHWIFTY_PRESETS\": %s", jErr)
}
}
@@ -130,7 +120,7 @@ func Enviro() Loader {
if err != nil {
return enviroTypeName, Configuration{}, fmt.Errorf(
"Unable to parse Preset data: %s", err)
"unable to parse Preset data: %s", err)
}
return enviroTypeName, Configuration{

View File

@@ -106,7 +106,7 @@ func (f fileCfgPresets) concretize() ([]Preset, error) {
if err != nil {
return nil, fmt.Errorf(
"Unable to concretize Preset %d (titled \"%s\"): %s",
"unable to concretize Preset %d (titled \"%s\"): %s",
i+1, p.Title, err)
}

View File

@@ -47,6 +47,6 @@ func Redundant(loaders ...Loader) Loader {
}
return redundantTypeName, Configuration{}, fmt.Errorf(
"All existing redundant loader has failed")
"all existing redundant loader has failed")
}
}

View File

@@ -51,7 +51,7 @@ func (s String) Parse() (string, error) {
f, e := os.Open(fPath)
if e != nil {
return "", fmt.Errorf("Unable to open %s: %s", fPath, e)
return "", fmt.Errorf("unable to open %s: %s", fPath, e)
}
defer f.Close()
@@ -59,7 +59,7 @@ func (s String) Parse() (string, error) {
fData, e := ioutil.ReadAll(f)
if e != nil {
return "", fmt.Errorf("Unable to read from %s: %s", fPath, e)
return "", fmt.Errorf("unable to read from %s: %s", fPath, e)
}
return string(fData), nil
@@ -74,6 +74,6 @@ func (s String) Parse() (string, error) {
default:
return "", fmt.Errorf(
"Scheme \"%s\" was unsupported", ss[:sSchemeLeadIdx])
"scheme \"%s\" was unsupported", ss[:sSchemeLeadIdx])
}
}

View File

@@ -84,7 +84,7 @@ func TestStringFile(t *testing.T) {
ss = String("file://" + filePath + ".notexist")
result, err = ss.Parse()
_, err = ss.Parse()
if err == nil {
t.Error("Parsing an non-existing file should result an error")