-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
path.go
100 lines (88 loc) · 3.08 KB
/
path.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2019 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package tlfhandle
import (
"fmt"
"strings"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/protocol/keybase1"
)
// PathType describes the types for different paths
type PathType string
const (
// KeybasePathType is the keybase root (like /keybase)
KeybasePathType PathType = "keybase"
// PublicPathType is the keybase public folder list (like /keybase/public)
PublicPathType PathType = "public"
// PrivatePathType is the keybase private folder list (like
// /keybase/private)
PrivatePathType PathType = "private"
// SingleTeamPathType is the keybase team folder list (like /keybase/teams)
SingleTeamPathType PathType = "team"
)
// BuildCanonicalPath returns a canonical path for a path components.
// This a canonical path and may need to be converted to a platform
// specific path, for example, on Windows, this might correspond to
// k:\private\username. Note that "canonical" here indicates it's in the form
// of /keybase/<tlfType>/blah... . It does not try to canonicalize TLF names.
func BuildCanonicalPath(pathType PathType, paths ...string) string {
var prefix string
switch pathType {
case KeybasePathType:
prefix = "/" + string(KeybasePathType)
default:
prefix = "/" + string(KeybasePathType) + "/" + string(pathType)
}
pathElements := []string{prefix}
for _, p := range paths {
if p != "" {
pathElements = append(pathElements, p)
}
}
return strings.Join(pathElements, "/")
}
// BuildCanonicalPathForTlfType is like BuildCanonicalPath, but accepts a
// tlf.Type instead of PathType.
func BuildCanonicalPathForTlfType(t tlf.Type, paths ...string) string {
var pathType PathType
switch t {
case tlf.Private:
pathType = PrivatePathType
case tlf.Public:
pathType = PublicPathType
case tlf.SingleTeam:
pathType = SingleTeamPathType
default:
panic(fmt.Sprintf("Unknown tlf path type: %d", t))
}
return BuildCanonicalPath(pathType, paths...)
}
// BuildCanonicalPathForTlfName returns a canonical path for a tlf.
func BuildCanonicalPathForTlfName(t tlf.Type, tlfName tlf.CanonicalName) string {
return BuildCanonicalPathForTlfType(t, string(tlfName))
}
// BuildCanonicalPathForTlf returns a canonical path for a tlf. Although tlf
// identifies a TLF, paths should still include the TLF name. This function
// does not try to canonicalize TLF names.
func BuildCanonicalPathForTlf(tlf tlf.ID, paths ...string) string {
return BuildCanonicalPathForTlfType(tlf.Type(), paths...)
}
// BuildProtocolPathForTlfName builds a `keybase1.Path` for the given
// TLF type and name.
func BuildProtocolPathForTlfName(
t tlf.Type, tlfName tlf.CanonicalName) keybase1.Path {
var pathType PathType
switch t {
case tlf.Private:
pathType = PrivatePathType
case tlf.Public:
pathType = PublicPathType
case tlf.SingleTeam:
pathType = SingleTeamPathType
default:
panic(fmt.Sprintf("Unknown tlf path type: %d", t))
}
return keybase1.NewPathWithKbfs(
keybase1.KBFSPath{Path: "/" + string(pathType) + "/" + string(tlfName)})
}