1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:04:43 -07:00
go/godoc/vfs/os.go
Agniva De Sarker 16d1af8d88 godoc: group package home page list by root
This CL groups the package list into two groups - standard library(GOROOT),
and third-party code(GOPATH).

It also wraps the list with a collapsible div header used in the package
documentation page. This makes the entire page easy to view and manage, and also
makes it consistent with the rest of the pages.

To implement this, a new function was added to the filesystem interface
which returns the root type of the filesystem. In most cases, it is either GOROOT
or GOPATH. There are other kinds of filesystems which are not used in the home page,
so additional values have been added to satisfy the interface.

A side effect of this is that the html template code has become a bit spaghetti-like
with if conditions all over. This is because the same template is used to render
a package directory as well as the package home page. A better way is to use
two separate templates for the different tasks. This cleans out a lot of the
if conditions and make for a much cleaner code. This has been taken care in CL 101295.

Fixes golang/go#3305
Fixes golang/go#15020

Change-Id: I876357dc76280a7df2ed08d7c6bc53d9a41e69ab
Reviewed-on: https://go-review.googlesource.com/95835
Run-TryBot: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
2018-04-10 16:41:40 +00:00

100 lines
2.4 KiB
Go

// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vfs
import (
"fmt"
"go/build"
"io/ioutil"
"os"
pathpkg "path"
"path/filepath"
"runtime"
)
// OS returns an implementation of FileSystem reading from the
// tree rooted at root. Recording a root is convenient everywhere
// but necessary on Windows, because the slash-separated path
// passed to Open has no way to specify a drive letter. Using a root
// lets code refer to OS(`c:\`), OS(`d:\`) and so on.
func OS(root string) FileSystem {
var t RootType
switch {
case root == runtime.GOROOT():
t = RootTypeGoRoot
case isGoPath(root):
t = RootTypeGoPath
default:
t = RootTypeStandAlone
}
return osFS{rootPath: root, rootType: t}
}
type osFS struct {
rootPath string
rootType RootType
}
func isGoPath(path string) bool {
for _, bp := range filepath.SplitList(build.Default.GOPATH) {
for _, gp := range filepath.SplitList(path) {
if bp == gp {
return true
}
}
}
return false
}
func (root osFS) String() string { return "os(" + root.rootPath + ")" }
// RootType returns the root type for the filesystem.
//
// Note that we ignore the path argument because roottype is a property of
// this filesystem. But for other filesystems, the roottype might need to be
// dynamically deduced at call time.
func (root osFS) RootType(path string) RootType {
return root.rootType
}
func (root osFS) resolve(path string) string {
// Clean the path so that it cannot possibly begin with ../.
// If it did, the result of filepath.Join would be outside the
// tree rooted at root. We probably won't ever see a path
// with .. in it, but be safe anyway.
path = pathpkg.Clean("/" + path)
return filepath.Join(root.rootPath, path)
}
func (root osFS) Open(path string) (ReadSeekCloser, error) {
f, err := os.Open(root.resolve(path))
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
if fi.IsDir() {
f.Close()
return nil, fmt.Errorf("Open: %s is a directory", path)
}
return f, nil
}
func (root osFS) Lstat(path string) (os.FileInfo, error) {
return os.Lstat(root.resolve(path))
}
func (root osFS) Stat(path string) (os.FileInfo, error) {
return os.Stat(root.resolve(path))
}
func (root osFS) ReadDir(path string) ([]os.FileInfo, error) {
return ioutil.ReadDir(root.resolve(path)) // is sorted
}