Skip to content
Permalink
Browse files

initial commit (already working)

  • Loading branch information...
marcobeierer committed Dec 28, 2015
0 parents commit 6050b625e3c88fadcda71ce5f53d9e183d583d1a
Showing with 101 additions and 0 deletions.
  1. +7 −0 README.md
  2. +94 −0 main.go
@@ -0,0 +1,7 @@
# Sitemap Generator CLI
A command line interface for my sitemap generator written in Go (golang).

## Installation
go get -u github.com/webguerilla/sitemapgenerator-cli
cd $GOPATH/github.com/webguerilla/sitemapgenerator-cli
go install
94 main.go
@@ -0,0 +1,94 @@
package main

import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)

func main() {
log.SetFlags(0)

tokenPath := flag.String("tokenpath", "", "path to the token file")
flag.Parse()

token, ok := readToken(*tokenPath)
if !ok {
log.Println("could not read token from file")
return
}

url := flag.Arg(0)
if url == "" {
log.Printf("usage: %s [flags] url\n", os.Args[0])
return
}

for {
if body, contentType, ok := doRequest(url, token); ok {
if contentType == "application/xml" {
fmt.Println(body)
return
}
} else {
log.Println("request failed")
return
}
time.Sleep(1 * time.Second)
}
}

func readToken(tokenPath string) (string, bool) {
if tokenPath == "" {
return "", true
}

bytes, err := ioutil.ReadFile(tokenPath)
if err != nil {
log.Println(err)
return "", false
}

return fmt.Sprintf("%s", bytes), true
}

func doRequest(url, token string) (string, string, bool) {
urlBase64 := base64.URLEncoding.EncodeToString([]byte(url))

req, err := http.NewRequest("GET", "https://api.marcobeierer.com/sitemap/v2/"+urlBase64, nil)
if err != nil {
log.Println(err)
return "", "", false
}

if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return "", "", false
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Printf("got status code %d, expected 200\n", resp.StatusCode)
return "", "", false
}

bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return "", "", false
}

contentType := resp.Header.Get("content-type")

return string(bytes), contentType, true
}

0 comments on commit 6050b62

Please sign in to comment.
You can’t perform that action at this time.