about summary refs log tree commit diff
path: root/tools/blog_cli
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-08-19T16·51+0100
committerVincent Ambo <tazjin@google.com>2019-08-19T16·51+0100
commitd5710db90060a23f0069d1f8a910f7bae83f8678 (patch)
tree58479f67c2e175262b2bef49dc0f3e13fcef53b5 /tools/blog_cli
parent2a16740445291366540f8ce3fa8589c3f9b7740a (diff)
refactor(tools/blog_cli): More sensible chunk encoding as records r/42
Instead of many rrdatas in one record, make many records!
Diffstat (limited to 'tools/blog_cli')
-rw-r--r--tools/blog_cli/main.go34
1 files changed, 17 insertions, 17 deletions
diff --git a/tools/blog_cli/main.go b/tools/blog_cli/main.go
index f376f256ea20..9175b2b66f86 100644
--- a/tools/blog_cli/main.go
+++ b/tools/blog_cli/main.go
@@ -50,20 +50,23 @@ type post struct {
 }
 
 func (p *post) writeToDNS() error {
-	metaRecord := dns.ResourceRecordSet{
+	var additions []*dns.ResourceRecordSet
+	additions = append(additions, &dns.ResourceRecordSet{
 		Name: fmt.Sprintf("_meta.%s.blog.tazj.in.", p.ID),
 		Type: "TXT",
 		Ttl:  1200,
 		Rrdatas: []string{
 			encodeJSON(p.Meta),
 		},
-	}
-
-	chunkRecord := dns.ResourceRecordSet{
-		Name:    fmt.Sprintf("_chunks.%s.blog.tazj.in.", p.ID),
-		Type:    "TXT",
-		Ttl:     1200,
-		Rrdatas: p.Chunks,
+	})
+
+	for i, c := range p.Chunks {
+		additions = append(additions, &dns.ResourceRecordSet{
+			Name:    fmt.Sprintf("_%v.%s.blog.tazj.in.", i, p.ID),
+			Type:    "TXT",
+			Ttl:     1200,
+			Rrdatas: []string{c},
+		})
 	}
 
 	ctx := context.Background()
@@ -73,7 +76,7 @@ func (p *post) writeToDNS() error {
 	}
 
 	change := dns.Change{
-		Additions: []*dns.ResourceRecordSet{&metaRecord, &chunkRecord},
+		Additions: additions,
 	}
 
 	_, err = dnsSvc.Changes.Create(*project, *zone, &change).Do()
@@ -93,14 +96,13 @@ func encodeJSON(v interface{}) string {
 // Encode a chunk and check whether it is too large
 func encodeChunk(c chunk) (string, bool) {
 	tooLarge := false
+	s := base64.RawStdEncoding.EncodeToString([]byte(c.Text))
 
-	j := encodeJSON(c)
-
-	if len(j) >= 255 {
+	if len(s) >= 255 {
 		tooLarge = true
 	}
 
-	return j, tooLarge
+	return s, tooLarge
 }
 
 func createPost(id, title, text string, date time.Time) post {
@@ -111,8 +113,6 @@ func createPost(id, title, text string, date time.Time) post {
 	var chunks []string
 
 	for chunkSize < len(runes) {
-		n++
-
 		c, l := encodeChunk(chunk{
 			Chunk: n,
 			Text:  string(runes[0:chunkSize:chunkSize]),
@@ -121,11 +121,10 @@ func createPost(id, title, text string, date time.Time) post {
 		tooLarge = tooLarge || l
 		chunks = append(chunks, c)
 		runes = runes[chunkSize:]
+		n++
 	}
 
 	if len(runes) > 0 {
-		n++
-
 		c, l := encodeChunk(chunk{
 			Chunk: n,
 			Text:  string(runes),
@@ -133,6 +132,7 @@ func createPost(id, title, text string, date time.Time) post {
 
 		tooLarge = tooLarge || l
 		chunks = append(chunks, c)
+		n++
 	}
 
 	if tooLarge {