Skip to content

Commit 1f3463f

Browse files
committed
improved settings set, added base64 flag
1 parent 7cbd4c9 commit 1f3463f

File tree

3 files changed

+19
-51
lines changed

3 files changed

+19
-51
lines changed

tmc-langs-cli/src/app.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::output::UpdatedExercise;
44
use anyhow::Context;
55
use clap::AppSettings;
66
use schemars::JsonSchema;
7-
use serde_json::Value as Json;
87
use std::{path::PathBuf, str::FromStr};
98
use structopt::StructOpt;
109
use tmc_langs::{
@@ -26,13 +25,13 @@ use tmc_langs::{
2625
)]
2726
pub struct Opt {
2827
/// Pretty-prints all output
29-
#[structopt(long)]
28+
#[structopt(long, short)]
3029
pub pretty: bool,
3130
/// Name used to differentiate between different TMC clients.
32-
#[structopt(long)]
31+
#[structopt(long, short)]
3332
pub client_name: Option<String>,
3433
/// Client version.
35-
#[structopt(long)]
34+
#[structopt(long, short = "v")]
3635
pub client_version: Option<String>,
3736
#[structopt(subcommand)]
3837
pub cmd: Command,
@@ -525,7 +524,9 @@ pub enum Settings {
525524
/// The key. Parsed as JSON, assumed to be a string if parsing fails.
526525
key: String,
527526
/// The value in JSON.
528-
json: Json,
527+
json: String,
528+
#[structopt(long)]
529+
base64: bool,
529530
},
530531
/// Unsets a value from the settings
531532
Unset {

tmc-langs-cli/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use anyhow::{Context, Result};
1515
use app::{Command, Core, OutputFormatWrapper, Settings};
1616
use clap::{Error, ErrorKind};
1717
use serde::Serialize;
18+
use serde_json::Value;
1819
use std::collections::HashMap;
1920
use std::env;
2021
use std::fs::File;
@@ -982,8 +983,14 @@ fn run_settings(client_name: &str, settings: Settings) -> Result<Output> {
982983
Output::finished("moved project directory")
983984
}
984985

985-
Settings::Set { key, json } => {
986-
tmc_langs::set_setting(client_name, &key, &json.to_string())?;
986+
Settings::Set { key, json, base64 } => {
987+
let json: Value = if base64 {
988+
let json = base64::decode(&json)?;
989+
serde_json::from_slice(&json)?
990+
} else {
991+
serde_json::from_str(&json)?
992+
};
993+
tmc_langs::set_setting(client_name, &key, &json)?;
987994
Output::finished("set setting")
988995
}
989996

tmc-langs/src/lib.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ use jwt::SignWithKey;
4848
use oauth2::{
4949
basic::BasicTokenType, AccessToken, EmptyExtraTokenFields, Scope, StandardTokenResponse,
5050
};
51-
use serde_json::Value as JsonValue;
5251
use std::{
5352
collections::BTreeMap,
5453
convert::TryFrom,
@@ -60,7 +59,7 @@ use std::{collections::HashMap, ffi::OsStr};
6059
use tmc_langs_framework::{TmcError, TmcProjectYml};
6160
use tmc_langs_plugins::{get_language_plugin, tmc_zip, AntPlugin, PluginType};
6261
use tmc_langs_util::progress_reporter;
63-
use toml::{map::Map as TomlMap, Value as TomlValue};
62+
use toml::Value as TomlValue;
6463
use url::Url;
6564
use walkdir::WalkDir;
6665

@@ -675,19 +674,12 @@ pub fn get_settings(client_name: &str) -> Result<TmcConfig, LangsError> {
675674
}
676675

677676
/// Saves a setting in the config.
678-
pub fn set_setting(client_name: &str, key: &str, value: &str) -> Result<(), LangsError> {
679-
log::debug!("setting {}={} in {}", key, value, client_name);
677+
pub fn set_setting<T: Serialize>(client_name: &str, key: &str, value: T) -> Result<(), LangsError> {
678+
log::debug!("setting {} in {}", key, client_name);
680679

681680
let config_path = TmcConfig::get_location(client_name)?;
682681
let mut tmc_config = TmcConfig::load(client_name, &config_path)?;
683-
let value = match serde_json::from_str(value) {
684-
Ok(json) => json,
685-
Err(_) => {
686-
// interpret as string
687-
JsonValue::String(value.to_string())
688-
}
689-
};
690-
let value = setting_json_to_toml(value)?;
682+
let value = TomlValue::try_from(value)?;
691683

692684
tmc_config.insert(key.to_string(), value)?;
693685
tmc_config.save(&config_path)?;
@@ -714,38 +706,6 @@ pub fn unset_setting(client_name: &str, key: &str) -> Result<(), LangsError> {
714706
Ok(())
715707
}
716708

717-
fn setting_json_to_toml(json: JsonValue) -> Result<TomlValue, LangsError> {
718-
match json {
719-
JsonValue::Array(arr) => {
720-
let mut v = vec![];
721-
for value in arr {
722-
v.push(setting_json_to_toml(value)?);
723-
}
724-
Ok(TomlValue::Array(v))
725-
}
726-
JsonValue::Bool(b) => Ok(TomlValue::Boolean(b)),
727-
JsonValue::Null => Err(LangsError::SettingsCannotContainNull),
728-
JsonValue::Number(num) => {
729-
if let Some(int) = num.as_i64() {
730-
Ok(TomlValue::Integer(int))
731-
} else if let Some(float) = num.as_f64() {
732-
Ok(TomlValue::Float(float))
733-
} else {
734-
// this error can occur because serde_json supports u64 ints but toml doesn't
735-
Err(LangsError::SettingNumberTooHigh(num))
736-
}
737-
}
738-
JsonValue::Object(obj) => {
739-
let mut map = TomlMap::new();
740-
for (key, value) in obj {
741-
map.insert(key, setting_json_to_toml(value)?);
742-
}
743-
Ok(TomlValue::Table(map))
744-
}
745-
JsonValue::String(s) => Ok(TomlValue::String(s)),
746-
}
747-
}
748-
749709
/// Checks the exercise's code quality.
750710
pub fn checkstyle(
751711
exercise_path: &Path,

0 commit comments

Comments
 (0)