aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2015-09-30 17:33:34 +1000
committerGravatar alecdwm 2015-09-30 17:33:34 +1000
commitff040436d737cf91cee73010f4e8fd946700b704 (patch)
tree0847531059f21282726ffa9446b188f618f1a846
parent538e4204e984bd4d426879aa03305e28bc7b996b (diff)
Multiple Improvements
+ handleError function + 'Profile Page' changes + Commandline flag parsing
-rw-r--r--gogetme.go73
1 files changed, 48 insertions, 25 deletions
diff --git a/gogetme.go b/gogetme.go
index 85666f6..9922727 100644
--- a/gogetme.go
+++ b/gogetme.go
@@ -19,6 +19,7 @@ package main
import (
"bufio"
"encoding/json"
+ "flag"
"fmt"
"net/http"
"os"
@@ -66,18 +67,12 @@ type GitHubUserData struct {
func getGitHubUserData(username string) *GitHubUserData {
// Fetch
response, err := http.Get(fmt.Sprintf("https://api.github.com/users/%s", username))
- if err != nil {
- fmt.Println(err)
- panic("Error fetching GitHub user data")
- }
+ handleError("Fetching GitHub user data", err)
// Decode
gitHubUserData := new(GitHubUserData)
err = json.NewDecoder(response.Body).Decode(&gitHubUserData)
- if err != nil {
- fmt.Println(err)
- panic("Error parsing response as JSON")
- }
+ handleError("Parsing response as JSON", err)
// Return
return gitHubUserData
@@ -88,6 +83,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("[%s] Handled connection from: %s\n", t.Format(time.RFC850), r.RemoteAddr)
// Todo: a minimal profile page with github user data, maybe use layout from people at http://themes.getbootstrap.com/
+ // Todo: detect whether project exists on GitHub or not, and forward through to GitHub user page if not
fmt.Fprintf(w,
"<!DOCTYPE html>\n"+
"<html lang=\"en\">\n"+
@@ -102,13 +98,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
"</head>\n"+
"<body>\n"+
"<div class=\"container\">\n"+
- "<div class=\"panel panel-default panel-profile m-b-0\">\n"+
- "<div class=\"panel-heading\" style=\"background-image: url(%s);\"></div>\n"+
+ "<div class=\"panel panel-default\">\n"+
+ "<img class=\"pull-left panel-profile-img img-circle\" style=\"width:120px;height:120px;\" src=\"%s\" />\n"+
"<div class=\"panel-body text-center\">\n"+
- "<img class=\"panel-profile-img\" src=\"%s\" />\n"+
- "<h5 class=\"panel-title\">%s</h5>\n"+
- "<p class=\"m-b\">%s/%s</p>\n"+
- "<a href=\"https://github.com/%s/%s\" class=\"btn btn-primary-outline btn-sm m-b\">%s</a>\n"+
+ "<h1 class=\"text-right panel-title\">%s</h5>\n"+
+ "<p class=\"text-right\">%s/%s</p>\n"+
+ "<a href=\"https://github.com/%s/%s\" class=\"pull-right btn btn-primary-outline btn-sm\">%s</a>\n"+
"</div>\n"+
"</div>\n"+
"</div>\n"+
@@ -117,7 +112,6 @@ func handler(w http.ResponseWriter, r *http.Request) {
"</html>",
customUrl+"/"+r.URL.Path[1:], gitHubUser, r.URL.Path[1:],
customUrl, gitHubUser, r.URL.Path[1:],
- "https://pbs.twimg.com/profile_banners/13334762/1415719104/1500x500",
gitHubUserAvatarURL,
gitHubUser,
customUrl, r.URL.Path[1:],
@@ -126,24 +120,46 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() {
// Get ready to GO!
+ flag.StringVar(&customUrl, "customUrl", "unset", "URL to host your Go packages at")
+ flag.StringVar(&gitHubUser, "gitHubUser", "unset", "your GitHub username")
+ listenPort := ":9999"
+ flag.StringVar(&listenPort, "listenPort", "unset", "port to listen for connections on")
+ flag.Parse()
+
reader := bufio.NewReader(os.Stdin)
+ if customUrl == "unset" {
+ fmt.Print("Input Custom Go Import URL > ")
+
+ input, err := reader.ReadString('\n')
+ handleError("Reading input string", err)
+
+ customUrl = strings.Trim(input, "\n")
+ }
+ fmt.Printf("\nCustom Go Import URL set to '%s'\n", customUrl)
- fmt.Print("Input Custom Go Import URL > ")
- customUrl, _ = reader.ReadString('\n')
- customUrl = strings.Trim(customUrl, "\n")
- fmt.Printf("\nCustom Go Import URL set to '%s'\n\n", customUrl)
+ if gitHubUser == "unset" {
+ fmt.Print("\nInput GitHub Username > ")
- fmt.Print("Input GitHub Username > ")
- gitHubUser, _ = reader.ReadString('\n')
- gitHubUser = strings.Trim(gitHubUser, "\n")
+ input, err := reader.ReadString('\n')
+ handleError("Reading input string", err)
+
+ gitHubUser = strings.Trim(input, "\n")
+ }
fmt.Printf("\nGitHub Username set to '%s'\n", gitHubUser)
gitHubUserAvatarURL = getGitHubUserData(gitHubUser).AvatarUrl
fmt.Printf("\nGitHub Avatar URL set to '%s'\n", gitHubUserAvatarURL)
- fmt.Print("\nInput port to listen on > ")
- listenPort, _ := reader.ReadString('\n')
- listenPort = ":" + strings.Trim(listenPort, "\n")
+ if listenPort == "unset" {
+ fmt.Print("\nInput port to listen on > ")
+
+ input, err := reader.ReadString('\n')
+ handleError("Reading input string", err)
+
+ listenPort = ":" + strings.Trim(input, "\n")
+ } else {
+ listenPort = ":" + listenPort
+ }
fmt.Printf("\nListen port set to '%s'\n", listenPort)
// Listen and SERVE!
@@ -151,3 +167,10 @@ func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(listenPort, nil)
}
+
+func handleError(str string, err error) {
+ if err != nil {
+ fmt.Println(err)
+ panic("Error: " + str)
+ }
+}