Skip to content

Commit

Permalink
create: support './' and abs for protofile path (#19)
Browse files Browse the repository at this point in the history
* create: support './' and abs for protofile path

* add verbose

* trim path
  • Loading branch information
WineChord authored Oct 20, 2023
1 parent a5e4c51 commit c43de64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func Test_CreateCmd(t *testing.T) {
opts = append(opts, "--protofile", tt.pbfile)
opts = append(opts, "-o", out)
opts = append(opts, "--check-update", "true")
opts = append(opts, "-v")

if tt.rpconly {
opts = append(opts, "--rpconly")
Expand Down
9 changes: 7 additions & 2 deletions cmd/create/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -165,6 +166,10 @@ func (c *Create) fixProtoDirs() error {

if c.options.UseBaseName {
c.options.Protofile = filepath.Base(target)
} else if filepath.IsAbs(c.options.Protofile) {
c.options.Protofile = strings.TrimPrefix(c.options.Protofile, "/")
} else {
c.options.Protofile = strings.TrimPrefix(c.options.Protofile, "./")
}

c.options.ProtofileAbs = target
Expand Down Expand Up @@ -408,8 +413,8 @@ func (c *Create) parsePBIDLOptions(flags *pflag.FlagSet) error {
if err != nil {
return fmt.Errorf("flags get protodir string array failed err: %w", err)
}
// Always append the current working directory.
c.options.Protodirs = fs.UniqFilePath(append(dirs, "."))
// Always append the current working directory and root directory.
c.options.Protodirs = fs.UniqFilePath(append(dirs, ".", "/"))
c.options.Protofile, err = flags.GetString("protofile")
if err != nil {
return fmt.Errorf("flags get protofile string failed err: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions util/fs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func LocateFile(protofile string, protodirs []string) (string, error) {

// If we can find the protofile under the current directory,
// directly return to prevent possible conflicts resulting from the relative path in protofile.
// Reference: https://mk.woa.com/q/286784
fp := filepath.Join(abs, protofile)
if info, err := os.Lstat(fp); err == nil && !info.IsDir() {
return fp, nil
Expand All @@ -61,7 +60,7 @@ func LocateFile(protofile string, protodirs []string) (string, error) {
log.Debug("protodirs: %s", protodirs)
fpaths, err := getPbFilePathList(protofile, protodirs)
if err != nil {
return "", fmt.Errorf("get pb file path listerr: %w", err)
return "", fmt.Errorf("get pb file path list err: %w", err)
}

// `-protofile=abc/d.proto`, works like `-protodir=abc -protofile=d.proto`
Expand Down

0 comments on commit c43de64

Please sign in to comment.