1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package filepath
import (
"runtime"
"strings"
"github.com/juju/errors"
"github.com/juju/utils"
)
// Renderer provides methods for the different functions in
// the stdlib path/filepath package that don't relate to a concrete
// filesystem. So Abs, EvalSymlinks, Glob, Rel, and Walk are not
// included. Also, while the functions in path/filepath relate to the
// current host, the PathRenderer methods relate to the renderer's
// target platform. So for example, a windows-oriented implementation
// will give windows-specific results even when used on linux.
type Renderer interface {
// Base mimics path/filepath.
Base(path string) string
// Clean mimics path/filepath.
Clean(path string) string
// Dir mimics path/filepath.
Dir(path string) string
// Ext mimics path/filepath.
Ext(path string) string
// FromSlash mimics path/filepath.
FromSlash(path string) string
// IsAbs mimics path/filepath.
IsAbs(path string) bool
// Join mimics path/filepath.
Join(path ...string) string
// Match mimics path/filepath.
Match(pattern, name string) (matched bool, err error)
// NormCase normalizes the case of a pathname. On Unix and Mac OS X,
// this returns the path unchanged; on case-insensitive filesystems,
// it converts the path to lowercase.
NormCase(path string) string
// Split mimics path/filepath.
Split(path string) (dir, file string)
// SplitList mimics path/filepath.
SplitList(path string) []string
// SplitSuffix splits the pathname into a pair (root, suffix) such
// that root + suffix == path, and ext is empty or begins with a
// period and contains at most one period. Leading periods on the
// basename are ignored; SplitSuffix('.cshrc') returns ('.cshrc', '').
SplitSuffix(path string) (string, string)
// ToSlash mimics path/filepath.
ToSlash(path string) string
// VolumeName mimics path/filepath.
VolumeName(path string) string
}
// NewRenderer returns a Renderer for the given os.
func NewRenderer(os string) (Renderer, error) {
if os == "" {
os = runtime.GOOS
}
os = strings.ToLower(os)
switch {
case os == utils.OSWindows:
return &WindowsRenderer{}, nil
case utils.OSIsUnix(os):
return &UnixRenderer{}, nil
case os == "ubuntu":
return &UnixRenderer{}, nil
default:
return nil, errors.NotFoundf("renderer for %q", os)
}
}
|