本文整理汇总了Golang中path/filepath.ToSlash函数的典型用法代码示例。如果您正苦于以下问题:Golang ToSlash函数的具体用法?Golang ToSlash怎么用?Golang ToSlash使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToSlash函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ParseIdl
// ParseIdl parses the idl in the given file with tests for the given
// language. The Idl object is returned unless an error occured.
func ParseIdl(fileName, lang string) (*idl.Idl, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("Error opening input file: %s", err)
}
defer f.Close()
var lexer IdlLex
lexer.Init(f, f.Name())
lexer.globals.pidl = new(idl.Idl)
lexer.globals.pidl.Init()
lexer.globals.pidl.Filename = filepath.ToSlash(fileName)
lexer.globals.basedir = filepath.ToSlash(filepath.Dir(fileName))
yyParse(&lexer)
if len(lexer.Errors) > 0 {
return nil, fmt.Errorf("%s\n%d parsing errors, exiting", strings.Join(lexer.Errors, "\n"), len(lexer.Errors))
}
err = lexer.globals.pidl.Validate(lang)
if err != nil {
return nil, fmt.Errorf("IDL does not validate: %s", err)
}
return lexer.globals.pidl, nil
}
示例2: main
func main() {
flag.Parse()
CUDAINC := filepath.ToSlash(os.Getenv(CUDA_INC_PATH))
CUDALIB := filepath.ToSlash(os.Getenv(CUDA_LIB_PATH))
config.Include = strings.Split(CUDAINC, VARDELIM)
config.Lib = strings.Split(CUDALIB, VARDELIM)
fmt.Println(*dir)
dirs, _ := ioutil.ReadDir(*dir)
for i, _ := range dirs {
if dirs[i].IsDir() {
dirName := dirs[i].Name()
currDir := *dir + "/" + dirName
fmt.Println(currDir)
tempPath := currDir + "/" + CONFIG_TEMP_FILE_NAME
outPath := currDir + "/" + CONFIG_FILE_NAME
t, error := template.ParseFiles(tempPath)
if error != nil {
fmt.Println("The folder has no template files")
continue
}
file, err := os.OpenFile(outPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
t.Execute(file, config)
file.Close()
}
}
}
示例3: canUse
// canUse reports whether the package in dir is usable from filename,
// respecting the Go "internal" and "vendor" visibility rules.
func canUse(filename, dir string) bool {
// Fast path check, before any allocations. If it doesn't contain vendor
// or internal, it's not tricky:
// Note that this can false-negative on directories like "notinternal",
// but we check it correctly below. This is just a fast path.
if !strings.Contains(dir, "vendor") && !strings.Contains(dir, "internal") {
return true
}
dirSlash := filepath.ToSlash(dir)
if !strings.Contains(dirSlash, "/vendor/") && !strings.Contains(dirSlash, "/internal/") && !strings.HasSuffix(dirSlash, "/internal") {
return true
}
// Vendor or internal directory only visible from children of parent.
// That means the path from the current directory to the target directory
// can contain ../vendor or ../internal but not ../foo/vendor or ../foo/internal
// or bar/vendor or bar/internal.
// After stripping all the leading ../, the only okay place to see vendor or internal
// is at the very beginning of the path.
abs, err := filepath.Abs(filename)
if err != nil {
return false
}
rel, err := filepath.Rel(abs, dir)
if err != nil {
return false
}
relSlash := filepath.ToSlash(rel)
if i := strings.LastIndex(relSlash, "../"); i >= 0 {
relSlash = relSlash[i+len("../"):]
}
return !strings.Contains(relSlash, "/vendor/") && !strings.Contains(relSlash, "/internal/") && !strings.HasSuffix(relSlash, "/internal")
}
示例4: NormalizePathURL
// NormalizePathURL is used to convert rawPath to a fqdn, using basePath as
// a reference in the filesystem, if necessary. basePath is assumed to contain
// either '.' when first used, or the file:// type fqdn of the parent resource.
// e.g. myFavScript.yaml => file://opt/lib/myFavScript.yaml
func NormalizePathURL(basePath, rawPath string) (string, error) {
u, err := url.Parse(rawPath)
if err != nil {
return "", err
}
// if a scheme is defined, it must be a fqdn already
if u.Scheme != "" {
return u.String(), nil
}
// if basePath is a url, then child resources are assumed to be relative to it
bu, err := url.Parse(basePath)
if err != nil {
return "", err
}
var basePathSys, absPathSys string
if bu.Scheme != "" {
basePathSys = filepath.FromSlash(bu.Path)
absPathSys = filepath.Join(basePathSys, rawPath)
bu.Path = filepath.ToSlash(absPathSys)
return bu.String(), nil
}
absPathSys = filepath.Join(basePath, rawPath)
u.Path = filepath.ToSlash(absPathSys)
if err != nil {
return "", err
}
u.Scheme = "file"
return u.String(), nil
}
示例5: canUse
func canUse(filename, dir string) bool {
dirSlash := filepath.ToSlash(dir)
if !strings.Contains(dirSlash, "/vendor/") && !strings.Contains(dirSlash, "/internal/") && !strings.HasSuffix(dirSlash, "/internal") {
return true
}
// Vendor or internal directory only visible from children of parent.
// That means the path from the current directory to the target directory
// can contain ../vendor or ../internal but not ../foo/vendor or ../foo/internal
// or bar/vendor or bar/internal.
// After stripping all the leading ../, the only okay place to see vendor or internal
// is at the very beginning of the path.
abs, err := filepath.Abs(filename)
if err != nil {
return false
}
rel, err := filepath.Rel(abs, dir)
if err != nil {
return false
}
relSlash := filepath.ToSlash(rel)
if i := strings.LastIndex(relSlash, "../"); i >= 0 {
relSlash = relSlash[i+len("../"):]
}
return !strings.Contains(relSlash, "/vendor/") && !strings.Contains(relSlash, "/internal/") && !strings.HasSuffix(relSlash, "/internal")
}
示例6: pathToUri
//Accepts several paths separated by separator and constructs the URLs
//relative to base path
func pathToUri(paths string, separator string, basePath string) (urls []url.URL, err error) {
var urlBase *url.URL
if basePath != "" {
if string(basePath[0]) != "/" {
//for windows path to build a proper url
basePath = "/" + basePath
}
urlBase, err = url.Parse("file:" + basePath)
}
if err != nil {
return nil, err
}
inputs := strings.Split(paths, ",")
for _, input := range inputs {
var urlInput *url.URL
if basePath != "" {
urlInput, err = url.Parse(filepath.ToSlash(input))
if err != nil {
return nil, err
}
urlInput = urlBase.ResolveReference(urlInput)
} else {
//TODO is opaque really apropriate?
urlInput = &url.URL{
Opaque: filepath.ToSlash(input),
}
}
urls = append(urls, *urlInput)
}
//clean
return
}
示例7: TestLoadSketchFromFolder
func TestLoadSketchFromFolder(t *testing.T) {
ctx := &types.Context{
SketchLocation: "sketch_with_subfolders",
}
commands := []types.Command{
&builder.SketchLoader{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}
sketch := ctx.Sketch
require.NotNil(t, sketch)
require.Contains(t, sketch.MainFile.Name, "sketch_with_subfolders.ino")
require.Equal(t, 0, len(sketch.OtherSketchFiles))
require.Equal(t, 4, len(sketch.AdditionalFiles))
require.Contains(t, filepath.ToSlash(sketch.AdditionalFiles[0].Name), "sketch_with_subfolders/src/subfolder/other.cpp")
require.Contains(t, filepath.ToSlash(sketch.AdditionalFiles[1].Name), "sketch_with_subfolders/src/subfolder/other.h")
require.Contains(t, filepath.ToSlash(sketch.AdditionalFiles[2].Name), "sketch_with_subfolders/subfolder/dont_load_me.cpp")
require.Contains(t, filepath.ToSlash(sketch.AdditionalFiles[3].Name), "sketch_with_subfolders/subfolder/other.h")
}
示例8: NextFile
func (f *serialFile) NextFile() (File, error) {
// if a file was opened previously, close it
err := f.Close()
if err != nil {
return nil, err
}
// if there aren't any files left in the root directory, we're done
if len(f.files) == 0 {
return nil, io.EOF
}
stat := f.files[0]
f.files = f.files[1:]
// open the next file
fileName := filepath.ToSlash(filepath.Join(f.name, stat.Name()))
filePath := filepath.ToSlash(filepath.Join(f.path, stat.Name()))
// recursively call the constructor on the next file
// if it's a regular file, we will open it as a ReaderFile
// if it's a directory, files in it will be opened serially
sf, err := NewSerialFile(fileName, filePath, stat)
if err != nil {
return nil, err
}
f.current = &sf
return sf, nil
}
示例9: executeAnsible
func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) error {
playbook := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.PlaybookFile)))
inventory := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.InventoryFile)))
extraArgs := ""
if len(p.config.ExtraArguments) > 0 {
extraArgs = " " + strings.Join(p.config.ExtraArguments, " ")
}
command := fmt.Sprintf("cd %s && %s %s%s -c local -i %s",
p.config.StagingDir, p.config.Command, playbook, extraArgs, inventory)
ui.Message(fmt.Sprintf("Executing Ansible: %s", command))
cmd := &packer.RemoteCmd{
Command: command,
}
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
}
if cmd.ExitStatus != 0 {
if cmd.ExitStatus == 127 {
return fmt.Errorf("%s could not be found. Verify that it is available on the\n"+
"PATH after connecting to the machine.",
p.config.Command)
}
return fmt.Errorf("Non-zero exit status: %d", cmd.ExitStatus)
}
return nil
}
示例10: TestZGlobAbs
func TestZGlobAbs(t *testing.T) {
tmpdir := setup(t)
defer os.RemoveAll(tmpdir)
curdir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(tmpdir)
if err != nil {
t.Fatal(err)
}
defer os.Chdir(curdir)
for _, test := range testZGlobs {
test.pattern = filepath.ToSlash(filepath.Join(tmpdir, test.pattern))
for i, expected := range test.expected {
test.expected[i] = filepath.ToSlash(filepath.Join(tmpdir, expected))
}
got, err := Glob(test.pattern)
if err != nil {
if test.err != err {
t.Error(err)
}
continue
}
if !check(test.expected, got) {
t.Errorf(`zglob failed: pattern %q: expected %v but got %v`, test.pattern, test.expected, got)
}
}
}
示例11: doMirror
// doMirror - Mirror an object to multiple destination. URLs status contains a copy of sURLs and error if any.
func (ms *mirrorSession) doMirror(sURLs URLs) URLs {
isFake := ms.Header.CommandBoolFlags["fake"]
if sURLs.Error != nil { // Errorneous sURLs passed.
return sURLs.WithError(sURLs.Error.Trace())
}
//s For a fake mirror make sure we update respective progress bars
// and accounting readers under relevant conditions.
if isFake {
ms.status.Add(sURLs.SourceContent.Size)
return sURLs.WithError(nil)
}
sourceAlias := sURLs.SourceAlias
sourceURL := sURLs.SourceContent.URL
targetAlias := sURLs.TargetAlias
targetURL := sURLs.TargetContent.URL
length := sURLs.SourceContent.Size
ms.status.SetCaption(sourceURL.String() + ": ")
sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path))
targetPath := filepath.ToSlash(filepath.Join(targetAlias, targetURL.Path))
ms.status.PrintMsg(mirrorMessage{
Source: sourcePath,
Target: targetPath,
Size: length,
TotalCount: sURLs.TotalCount,
TotalSize: sURLs.TotalSize,
})
return uploadSourceToTargetURL(sURLs, ms.status)
}
示例12: setUserDir
func setUserDir() {
user_app_data := filepath.ToSlash(os.Getenv("LOCALAPPDATA"))
if user_app_data == "" {
user_app_data = filepath.Join(filepath.ToSlash(os.Getenv("HOMEDRIVE")+os.Getenv("HOMEPATH")), "AppData", "Local")
}
USER_DIR = filepath.ToSlash(user_app_data)
}
示例13: TestArchive_gitSubdir
func TestArchive_gitSubdir(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}
// Git doesn't allow nested ".git" directories so we do some hackiness
// here to get around that...
testDir := testFixture("archive-git")
oldName := filepath.ToSlash(filepath.Join(testDir, "DOTgit"))
newName := filepath.ToSlash(filepath.Join(testDir, ".git"))
os.Remove(newName)
if err := os.Rename(oldName, newName); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Rename(newName, oldName)
// testDir with VCS set to true
r, err := CreateArchive(filepath.Join(testDir, "subdir"), &ArchiveOpts{VCS: true})
if err != nil {
t.Fatalf("err: %s", err)
}
expected := []string{
"hello.txt",
}
entries := testArchive(t, r, false)
if !reflect.DeepEqual(entries, expected) {
t.Fatalf("bad: %#v", entries)
}
}
示例14: transformRef
func (c *GraphContext) transformRef(rawRef *RawRef) (*graph.Ref, error) {
defUnit, err := c.inferSourceUnit(rawRef, c.Reqs)
if err != nil {
return nil, err
}
defPath := string(rawRef.DefPath)
if defPath == "" {
defPath = "."
}
return &graph.Ref{
DefRepo: defUnit.Repo,
DefUnitType: defUnit.Type,
DefUnit: defUnit.Name,
DefPath: filepath.ToSlash(defPath),
Repo: c.Unit.Repo,
Unit: c.Unit.Name,
UnitType: c.Unit.Type,
File: filepath.ToSlash(rawRef.File),
Start: rawRef.Start,
End: rawRef.End,
Def: rawRef.Def,
}, nil
}
示例15: setGlobalDir
func setGlobalDir() {
programData := filepath.ToSlash(os.Getenv("ALLUSERSPROFILE"))
if programData == "" {
programData = filepath.ToSlash(os.Getenv("ProgramData"))
}
GLOBAL_DIRS = programData
}