func debugMenuItems() []trayhost.MenuItem { return []trayhost.MenuItem{ { Title: "Debug: Get Clipboard Content", Handler: func() { cc, err := trayhost.GetClipboardContent() log.Printf("GetClipboardContent() error: %v\n", err) log.Printf("Text: %q\n", cc.Text) log.Printf("Image: %v len(%v)\n", cc.Image.Kind, len(cc.Image.Bytes)) log.Printf("Files: len(%v) %v\n", len(cc.Files), cc.Files) }, }, { Title: "Debug: Set Clipboard Text", Handler: func() { trayhost.SetClipboardText("http://www.example.com/image.png") }, }, { Title: "Debug: Notification", Handler: func() { handler := func() { open.Open("http://www.example.com/image.png") } notification := trayhost.Notification{Title: "Success", Body: "http://www.example.com/image.png", Timeout: 3 * time.Second, Handler: handler} //trayhost.Notification{Title: "Upload Failed", Body: "error description goes here"}.Display() if cc, err := trayhost.GetClipboardContent(); err == nil && cc.Image.Kind != "" { notification.Image = cc.Image } notification.Display() }, }, } }
// DisplayHTMLInBrowser displays given html page in a new browser window/tab. func DisplayHTMLInBrowser(mux *http.ServeMux, stopServerChan <-chan struct{}, query string) { // TODO: Aquire a free port similarly to using ioutil.TempFile() for files. open.Open("http://localhost:7044/index" + query) err := httpstoppable.ListenAndServe("localhost:7044", mux, stopServerChan) if err != nil { panic(err) } }
func instantShareHandler() { log.Println("request URL") resp, err := httpClient.Get(*hostFlag + "/api/getfilename?ext=" + clipboard.extension) if err != nil { trayhost.Notification{Title: "Upload Failed", Body: err.Error()}.Display() log.Println(err) return } defer resp.Body.Close() filename, err := ioutil.ReadAll(resp.Body) if err != nil { trayhost.Notification{Title: "Upload Failed", Body: err.Error()}.Display() log.Println(err) return } log.Println("display/put URL in clipboard") url := *hostFlag + "/" + string(filename) trayhost.SetClipboardText(url) trayhost.Notification{ Title: "Success", Body: url, Image: notificationThumbnail, Timeout: 3 * time.Second, Handler: func() { // On click, open the displayed URL. open.Open(url) }, }.Display() log.Println("upload image in background of size", len(clipboard.bytes)) go func(b []byte) { req, err := http.NewRequest("PUT", url, bytes.NewReader(b)) if err != nil { log.Println(err) return } req.Header.Set("Content-Type", "application/octet-stream") resp, err := http.DefaultClient.Do(req) if err != nil { log.Println(err) return } _ = resp.Body.Close() log.Println("done") }(clipboard.bytes) }
// DisplayMarkdownInBrowser displays given Markdown in a new browser window/tab. func DisplayMarkdownInBrowser(markdown []byte) { stopServerChan := make(chan struct{}) handler := func(w http.ResponseWriter, req *http.Request) { gfmutil.WriteGitHubFlavoredMarkdownViaLocal(w, markdown) stopServerChan <- struct{}{} } http.HandleFunc("/index", handler) http.Handle("/favicon.ico", http.NotFoundHandler()) // TODO: Aquire a free port similarly to using ioutil.TempFile() for files. // TODO: Consider using httptest.NewServer. open.Open("http://localhost:7044/index") err := httpstoppable.ListenAndServe("localhost:7044", nil, stopServerChan) if err != nil { panic(err) } }
func main() { flag.Usage = usage flag.Parse() // If we can have access to a cache directory on this system, use it for // caching HTTP requests of presenters. cacheDir, err := ospath.CacheDir("github.com/shurcooL/Go-Package-Store") if err != nil { log.Println("skipping persistent on-disk caching, because unable to acquire a cache dir:", err) cacheDir = "" } // Set GitHub presenter client. { var transport http.RoundTripper // Optionally, perform GitHub API authentication with provided token. if token := os.Getenv("GO_PACKAGE_STORE_GITHUB_TOKEN"); token != "" { transport = &oauth2.Transport{ Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}), } } if cacheDir != "" { transport = &httpcache.Transport{ Transport: transport, Cache: diskcache.New(filepath.Join(cacheDir, "github-presenter")), MarkCachedResponses: true, } } github.SetClient(&http.Client{Transport: transport}) } // Set Gitiles presenter client. { var transport http.RoundTripper if cacheDir != "" { transport = &httpcache.Transport{ Transport: transport, Cache: diskcache.New(filepath.Join(cacheDir, "gitiles-presenter")), MarkCachedResponses: true, } } gitiles.SetClient(&http.Client{Transport: transport}) } switch { case !production: fmt.Println("Using no real packages (hit /mock.html endpoint for mocks).") pipeline.Done() updater = repo.MockUpdater{} default: fmt.Println("Using all Go packages in GOPATH.") go func() { // This needs to happen in the background because sending input will be blocked on processing. forEachRepository(func(r Repo) { pipeline.AddRepository(r) }) pipeline.Done() }() updater = repo.GopathUpdater{} case *stdinFlag: fmt.Println("Reading the list of newline separated Go packages from stdin.") go func() { // This needs to happen in the background because sending input will be blocked on processing. br := bufio.NewReader(os.Stdin) for line, err := br.ReadString('\n'); err == nil; line, err = br.ReadString('\n') { importPath := line[:len(line)-1] // Trim last newline. pipeline.Add(importPath) } pipeline.Done() }() updater = repo.GopathUpdater{} case *godepsFlag != "": fmt.Println("Reading the list of Go packages from Godeps.json file:", *godepsFlag) g, err := readGodeps(*godepsFlag) if err != nil { log.Fatalln("Failed to read Godeps.json file", err) } go func() { // This needs to happen in the background because sending input will be blocked on processing. for _, dependency := range g.Deps { pipeline.AddRevision(dependency.ImportPath, dependency.Rev) } pipeline.Done() }() updater = nil case *govendorFlag != "": fmt.Println("Reading the list of Go packages from vendor.json file:", *govendorFlag) v, err := readGovendor(*govendorFlag) if err != nil { log.Fatalln("Failed to read vendor.json file:", err) } go func() { // This needs to happen in the background because sending input will be blocked on processing. for _, dependency := range v.Package { pipeline.AddRevision(dependency.Path, dependency.Revision) } pipeline.Done() }() if gu, err := repo.NewGovendorUpdater(""); err == nil { updater = gu } else { log.Println("govendor updater is not available:", err) } } err = loadTemplates() if err != nil { log.Fatalln("loadTemplates:", err) } http.HandleFunc("/index.html", mainHandler) http.Handle("/favicon.ico", http.NotFoundHandler()) fileServer := httpgzip.FileServer(assets, httpgzip.FileServerOptions{ServeError: httpgzip.Detailed}) http.Handle("/assets/", fileServer) http.Handle("/assets/octicons/", http.StripPrefix("/assets", fileServer)) http.Handle("/opened", websocket.Handler(openedHandler)) // Exit server when client tab is closed. if updater != nil { http.HandleFunc("/-/update", updateHandler) go updateWorker() } // Start listening first. listener, err := net.Listen("tcp", *httpFlag) if err != nil { log.Fatalf("failed to listen on %q: %v\n", *httpFlag, err) } if production { // Open a browser tab and navigate to the main page. go open.Open("http://" + *httpFlag + "/index.html") } fmt.Println("Go Package Store server is running at http://" + *httpFlag + "/index.html.") err = http.Serve(listener, nil) if err != nil { log.Fatalln(err) } }