Exemple #1
0
func TestGetPageForPlatform(t *testing.T) {
	server := testServer{statusCode: 200, response: "DO IT BRO"}
	var resp entity.Page
	var err error
	server.Intercept(func() {
		resp, err = repository.Page("tldr", "osx")
	})
	defer resp.Close()
	if err != nil {
		t.Error(err)
	}
	if expected := "/osx/tldr.md"; server.originalRequest.URL.Path != expected {
		t.Errorf("Page requested from wrong url: %s", server.originalRequest.URL.Path)
	}
	if body, _ := ioutil.ReadAll(resp.Reader()); string(body) != "DO IT BRO" {
		t.Errorf("Read wrong body: %s")
	}
}
Exemple #2
0
func run() error {
	if len(os.Args) <= 1 {
		return errors.New("Usage: tldr <command>")
	}

	usr, err := user.Current()
	if err != nil {
		return err
	}
	if usr.HomeDir == "" {
		return errors.New("Can't load user's home folder path")
	}

	var repository entity.Repository
	repository = remote.NewRemoteRepository(remoteAddress)
	repository = cache.NewFileSystemCacheRepository(repository, path.Join(usr.HomeDir, ".tldr"), time.Hour*24)
	repository = tldr.NewIndexCheckerRepository(repository)

	cmd := os.Args[1]
	platform := runtime.GOOS
	switch platform {
	case "darwin":
		platform = "osx"
	}

	var page entity.Page
	for _, platform := range []string{platform, "common"} {
		page, err = repository.Page(cmd, platform)
		if err != nil {
			continue
		}
		defer page.Close()
		fmt.Println(tldr.Render(page.Reader()))
		return nil
	}
	return err
}