Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

81 řádky
2.0 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. // +build ignore
  7. // Command print fetches and prints package.
  8. //
  9. // Usage: go run print.go importPath
  10. package main
  11. import (
  12. "flag"
  13. "fmt"
  14. "log"
  15. "net/http"
  16. "strings"
  17. "github.com/golang/gddo/gosrc"
  18. )
  19. var (
  20. etag = flag.String("etag", "", "Etag")
  21. local = flag.String("local", "", "Get package from local workspace.")
  22. present = flag.Bool("present", false, "Get presentation.")
  23. )
  24. func main() {
  25. flag.Parse()
  26. if len(flag.Args()) != 1 {
  27. log.Fatal("Usage: go run print.go importPath")
  28. }
  29. if *present {
  30. printPresentation(flag.Args()[0])
  31. } else {
  32. printDir(flag.Args()[0])
  33. }
  34. }
  35. func printDir(path string) {
  36. if *local != "" {
  37. gosrc.SetLocalDevMode(*local)
  38. }
  39. dir, err := gosrc.Get(http.DefaultClient, path, *etag)
  40. if e, ok := err.(gosrc.NotFoundError); ok && e.Redirect != "" {
  41. log.Fatalf("redirect to %s", e.Redirect)
  42. } else if err != nil {
  43. log.Fatalf("%+v", err)
  44. }
  45. fmt.Println("ImportPath ", dir.ImportPath)
  46. fmt.Println("ResovledPath ", dir.ResolvedPath)
  47. fmt.Println("ProjectRoot ", dir.ProjectRoot)
  48. fmt.Println("ProjectName ", dir.ProjectName)
  49. fmt.Println("ProjectURL ", dir.ProjectURL)
  50. fmt.Println("VCS ", dir.VCS)
  51. fmt.Println("Etag ", dir.Etag)
  52. fmt.Println("BrowseURL ", dir.BrowseURL)
  53. fmt.Println("Subdirectories", strings.Join(dir.Subdirectories, ", "))
  54. fmt.Println("LineFmt ", dir.LineFmt)
  55. fmt.Println("Files:")
  56. for _, file := range dir.Files {
  57. fmt.Printf("%30s %5d %s\n", file.Name, len(file.Data), file.BrowseURL)
  58. }
  59. }
  60. func printPresentation(path string) {
  61. pres, err := gosrc.GetPresentation(http.DefaultClient, path)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. fmt.Printf("%s\n", pres.Files[pres.Filename])
  66. for name, data := range pres.Files {
  67. if name != pres.Filename {
  68. fmt.Printf("---------- %s ----------\n%s\n", name, data)
  69. }
  70. }
  71. }