gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/complete/env.go (about) 1 // Copyright 2012-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package complete 6 7 import ( 8 "fmt" 9 "os" 10 "strings" 11 ) 12 13 // NewEnvCompleter creates a MultiCompleter consisting of one 14 // or more FileCompleters. It is given an environment variable, 15 // which it splits on :. If there are only zero entries, 16 // it returns an error; else it returns a MultiCompleter. 17 // N.B. it does *not* check for whether a directory exists 18 // or not; directories can come and go. 19 func NewEnvCompleter(s string) (Completer, error) { 20 e := os.Getenv(s) 21 Debug("NewEnvCompleter: path %q has value %q", s, e) 22 if e == "" { 23 return nil, ErrEmptyEnv 24 } 25 dirs := strings.Split(e, ":") 26 if len(dirs) == 0 { 27 return nil, fmt.Errorf("%s is empty", s) 28 } 29 Debug("Build completer for %d dirs: %v", len(dirs), dirs) 30 c := make([]Completer, len(dirs)) 31 for i := range dirs { 32 c[i] = NewFileCompleter(dirs[i]) 33 } 34 return NewMultiCompleter(c[0], c[1:]...), nil 35 } 36 37 // NewPathCompleter calls NewEnvCompleter with "PATH" as the 38 // variable name. It can be used to create completers for shells. 39 func NewPathCompleter() (Completer, error) { 40 // Getenv returns the same value ("") if a path is not found 41 // or if it has the value "". Oh well. 42 return NewEnvCompleter("PATH") 43 }