Last active
March 25, 2017 02:47
-
-
Save shivakar/48b28cad5226d0cca1be12497a0134d0 to your computer and use it in GitHub Desktop.
MVP for a command line RFC reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
) | |
func main() { | |
var rfcID string | |
var err error | |
if len(os.Args) > 1 { | |
rfcID = os.Args[1] | |
} | |
if rfcID == "" { | |
fmt.Printf("Enter the RFC ID: ") | |
rfcID, err = bufio.NewReader(os.Stdin).ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
url := fmt.Sprintf("https://tools.ietf.org/rfc/rfc%s.txt", rfcID) | |
url = strings.Replace(url, "\n", "", -1) | |
response, err := http.Get(url) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer response.Body.Close() | |
if response.StatusCode != 200 { | |
log.Fatal(fmt.Sprintf("Status returned: %d", response.StatusCode)) | |
} | |
rfcData, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
rfcPages := strings.Split(string(rfcData), "\f") | |
pageIdx := 0 | |
for pageIdx < len(rfcPages) { | |
fmt.Print(rfcPages[pageIdx]) | |
input, err := bufio.NewReader(os.Stdin).ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
if input[0] == 'q' { | |
break | |
} | |
pageIdx++ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment