예제 #1
0
func TestHTTP_BaseURL(t *testing.T) {
	m := map[string]string{"b/c": "c"}
	mapFS := rwvfs.Map(m)

	prefix := "/foo/bar/baz"

	h := http.Handler(http.StripPrefix(prefix, rwvfs.HTTPHandler(mapFS, nil)))
	httpServer := httptest.NewServer(h)
	defer httpServer.Close()
	httpURL, err := url.Parse(httpServer.URL + prefix)
	if err != nil {
		t.Fatal(err)
	}

	fs := rwvfs.HTTP(httpURL, nil)

	if err := rwvfs.MkdirAll(fs, "b"); err != nil {
		t.Errorf("MkdirAll %q: %s", "b", err)
	}

	fis, err := fs.ReadDir("b")
	if err != nil {
		t.Fatal(err)
	}
	if len(fis) != 1 {
		t.Errorf("got len(fis) == %d, want 1", len(fis))
	}
	if wantName := "c"; fis[0].Name() != wantName {
		t.Errorf("got name == %q, want %q", fis[0].Name(), wantName)
	}
}
예제 #2
0
func TestRWVFS(t *testing.T) {
	tmpdir, err := ioutil.TempDir("", "rwvfs-test-")
	if err != nil {
		t.Fatal("TempDir", err)
	}
	defer os.RemoveAll(tmpdir)

	h := http.Handler(rwvfs.HTTPHandler(rwvfs.Map(map[string]string{}), nil))
	httpServer := httptest.NewServer(h)
	defer httpServer.Close()
	httpURL, err := url.Parse(httpServer.URL)
	if err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		fs rwvfs.FileSystem
	}{
		{rwvfs.OS(tmpdir)},
		{rwvfs.Map(map[string]string{})},
		{rwvfs.Sub(rwvfs.Map(map[string]string{}), "/x")},
		{rwvfs.HTTP(httpURL, nil)},
		{rwvfs.Union(rwvfs.Map(map[string]string{}), rwvfs.Map(map[string]string{}))},
	}
	for _, test := range tests {
		testutil.Write(t, test.fs)
		testutil.Mkdir(t, test.fs)
		testutil.MkdirAll(t, test.fs)
		testutil.Glob(t, test.fs)
	}
}
예제 #3
0
func (s *buildDataService) FileSystem(repo RepoRevSpec) (rwvfs.FileSystem, error) {
	v := repo.RouteVars()
	v["Path"] = "."
	baseURL, err := s.client.URL(router.RepoBuildDataEntry, v, nil)
	if err != nil {
		return nil, err
	}
	return rwvfs.HTTP(s.client.BaseURL.ResolveReference(baseURL), s.client.httpClient), nil
}
예제 #4
0
파일: main.go 프로젝트: alexsaveliev/rwvfs
func main() {
	log.SetFlags(0)
	flag.Parse()

	if flag.NArg() != 2 {
		log.Fatal("error: usage: httpvfs-client [opts] <cat|ls|put|rm> <path>")
	}
	op := flag.Arg(0)
	path := path.Clean(flag.Arg(1))

	url, err := url.Parse(*urlStr)
	if err != nil {
		log.Fatal(err)
	}

	fs := rwvfs.HTTP(url, nil)

	switch strings.ToLower(op) {
	case "cat":
		if *startByte != 0 && *endByte < *startByte {
			log.Fatal("error: -end-byte must be greater than -start-byte")
		}

		var f vfs.ReadSeekCloser
		var err error

		if *startByte != 0 {
			f, err = fs.(rwvfs.FetcherOpener).OpenFetcher(path)
		} else {
			f, err = fs.Open(path)
		}
		if err != nil {
			log.Fatal(err)
		}
		defer func() {
			if err := f.Close(); err != nil {
				log.Fatal(err)
			}
		}()

		rdr := io.Reader(f)

		if *startByte != 0 {
			if err := f.(rwvfs.Fetcher).Fetch(int64(*startByte), int64(*endByte)); err != nil {
				log.Fatalf("Fetch bytes=%d-%d: %s", *startByte, *endByte, err)
			}
		}

		if *startByte != 0 {
			if _, err := f.Seek(int64(*startByte), 0); err != nil {
				log.Fatalln("Seek:", err)
			}
		}
		if *endByte != -1 {
			byteLen := *endByte - *startByte
			rdr = io.LimitReader(f, int64(byteLen))
		}

		if _, err := io.Copy(os.Stdout, rdr); err != nil {
			log.Fatalln("Copy:", err)
		}

	case "ls":
		fis, err := fs.ReadDir(path)
		if err != nil {
			log.Fatal(err)
		}

		var longestNameLen int
		for _, fi := range fis {
			if len(fi.Name()) > longestNameLen {
				longestNameLen = len(fi.Name())
			}
		}
		longestNameLen++ // account for "/" suffix on dirs

		for _, fi := range fis {
			name := fi.Name()
			if fi.IsDir() {
				name += "/"
			}
			mtime := fi.ModTime().Round(time.Second)
			fmt.Printf("%-*s   %s   %d\n", longestNameLen, name, mtime, fi.Size())
		}

	case "put":
		f, err := fs.Create(path)
		if err != nil {
			log.Fatal(err)
		}
		defer func() {
			if err := f.Close(); err != nil {
				log.Fatal(err)
			}
		}()
		log.Println("(reading file data on stdin...)")
		if _, err := io.Copy(f, os.Stdin); err != nil {
			log.Fatal(err)
		}

	case "rm":
		if err := fs.Remove(path); err != nil {
			log.Fatal(err)
		}

	default:
		log.Fatal("error: invalid op (see -h)")
	}
}