Skip to content

Commit

Permalink
fs(perm): set permission to 0755 (drwxr-xr-x) when create new directo…
Browse files Browse the repository at this point in the history
…ry (#161)

* dir(perm): change permission to 0755 (drwxr-xr-x) when create a new directory

* refactor: define const DefaultDirPerm=0755 and DefaultFilePerm=0644

---------

Co-authored-by: wenchyzhu <wenchyzhu@tencent.com>
  • Loading branch information
wenchy and wenchyzhu authored Oct 30, 2024
1 parent e5ff8b9 commit 3a3f5cb
Show file tree
Hide file tree
Showing 30 changed files with 87 additions and 78 deletions.
8 changes: 4 additions & 4 deletions internal/confgen/confgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"time"

"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/options"
"github.com/tableauio/tableau/proto/tableaupb"
Expand Down Expand Up @@ -100,7 +100,7 @@ func (gen *Generator) GenWorkbook(bookSpecifiers ...string) error {
if err != nil {
return xerrors.Wrapf(err, "parse book specifier failed: %s", specifier)
}
relCleanSlashPath := fs.CleanSlashPath(bookName)
relCleanSlashPath := xfs.CleanSlashPath(bookName)
log.Debugf("convert relWorkbookPath to relCleanSlashPath: %s -> %s", bookName, relCleanSlashPath)
primaryBookIndexInfo, ok := bookIndexes[relCleanSlashPath]
if !ok {
Expand Down Expand Up @@ -137,12 +137,12 @@ func (gen *Generator) convert(prFiles *protoregistry.Files, fd protoreflect.File
}

// filter subdir
if !fs.HasSubdirPrefix(workbook.Name, gen.InputOpt.Subdirs) {
if !xfs.HasSubdirPrefix(workbook.Name, gen.InputOpt.Subdirs) {
return nil
}

// rewrite subdir
rewrittenWorkbookName := fs.RewriteSubdir(workbook.Name, gen.InputOpt.SubdirRewrites)
rewrittenWorkbookName := xfs.RewriteSubdir(workbook.Name, gen.InputOpt.SubdirRewrites)
absWbPath := filepath.Join(gen.InputDir, rewrittenWorkbookName)
log.Debugf("proto: %s, workbook options: %s", fd.Path(), workbook)

Expand Down
3 changes: 2 additions & 1 deletion internal/confgen/confgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"testing"

"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/options"
)

func prepareOutput() error {
// prepare output common dir
outdir := "./testdata/_conf"
err := os.MkdirAll(outdir, 0700)
err := os.MkdirAll(outdir, xfs.DefaultDirPerm)
if err != nil {
return fmt.Errorf("failed to create output dir: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/confgen/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/internal/confgen/prop"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/strcase"
"github.com/tableauio/tableau/internal/types"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/options"
"github.com/tableauio/tableau/proto/tableaupb"
Expand Down Expand Up @@ -57,7 +57,7 @@ func (x *sheetExporter) ScatterAndExport(info *SheetInfo,
filename = fmt.Sprintf("%s_%s", impInfo.BookName(), sheetName)
}
if info.Opts.WithParentDir {
parentDirName := fs.GetDirectParentDirName(impInfo.Filename())
parentDirName := xfs.GetDirectParentDirName(impInfo.Filename())
return filepath.Join(parentDirName, filename)
}
return filename
Expand Down Expand Up @@ -113,7 +113,7 @@ func (x *sheetExporter) MergeAndExport(info *SheetInfo,
// here filename has no ext suffix
filename := string(info.MD.Name())
if info.Opts.WithParentDir {
parentDirName := fs.GetDirectParentDirName(impInfo.Filename())
parentDirName := xfs.GetDirectParentDirName(impInfo.Filename())
return filepath.Join(parentDirName, filename)
}
return filename
Expand Down
4 changes: 2 additions & 2 deletions internal/confgen/prop/refer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"sync"

"github.com/emirpasic/gods/sets/hashset"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/proto/tableaupb"
"github.com/tableauio/tableau/xerrors"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -152,7 +152,7 @@ func loadValueSpace(refer string, input *Input) (*ValueSpace, error) {
sheetName := sheetOpts.Name

// rewrite subdir
rewrittenWorkbookName := fs.RewriteSubdir(bookName, input.SubdirRewrites)
rewrittenWorkbookName := xfs.RewriteSubdir(bookName, input.SubdirRewrites)
absWbPath := filepath.Join(input.InputDir, rewrittenWorkbookName)
primaryImporter, err := importer.New(absWbPath, importer.Sheets([]string{sheetName}))
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions internal/confgen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"sync"

"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer"
"github.com/tableauio/tableau/internal/strcase"
"github.com/tableauio/tableau/internal/types"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/options"
Expand Down Expand Up @@ -119,7 +119,7 @@ func parseBookSpecifier(bookSpecifier string) (bookName string, sheetName string
fmt := format.GetFormat(bookSpecifier)
if fmt == format.CSV {
// special process for CSV filename pattern: "<BookName>#<SheetName>.csv"
bookName, err := fs.ParseCSVBooknamePatternFrom(bookSpecifier)
bookName, err := xfs.ParseCSVBooknamePatternFrom(bookSpecifier)
if err != nil {
return "", "", err
}
Expand All @@ -129,9 +129,9 @@ func parseBookSpecifier(bookSpecifier string) (bookName string, sheetName string
baseBookSpecifier := filepath.Base(bookSpecifier)
tokens := strings.SplitN(baseBookSpecifier, "#", 2)
if len(tokens) == 2 {
return fs.Join(dir, tokens[0]), tokens[1], nil
return xfs.Join(dir, tokens[0]), tokens[1], nil
}
return fs.Join(dir, tokens[0]), "", nil
return xfs.Join(dir, tokens[0]), "", nil
}

type bookIndexInfo struct {
Expand All @@ -149,11 +149,11 @@ func buildWorkbookIndex(protoPackage, inputDir string, subdirs []string, subdirR
return true
}
// filter subdir
if !fs.HasSubdirPrefix(workbook.Name, subdirs) {
if !xfs.HasSubdirPrefix(workbook.Name, subdirs) {
return true
}
// add self: rewrite subdir
rewrittenWorkbookName := fs.RewriteSubdir(workbook.Name, subdirRewrites)
rewrittenWorkbookName := xfs.RewriteSubdir(workbook.Name, subdirRewrites)
if bookIndexes[rewrittenWorkbookName] == nil {
bookIndexes[rewrittenWorkbookName] = &bookIndexInfo{
books: make(map[string]protoreflect.FileDescriptor),
Expand Down Expand Up @@ -213,7 +213,7 @@ func getRealSheetName(info *SheetInfo, impInfo importer.ImporterInfo) string {
}

func getRelBookName(basepath, filename string) string {
if relBookName, err := fs.Rel(basepath, filename); err != nil {
if relBookName, err := xfs.Rel(basepath, filename); err != nil {
log.Warnf("get relative path failed: %+v", err)
return filename
} else {
Expand Down
Empty file.
10 changes: 5 additions & 5 deletions internal/importer/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"path/filepath"

"github.com/emirpasic/gods/sets/treeset"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/proto/tableaupb"
"github.com/tableauio/tableau/xerrors"
Expand Down Expand Up @@ -141,11 +141,11 @@ func readCSVRows(filename string, topN uint) (rows [][]string, err error) {
}

func parseCSVBookReaderOptions(filename string, sheetNames []string) (*bookReaderOptions, error) {
bookName, _, err := fs.ParseCSVFilenamePattern(filename)
bookName, _, err := xfs.ParseCSVFilenamePattern(filename)
if err != nil {
return nil, xerrors.Errorf("cannot parse the book name from filename: %s", filename)
}
globFilename := fs.GenCSVBooknamePattern(filepath.Dir(filename), bookName)
globFilename := xfs.GenCSVBooknamePattern(filepath.Dir(filename), bookName)
matches, err := filepath.Glob(globFilename)
if err != nil {
return nil, xerrors.Wrapf(err, "failed to glob %s", globFilename)
Expand All @@ -157,7 +157,7 @@ func parseCSVBookReaderOptions(filename string, sheetNames []string) (*bookReade
// NOTE: keep the order of sheets
set := treeset.NewWithStringComparator()
for _, filename := range matches {
set.Add(fs.CleanSlashPath(filename))
set.Add(xfs.CleanSlashPath(filename))
}

brOpts := &bookReaderOptions{
Expand All @@ -166,7 +166,7 @@ func parseCSVBookReaderOptions(filename string, sheetNames []string) (*bookReade
}
for _, val := range set.Values() {
filename := val.(string)
_, sheetName, err := fs.ParseCSVFilenamePattern(filename)
_, sheetName, err := xfs.ParseCSVFilenamePattern(filename)
if err != nil {
return nil, xerrors.Errorf("cannot parse the book name from filename: %s", filename)
}
Expand Down
16 changes: 8 additions & 8 deletions internal/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"

"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/proto/tableaupb/internalpb"
"github.com/tableauio/tableau/xerrors"
Expand Down Expand Up @@ -73,7 +73,7 @@ func GetScatterImporters(inputDir, primaryBookName, sheetName string, scatterSpe
for relBookPath := range relBookPaths {
log.Infof("%18s: %s#%s", "scatter sheet", relBookPath, specifiedSheetName)
fpath := filepath.Join(inputDir, relBookPath)
rewrittenWorkbookName := fs.RewriteSubdir(primaryBookName, subdirRewrites)
rewrittenWorkbookName := xfs.RewriteSubdir(primaryBookName, subdirRewrites)
primaryBookPath := filepath.Join(inputDir, rewrittenWorkbookName)
importer, err := New(fpath, Sheets([]string{specifiedSheetName}), Cloned(primaryBookPath))
if err != nil {
Expand Down Expand Up @@ -102,7 +102,7 @@ func GetMergerImporters(inputDir, primaryBookName, sheetName string, sheetSpecif
for relBookPath := range relBookPaths {
log.Infof("%18s: %s#%s", "merge sheet", relBookPath, specifiedSheetName)
fpath := filepath.Join(inputDir, relBookPath)
rewrittenWorkbookName := fs.RewriteSubdir(primaryBookName, subdirRewrites)
rewrittenWorkbookName := xfs.RewriteSubdir(primaryBookName, subdirRewrites)
primaryBookPath := filepath.Join(inputDir, rewrittenWorkbookName)
importer, err := New(fpath, Sheets([]string{specifiedSheetName}), Cloned(primaryBookPath))
if err != nil {
Expand All @@ -124,12 +124,12 @@ func ResolveSheetSpecifier(inputDir, primaryBookName string, sheetSpecifier stri
bookNameGlob, specifiedSheetName := ParseSheetSpecifier(sheetSpecifier)

// rewrite subdir
rewrittenWorkbookName := fs.RewriteSubdir(primaryBookName, subdirRewrites)
rewrittenWorkbookName := xfs.RewriteSubdir(primaryBookName, subdirRewrites)

primaryBookPath := filepath.Join(inputDir, rewrittenWorkbookName)
log.Debugf("rewrittenAbsWorkbookName: %s", primaryBookPath)
fmt := format.GetFormat(primaryBookPath)
pattern := fs.Join(filepath.Dir(primaryBookPath), bookNameGlob)
pattern := xfs.Join(filepath.Dir(primaryBookPath), bookNameGlob)
matches, err := filepath.Glob(pattern)
if err != nil {
return nil, "", xerrors.Wrapf(err, "failed to glob pattern: %s", pattern)
Expand All @@ -142,16 +142,16 @@ func ResolveSheetSpecifier(inputDir, primaryBookName string, sheetSpecifier stri
path := match
if fmt == format.CSV {
// special process for CSV filename pattern: "<BookName>#<SheetName>.csv"
path, err = fs.ParseCSVBooknamePatternFrom(match)
path, err = xfs.ParseCSVBooknamePatternFrom(match)
if err != nil {
return nil, "", err
}
}
if specifiedSheetName == "" && fs.IsSamePath(path, primaryBookPath) {
if specifiedSheetName == "" && xfs.IsSamePath(path, primaryBookPath) {
// sheet name not specified, so exclude self
continue
}
secondaryBookName, err := fs.Rel(inputDir, path)
secondaryBookName, err := xfs.Rel(inputDir, path)
if err != nil {
return nil, "", err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/importer/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

"github.com/stretchr/testify/assert"
"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/xfs"
)

func init() {
err := fs.RangeFilesByFormat("./testdata", format.CSV, func(bookPath string) error {
err := xfs.RangeFilesByFormat("./testdata", format.CSV, func(bookPath string) error {
// log.Printf("path: %s", bookPath)
imp, err := NewCSVImporter(bookPath, nil, nil, 0, false)
if err != nil {
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestGetMergerImporters(t *testing.T) {
}
filenames := []string{}
for _, imp := range got {
filenames = append(filenames, fs.CleanSlashPath(imp.Filename()))
filenames = append(filenames, xfs.CleanSlashPath(imp.Filename()))
}
assert.ElementsMatch(t, tt.want, filenames, "got book filenames not match")
})
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestGetScatterImporters(t *testing.T) {
}
filenames := []string{}
for _, imp := range got {
filenames = append(filenames, fs.CleanSlashPath(imp.Filename()))
filenames = append(filenames, xfs.CleanSlashPath(imp.Filename()))
}
assert.ElementsMatch(t, tt.want, filenames, "got book filenames not match")
})
Expand Down
4 changes: 2 additions & 2 deletions internal/protogen/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

"github.com/emirpasic/gods/sets/treeset"
"github.com/rogpeppe/go-internal/lockedfile"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/printer"
"github.com/tableauio/tableau/internal/strcase"
"github.com/tableauio/tableau/internal/types"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/proto/tableaupb"
Expand Down Expand Up @@ -106,7 +106,7 @@ func (x *bookExporter) export(checkProtoFileConflicts bool) error {
// refer: https://go.googlesource.com/proposal/+/master/design/33974-add-public-lockedfile-pkg.md

if checkProtoFileConflicts {
if existed, err := fs.Exists(path); err != nil {
if existed, err := xfs.Exists(path); err != nil {
return xerrors.WrapKV(err)
} else {
if existed {
Expand Down
4 changes: 2 additions & 2 deletions internal/protogen/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"path/filepath"
"strings"

"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/protogen/parseroptions"
"github.com/tableauio/tableau/internal/strcase"
"github.com/tableauio/tableau/internal/types"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/proto/tableaupb"
Expand Down Expand Up @@ -42,7 +42,7 @@ func newBookParser(bookName, alias, relSlashPath string, gen *Generator) *bookPa
filename := strcase.ToSnake(protoBookName)
if gen.OutputOpt.FilenameWithSubdirPrefix {
bookPath := filepath.Join(filepath.Dir(relSlashPath), protoBookName)
snakePath := strcase.ToSnake(fs.CleanSlashPath(bookPath))
snakePath := strcase.ToSnake(xfs.CleanSlashPath(bookPath))
filename = strings.ReplaceAll(snakePath, "/", "__")
}
bp := &bookParser{
Expand Down
8 changes: 4 additions & 4 deletions internal/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/internal/confgen"
"github.com/tableauio/tableau/internal/excel"
"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/protogen/parseroptions"
"github.com/tableauio/tableau/internal/strcase"
"github.com/tableauio/tableau/internal/types"
"github.com/tableauio/tableau/internal/xfs"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/options"
Expand Down Expand Up @@ -212,7 +212,7 @@ func (gen *Generator) generate(dir string) (err error) {
}

if fmt == format.CSV {
bookName, _, err := fs.ParseCSVFilenamePattern(entry.Name())
bookName, _, err := xfs.ParseCSVFilenamePattern(entry.Name())
if err != nil {
return err
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (gen *Generator) convertDocument(dir, filename string, checkProtoFileConfli
}
debugBookName := relativePath
// rewrite subdir
rewrittenBookName := fs.RewriteSubdir(relativePath, gen.InputOpt.SubdirRewrites)
rewrittenBookName := xfs.RewriteSubdir(relativePath, gen.InputOpt.SubdirRewrites)
if rewrittenBookName != relativePath {
debugBookName += " (rewrite: " + rewrittenBookName + ")"
}
Expand Down Expand Up @@ -373,7 +373,7 @@ func (gen *Generator) convertTable(dir, filename string, checkProtoFileConflicts
}
debugBookName := relativePath
// rewrite subdir
rewrittenBookName := fs.RewriteSubdir(relativePath, gen.InputOpt.SubdirRewrites)
rewrittenBookName := xfs.RewriteSubdir(relativePath, gen.InputOpt.SubdirRewrites)
if rewrittenBookName != relativePath {
debugBookName += " (rewrite: " + rewrittenBookName + ")"
}
Expand Down
Loading

0 comments on commit 3a3f5cb

Please sign in to comment.