1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:54:40 -07:00
go/godoc/vfs/emptyvfs.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

90 lines
2.0 KiB
Go

// Copyright 2016 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"
"os"
"time"
)
// NewNameSpace returns a NameSpace pre-initialized with an empty
// emulated directory mounted on the root mount point "/". This
// allows directory traversal routines to work properly even if
// a folder is not explicitly mounted at root by the user.
func NewNameSpace() NameSpace {
ns := NameSpace{}
ns.Bind("/", &emptyVFS{}, "/", BindReplace)
return ns
}
// type emptyVFS emulates a FileSystem consisting of an empty directory
type emptyVFS struct{}
// Open implements Opener. Since emptyVFS is an empty directory, all
// attempts to open a file should returns errors.
func (e *emptyVFS) Open(path string) (ReadSeekCloser, error) {
if path == "/" {
return nil, fmt.Errorf("open: / is a directory")
}
return nil, os.ErrNotExist
}
// Stat returns os.FileInfo for an empty directory if the path is
// is root "/" or error. os.FileInfo is implemented by emptyVFS
func (e *emptyVFS) Stat(path string) (os.FileInfo, error) {
if path == "/" {
return e, nil
}
return nil, os.ErrNotExist
}
func (e *emptyVFS) Lstat(path string) (os.FileInfo, error) {
return e.Stat(path)
}
// ReadDir returns an empty os.FileInfo slice for "/", else error.
func (e *emptyVFS) ReadDir(path string) ([]os.FileInfo, error) {
if path == "/" {
return []os.FileInfo{}, nil
}
return nil, os.ErrNotExist
}
func (e *emptyVFS) String() string {
return "emptyVFS(/)"
}
func (e *emptyVFS) RootType(path string) RootType {
return RootTypeStandAlone
}
// These functions below implement os.FileInfo for the single
// empty emulated directory.
func (e *emptyVFS) Name() string {
return "/"
}
func (e *emptyVFS) Size() int64 {
return 0
}
func (e *emptyVFS) Mode() os.FileMode {
return os.ModeDir | os.ModePerm
}
func (e *emptyVFS) ModTime() time.Time {
return time.Time{}
}
func (e *emptyVFS) IsDir() bool {
return true
}
func (e *emptyVFS) Sys() interface{} {
return nil
}