Skip to content

Commit a35d0f5

Browse files
authored
Merge pull request #186 from rage/shorthand
fixes
2 parents 8a2915f + 1f3463f commit a35d0f5

File tree

4 files changed

+23
-52
lines changed

4 files changed

+23
-52
lines changed

tmc-client/src/tmc_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct TmcCore {
5656
// TODO: cache API results?
5757
impl TmcClient {
5858
/// Convenience function for checking authentication.
59-
fn require_authentication(&self) -> Result<(), ClientError> {
59+
pub fn require_authentication(&self) -> Result<(), ClientError> {
6060
if self.0.token.is_some() {
6161
Ok(())
6262
} else {

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: 7 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

@@ -208,6 +207,7 @@ pub fn paste_exercise(
208207
}
209208

210209
/// Downloads the given exercises, by either downloading the exercise template, updating the exercise or downloading an old submission.
210+
/// Requires authentication.
211211
/// If the exercise doesn't exist on disk yet...
212212
/// if there are previous submissions and download_template is not set, the latest submission is downloaded.
213213
/// otherwise, the exercise template is downloaded.
@@ -223,6 +223,8 @@ pub fn download_or_update_course_exercises(
223223
projects_dir.display()
224224
);
225225

226+
client.require_authentication()?;
227+
226228
let exercises_details = client.get_exercises_details(exercises)?;
227229
let projects_config = ProjectsConfig::load(projects_dir)?;
228230

@@ -672,19 +674,12 @@ pub fn get_settings(client_name: &str) -> Result<TmcConfig, LangsError> {
672674
}
673675

674676
/// Saves a setting in the config.
675-
pub fn set_setting(client_name: &str, key: &str, value: &str) -> Result<(), LangsError> {
676-
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);
677679

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

689684
tmc_config.insert(key.to_string(), value)?;
690685
tmc_config.save(&config_path)?;
@@ -711,38 +706,6 @@ pub fn unset_setting(client_name: &str, key: &str) -> Result<(), LangsError> {
711706
Ok(())
712707
}
713708

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

0 commit comments

Comments
 (0)