~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/filepath/filepath.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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