summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-25 23:57:26 +0530
committerAndinus <andinus@nand.sh>2020-03-25 23:57:26 +0530
commitc55368d4f45cafcc7efd7553fa4cb69360c64d6d (patch)
tree20a7985e136f80a25362d1feb9ee1064467c3356
parent57fb2b88ba050e4c376fdbe1d9eca83966e0c4a4 (diff)
downloadcetus-c55368d4f45cafcc7efd7553fa4cb69360c64d6d.tar.gz
Disable caching on random (bpod)
-rw-r--r--bpod/json.go19
-rw-r--r--cmd/cetus/bpod.go78
2 files changed, 11 insertions, 86 deletions
diff --git a/bpod/json.go b/bpod/json.go
index 8829f36..3c24bf7 100644
--- a/bpod/json.go
+++ b/bpod/json.go
@@ -26,25 +26,6 @@ type List struct {
 	Photos []BPOD `json:"images"`
 }
 
-// MarshalJson takes res as input and returns body. This remarshaling
-// is required because of a bug. To learn about why this is required,
-// remove this function & then run `cetus set bpod -random`. Put a
-// `fmt.Println(res, body)` somewhere and look at how they differ. res
-// will contain a single entry but body will have all 7 entries which
-// is bad because body is cached to disk to view later. Look at
-// comment in UnmarshalJson func to understand why res has a single
-// entry and body has all when random flag is passed.
-func MarshalJson(res BPOD) (string, error) {
-	out, err := json.Marshal(res)
-	if err != nil {
-		err = fmt.Errorf("%s\n%s",
-			"MarshalJson failed",
-			err.Error())
-	}
-	body := string(out)
-	return body, err
-}
-
 // UnmarshalJson will take body as input & unmarshal it to res,
 func UnmarshalJson(body string) (BPOD, error) {
 	list := List{}
diff --git a/cmd/cetus/bpod.go b/cmd/cetus/bpod.go
index e2bb8e2..0c5b7cf 100644
--- a/cmd/cetus/bpod.go
+++ b/cmd/cetus/bpod.go
@@ -76,7 +76,9 @@ func execBPOD() {
 	} else {
 		// If random then get the file and save it to disk
 		// after unmarshal because we don't know the file name
-		// yet
+		// yet. Caching on random was disabled because the
+		// solution was very hacky and inconsistent with
+		// caching on random = false.
 		body, err = bpod.GetJson(reqInfo)
 		if err != nil {
 			err = fmt.Errorf("%s\n%s",
@@ -95,36 +97,6 @@ func execBPOD() {
 		log.Fatal(err)
 	}
 
-	// res and body are out of sync if random was passed because
-	// body has all 7 entries but res has only one because
-	// UnmarshalJson returns only one entry selected randomly.
-	// Look at comment in UnmarshalJson and MarshalJson to
-	// understand why this is required.
-	if random {
-		body, err = bpod.MarshalJson(res)
-
-		// If an error was caused then that means body and res
-		// is still out of sync which is not a big issue and
-		// program shouldn't stop because of this, instead we
-		// set random=false so that this body doesn't get
-		// saved to cache and also add warning if user passed
-		// the dump flag because dump is dumping body which
-		// has all 7 entries.
-		if err != nil {
-			err = fmt.Errorf("%s\n%s",
-				"bpod.go: failed to marshal res to body, both out of sync",
-				err.Error())
-			log.Println(err)
-
-			log.Println("bpod.go: not saving this to cache")
-			log.Println("bpod.go: dump will contain incorrect information")
-
-			// This will prevent the program from saving
-			// this to cache.
-			random = false
-		}
-	}
-
 	// Correct format
 	res.URL = fmt.Sprintf("%s%s", "https://www.bing.com", res.URL)
 	dt, err := time.Parse("20060102", res.StartDate)
@@ -134,23 +106,6 @@ func execBPOD() {
 	res.StartDate = dt.Format("2006-01-02")
 
 	file = fmt.Sprintf("%s/%s.json", cacheDir, res.StartDate)
-	if random {
-
-		// Write body to the cache so that it can be read
-		// later.
-		err = ioutil.WriteFile(file, []byte(body), 0644)
-
-		// Not being able to write to the cache file is a
-		// small error and the program shouldn't exit but
-		// should continue after printing the log so that the
-		// user can investigate it later.
-		if err != nil {
-			err = fmt.Errorf("%s\n%s",
-				"bpod.go: failed to write body to file: ", file,
-				err.Error())
-			log.Println(err)
-		}
-	}
 
 	if dump {
 		fmt.Printf(body)
@@ -219,27 +174,16 @@ func dlAndCacheBPODBody() {
 		log.Fatal(err)
 	}
 
-	// Write body to the cache so that it can be read later. This
-	// will mess up the cache because the cache format will be
-	// inconsistent, res is not the same as body and they should
-	// be in sync before saving body to cache but here we don't
-	// yet have res. This issue arises when the user passes random
-	// flag, we are saving to cache the raw body returned but when
-	// random is passed the format of body is changed to only
-	// values unmarshalled in res because body is rebuilt from res
-	// so the cache will have different format of body. Disabling
-	// this cache for now seems like a good option, later we can
-	// figure out how to make this body and body when random is
-	// passed of same format.
-	// err = ioutil.WriteFile(file, []byte(body), 0644)
+	// Write body to the cache so that it can be read later.
+	err = ioutil.WriteFile(file, []byte(body), 0644)
 
 	// Not being able to write to the cache file is a small error
 	// and the program shouldn't exit but should continue after
 	// printing the log so that the user can investigate it later.
-	// if err != nil {
-	// 	err = fmt.Errorf("%s\n%s",
-	// 		"bpod.go: failed to write body to file: ", file,
-	// 		err.Error())
-	// 	log.Println(err)
-	// }
+	if err != nil {
+		err = fmt.Errorf("%s\n%s",
+			"bpod.go: failed to write body to file: ", file,
+			err.Error())
+		log.Println(err)
+	}
 }
href='#n354'>354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632