Copilot commented on code in PR #996: URL: https://github.com/apache/skywalking-banyandb/pull/996#discussion_r2906380431
########## pkg/path/path_test.go: ########## @@ -0,0 +1,82 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package path + +import ( + "os" + "path/filepath" + "testing" +) + +func TestGet(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + t.Fatalf("failed to get working directory: %v", err) + } + + tests := []struct { + name string + input string + want string + wantErr bool + }{ + { + name: "empty string", + input: "", + want: "", + wantErr: false, + }, + { + name: "absolute path", + input: "/tmp/test", + want: filepath.Clean("/tmp/test"), + wantErr: false, + }, Review Comment: This test hard-codes a Unix-style absolute path (`/tmp/test`), which can behave differently or fail on Windows (volume-less rooted paths, path semantics). Consider generating an OS-appropriate absolute path (e.g., based on `t.TempDir()` and `filepath.Join`) so the test is cross-platform. ########## pkg/path/path.go: ########## @@ -0,0 +1,34 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Package path provides utilities to resolve relative paths to absolute paths. +package path + +import ( + "path/filepath" +) + +// Get resolves the given path to an absolute path. +// If the path is empty, it returns an empty string. +// If the path is already absolute, it returns the path as is. +// Otherwise, it returns the absolute path relative to the current working directory. +func Get(p string) (string, error) { + if p == "" { + return "", nil + } + return filepath.Abs(p) +} Review Comment: `filepath.Abs` will `Clean` the path (even when already absolute), so the comment 'returns the path as is' is not strictly accurate. Update the doc to reflect that it returns a cleaned absolute path (or implement the exact behavior described). ########## banyand/trace/svc_standalone.go: ########## @@ -136,6 +137,15 @@ func (s *standalone) PreRun(ctx context.Context) error { s.l = logger.GetLogger(s.Name()) s.l.Info().Msg("memory protector is initialized in PreRun") s.lfs = fs.NewLocalFileSystemWithLoggerAndLimit(s.l, s.pm.GetLimit()) + var err error + if s.root, err = banyandbpath.Get(s.root); err != nil { + return err + } + if s.dataPath != "" { + if s.dataPath, err = banyandbpath.Get(s.dataPath); err != nil { + return err + } Review Comment: `banyandbpath.Get` already treats an empty string as a no-op (`\"\" → \"\"` with nil error), so the `if s.dataPath != \"\"` guard is redundant. You can simplify and standardize these call sites to always call `Get` for optional paths, which reduces boilerplate across services. ```suggestion if s.dataPath, err = banyandbpath.Get(s.dataPath); err != nil { return err ``` ########## banyand/metadata/client.go: ########## @@ -263,6 +264,40 @@ func (s *clientService) PreRun(ctx context.Context) error { } }() + var err error + if s.etcdTLSCAFile != "" { + if s.etcdTLSCAFile, err = banyandbpath.Get(s.etcdTLSCAFile); err != nil { + return err + } + } + if s.etcdTLSCertFile != "" { + if s.etcdTLSCertFile, err = banyandbpath.Get(s.etcdTLSCertFile); err != nil { + return err + } + } + if s.etcdTLSKeyFile != "" { + if s.etcdTLSKeyFile, err = banyandbpath.Get(s.etcdTLSKeyFile); err != nil { + return err + } + } + if s.propertySchemaClientCACert != "" { + if s.propertySchemaClientCACert, err = banyandbpath.Get(s.propertySchemaClientCACert); err != nil { + return err + } + } + if s.filePath != "" { + if s.filePath, err = banyandbpath.Get(s.filePath); err != nil { + return err Review Comment: Returning the raw error here loses field-level context (which path failed to resolve). Wrap the error with a message including the specific config field (e.g., `etcdTLSCAFile`) so operators can diagnose misconfiguration more quickly. ```suggestion return errors.Wrapf(err, "failed to resolve path for etcdTLSCAFile %q", s.etcdTLSCAFile) } } if s.etcdTLSCertFile != "" { if s.etcdTLSCertFile, err = banyandbpath.Get(s.etcdTLSCertFile); err != nil { return errors.Wrapf(err, "failed to resolve path for etcdTLSCertFile %q", s.etcdTLSCertFile) } } if s.etcdTLSKeyFile != "" { if s.etcdTLSKeyFile, err = banyandbpath.Get(s.etcdTLSKeyFile); err != nil { return errors.Wrapf(err, "failed to resolve path for etcdTLSKeyFile %q", s.etcdTLSKeyFile) } } if s.propertySchemaClientCACert != "" { if s.propertySchemaClientCACert, err = banyandbpath.Get(s.propertySchemaClientCACert); err != nil { return errors.Wrapf(err, "failed to resolve path for propertySchemaClientCACert %q", s.propertySchemaClientCACert) } } if s.filePath != "" { if s.filePath, err = banyandbpath.Get(s.filePath); err != nil { return errors.Wrapf(err, "failed to resolve path for filePath %q", s.filePath) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
