github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/path.go (about) 1 // Copyright 2015 The Vanadium 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 jiri 6 7 import ( 8 "path/filepath" 9 "strings" 10 11 "v.io/x/lib/envvar" 12 ) 13 14 // RelPath represents a relative path whose root is JIRI_ROOT. 15 type RelPath string 16 17 // NewRelPath returns a RelPath with path consisting of the specified 18 // components. 19 func NewRelPath(components ...string) RelPath { 20 return RelPath(filepath.Join(components...)) 21 } 22 23 // Abs returns an absolute path corresponding to the RelPath rooted at the 24 // JIRI_ROOT in X. 25 func (rp RelPath) Abs(x *X) string { 26 return filepath.Join(x.Root, string(rp)) 27 } 28 29 // Join returns a copy of RelPath with the specified components appended 30 // to the path using filepath.Join. 31 func (rp RelPath) Join(components ...string) RelPath { 32 path := append([]string{string(rp)}, components...) 33 return RelPath(filepath.Join(path...)) 34 } 35 36 // Symbolic returns an absolute path corresponding to the RelPath, but 37 // with the JIRI_ROOT environment varible at the root instead of the actual 38 // value of JIRI_ROOT. 39 func (rp RelPath) Symbolic() string { 40 root := "${" + RootEnv + "}" 41 if string(rp) == "" { 42 return root 43 } 44 return root + string(filepath.Separator) + string(rp) 45 } 46 47 // ExpandEnv expands all instances of the JIRI_ROOT variable in the supplied 48 // environment with the root from jirix. 49 func ExpandEnv(x *X, env *envvar.Vars) { 50 e := env.ToMap() 51 rootEnv := "${" + RootEnv + "}" 52 for k, v := range e { 53 n := strings.Replace(v, rootEnv, x.Root, -1) 54 if n != v { 55 env.Set(k, n) 56 } 57 } 58 }