sapling/eden/mononoke/cmds/admin/blobstore_upload.rs
Thomas Orozco c2c904f933 mononoke: initialize loggers, config, caching, tunables & runtime in MononokeMatches
Summary:
Basically every single Mononoke binary starts with the same preamble:

- Init mononoke
- Init caching
- Init logging
- Init tunables

Some of them forget to do it, some don't, etc. This is a mess.

To make things messier, our initialization consists of a bunch of lazy statics
interacting with each other (init logging & init configerator are kinda
intertwined due to the fact that configerator wants a logger but dynamic
observability wants a logger), and methods you must only call once.

This diff attempts to clean this up by moving all this initialization into the
construction of MononokeMatches. I didn't change all the accessor methods
(though I did update those that would otherwise return things instantiated at
startup).

I'm planning to do a bit more on top of this, as my actual goal here is to make
it easier to thread arguments from MononokeMatches to RepoFactory, and to do so
I'd like to just pass my MononokeEnvironment as an input to RepoFactory.

Reviewed By: HarveyHunt

Differential Revision: D27767698

fbshipit-source-id: 00d66b07b8c69f072b92d3d3919393300dd7a392
2021-04-16 10:27:43 -07:00

67 lines
2.0 KiB
Rust

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
use anyhow::{anyhow, Error};
use blobstore::{Blobstore, BlobstoreBytes};
use clap::{App, Arg, ArgMatches, SubCommand};
use cmdlib::args::{self, MononokeMatches};
use context::CoreContext;
use fbinit::FacebookInit;
use futures::TryFutureExt;
use slog::{info, Logger};
use crate::error::SubcommandError;
pub const KEY_ARG: &str = "key";
pub const VALUE_FILE_ARG: &str = "value-file";
pub const BLOBSTORE_UPLOAD: &str = "blobstore-upload";
pub fn build_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name(BLOBSTORE_UPLOAD)
.about("uploads blobs to blobstore")
.arg(
Arg::with_name(KEY_ARG)
.long(KEY_ARG)
.takes_value(true)
.required(true)
.help("blobstore key to upload to"),
)
.arg(
Arg::with_name(VALUE_FILE_ARG)
.long(VALUE_FILE_ARG)
.takes_value(true)
.required(true)
.help("file with value to write to blobstore"),
)
}
pub async fn subcommand_blobstore_upload<'a>(
fb: FacebookInit,
logger: Logger,
matches: &'a MononokeMatches<'a>,
sub_m: &'a ArgMatches<'a>,
) -> Result<(), SubcommandError> {
let ctx = CoreContext::new_with_logger(fb, logger.clone());
let repo = args::open_repo(fb, &logger, &matches).await?;
let key = sub_m
.value_of(KEY_ARG)
.ok_or_else(|| anyhow!("{} is not set", KEY_ARG))?;
let value_file = sub_m
.value_of(VALUE_FILE_ARG)
.ok_or_else(|| anyhow!("{} is not set", VALUE_FILE_ARG))?;
let data = tokio::fs::read(value_file).map_err(Error::from).await?;
info!(ctx.logger(), "writing {} bytes to blobstore", data.len());
repo.blobstore()
.put(&ctx, key.to_string(), BlobstoreBytes::from_bytes(data))
.await?;
Ok(())
}