github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/tlfhandle/path.go (about)

     1  // Copyright 2019 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package tlfhandle
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/keybase/client/go/kbfs/tlf"
    12  	"github.com/keybase/client/go/protocol/keybase1"
    13  )
    14  
    15  // PathType describes the types for different paths
    16  type PathType string
    17  
    18  const (
    19  	// KeybasePathType is the keybase root (like /keybase)
    20  	KeybasePathType PathType = "keybase"
    21  	// PublicPathType is the keybase public folder list (like /keybase/public)
    22  	PublicPathType PathType = "public"
    23  	// PrivatePathType is the keybase private folder list (like
    24  	// /keybase/private)
    25  	PrivatePathType PathType = "private"
    26  	// SingleTeamPathType is the keybase team folder list (like /keybase/teams)
    27  	SingleTeamPathType PathType = "team"
    28  )
    29  
    30  // BuildCanonicalPath returns a canonical path for a path components.
    31  // This a canonical path and may need to be converted to a platform
    32  // specific path, for example, on Windows, this might correspond to
    33  // k:\private\username. Note that "canonical" here indicates it's in the form
    34  // of /keybase/<tlfType>/blah... . It does not try to canonicalize TLF names.
    35  func BuildCanonicalPath(pathType PathType, paths ...string) string {
    36  	var prefix string
    37  	switch pathType {
    38  	case KeybasePathType:
    39  		prefix = "/" + string(KeybasePathType)
    40  	default:
    41  		prefix = "/" + string(KeybasePathType) + "/" + string(pathType)
    42  	}
    43  	pathElements := []string{prefix}
    44  	for _, p := range paths {
    45  		if p != "" {
    46  			pathElements = append(pathElements, p)
    47  		}
    48  	}
    49  	return strings.Join(pathElements, "/")
    50  }
    51  
    52  // BuildCanonicalPathForTlfType is like BuildCanonicalPath, but accepts a
    53  // tlf.Type instead of PathType.
    54  func BuildCanonicalPathForTlfType(t tlf.Type, paths ...string) string {
    55  	var pathType PathType
    56  	switch t {
    57  	case tlf.Private:
    58  		pathType = PrivatePathType
    59  	case tlf.Public:
    60  		pathType = PublicPathType
    61  	case tlf.SingleTeam:
    62  		pathType = SingleTeamPathType
    63  	default:
    64  		panic(fmt.Sprintf("Unknown tlf path type: %d", t))
    65  	}
    66  
    67  	return BuildCanonicalPath(pathType, paths...)
    68  }
    69  
    70  // BuildCanonicalPathForTlfName returns a canonical path for a tlf.
    71  func BuildCanonicalPathForTlfName(t tlf.Type, tlfName tlf.CanonicalName) string {
    72  	return BuildCanonicalPathForTlfType(t, string(tlfName))
    73  }
    74  
    75  // BuildCanonicalPathForTlf returns a canonical path for a tlf. Although tlf
    76  // identifies a TLF, paths should still include the TLF name. This function
    77  // does not try to canonicalize TLF names.
    78  func BuildCanonicalPathForTlf(tlf tlf.ID, paths ...string) string {
    79  	return BuildCanonicalPathForTlfType(tlf.Type(), paths...)
    80  }
    81  
    82  // BuildProtocolPathForTlfName builds a `keybase1.Path` for the given
    83  // TLF type and name.
    84  func BuildProtocolPathForTlfName(
    85  	t tlf.Type, tlfName tlf.CanonicalName) keybase1.Path {
    86  	var pathType PathType
    87  	switch t {
    88  	case tlf.Private:
    89  		pathType = PrivatePathType
    90  	case tlf.Public:
    91  		pathType = PublicPathType
    92  	case tlf.SingleTeam:
    93  		pathType = SingleTeamPathType
    94  	default:
    95  		panic(fmt.Sprintf("Unknown tlf path type: %d", t))
    96  	}
    97  
    98  	return keybase1.NewPathWithKbfs(
    99  		keybase1.KBFSPath{Path: "/" + string(pathType) + "/" + string(tlfName)})
   100  }