Skip to content
This repository has been archived by the owner on Jul 16, 2019. It is now read-only.

Commit

Permalink
Add "run" command
Browse files Browse the repository at this point in the history
Signed-off-by: Knut Ahlers <[email protected]>
  • Loading branch information
Luzifer committed Mar 13, 2017
1 parent f6d4ff7 commit da80c66
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gh-private-dl
58 changes: 58 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"net/http"
"strings"

"github.com/Luzifer/gh-private-dl/privatehub"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
sparta "github.com/mweagle/Sparta"
"github.com/spf13/cobra"
)

var executionPort string

func init() {
cmd := &cobra.Command{
Use: "run",
Short: "Run an HTTP server to serve this locally",
RunE: runLocally,
}

cmd.Flags().StringVar(&executionPort, "listen", ":3000", "IP/Port to listen on")

sparta.CommandLineOptions.Root.AddCommand(cmd)
}

func runLocally(cmd *cobra.Command, args []string) error {
r := mux.NewRouter()
r.HandleFunc("/{user}/{repo}/releases/download/{version}/{binary}", handleLocalExecution)

log.Printf("Starting local webserver on %s", executionPort)
return http.ListenAndServe(executionPort, r)
}

func handleLocalExecution(res http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

_, githubToken, ok := r.BasicAuth()
if !ok || githubToken == "" {
http.Error(res, "You need to provide HTTP basic auth", http.StatusBadRequest)
return
}

dlURL, err := privatehub.GetDownloadURL(
strings.Join([]string{vars["user"], vars["repo"]}, "/"),
vars["version"],
vars["binary"],
githubToken,
)

if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}

http.Redirect(res, r, dlURL, http.StatusFound)
}

0 comments on commit da80c66

Please sign in to comment.