Skip to content

Commit

Permalink
expand: replace Config.NoGlob with Config.ReadDir
Browse files Browse the repository at this point in the history
One could already disable globbing by using ReadDir==nil instead of
NoGlob==true, so make that the only way to disable globbing. NoGlob had
no advantage over a nil func, and NoGlob==false with ReadDir==nil
confusingly disabled globbing.
  • Loading branch information
mvdan committed Dec 17, 2018
1 parent 9c25adb commit 4b0ffa9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
18 changes: 5 additions & 13 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ type Config struct {
// variables.
Env Environ

// TODO(mvdan): consider replacing NoGlob==true with ReadDir==nil.

// NoGlob corresponds to the shell option that disables globbing.
NoGlob bool
// GlobStar corresponds to the shell option that allows globbing with
// "**".
GlobStar bool

// CmdSubst expands a command substitution node, writing its standard
// output to the provided io.Writer.
//
Expand All @@ -54,6 +46,10 @@ type Config struct {
// Use ioutil.ReadDir to use the filesystem directly.
ReadDir func(string) ([]os.FileInfo, error)

// GlobStar corresponds to the shell option that allows globbing with
// "**".
GlobStar bool

bufferAlloc bytes.Buffer
fieldAlloc [4]fieldPart
fieldsAlloc [4][]fieldPart
Expand Down Expand Up @@ -328,7 +324,7 @@ func Fields(cfg *Config, words ...*syntax.Word) ([]string, error) {
for _, field := range wfields {
path, doGlob := cfg.escapedGlobField(field)
var matches []string
if doGlob && !cfg.NoGlob {
if doGlob && cfg.ReadDir != nil {
matches, err = cfg.glob(dir, path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -688,10 +684,6 @@ func (cfg *Config) glob(base, pattern string) ([]string, error) {
}

func (cfg *Config) globDir(base, dir string, rx *regexp.Regexp, wantDir bool, matches []string) ([]string, error) {
if cfg.ReadDir == nil {
// TODO(mvdan): check this at the beginning of a glob?
return nil, nil
}
fullDir := dir
if !filepath.IsAbs(dir) {
fullDir = filepath.Join(base, dir)
Expand Down
7 changes: 5 additions & 2 deletions interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func (r *Runner) fillExpandConfig(ctx context.Context) {
r2.stmts(ctx, cs.StmtList)
return r2.err
},
ReadDir: ioutil.ReadDir,
}
r.updateExpandOpts()
}
Expand All @@ -107,7 +106,11 @@ func catShortcutArg(stmt *syntax.Stmt) *syntax.Word {
}

func (r *Runner) updateExpandOpts() {
r.ecfg.NoGlob = r.opts[optNoGlob]
if r.opts[optNoGlob] {
r.ecfg.ReadDir = nil
} else {
r.ecfg.ReadDir = ioutil.ReadDir
}
r.ecfg.GlobStar = r.opts[optGlobStar]
}

Expand Down

0 comments on commit 4b0ffa9

Please sign in to comment.