Skip to content

Commit 339dce5

Browse files
committed
Update from PR comments
1 parent 8ba0e93 commit 339dce5

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

src/adapters/backend/mod.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,10 @@ impl BackendClient {
4545
pub fn new(cli: &Cli) -> Result<Self> {
4646
let fs = FileSystem::new().unwrap();
4747

48-
// Load the user's session information from the filesystem.
49-
let session = fs.load_file(SessionFile).map(|session| session);
48+
// Load the user's session information from the filesystem, if it exists
49+
let session = fs.load_file(SessionFile)?;
5050

51-
let creds = match session {
52-
Session::User(creds) => creds,
53-
};
54-
55-
let conf = BackendConfig::new(cli.origin(), Some(creds.jwt.clone()));
51+
let conf = BackendConfig::new(cli.origin(), Some(session.clone()));
5652

5753
let raw_conf: Configuration = conf.clone().into();
5854

@@ -64,8 +60,12 @@ impl BackendClient {
6460
})
6561
}
6662

67-
pub fn is_authenicated(&self) -> bool {
68-
self.session.is_some()
63+
pub fn is_authenicated(&self) -> Result<()> {
64+
if self.session.clone().is_some_and(Session::is_not_expired) {
65+
return Ok(());
66+
} else {
67+
bail!("Please login before running this command.");
68+
}
6969
}
7070

7171
/// Given the workspace name and the application name, fetch
@@ -114,9 +114,8 @@ impl BackendClient {
114114

115115
/// Return information about the workspace given its name.
116116
async fn get_workspace_by_name(&self, name: &str) -> Result<WorkspaceSummary> {
117-
if !self.is_authenicated() {
118-
bail!("Please login before running this command.");
119-
}
117+
self.is_authenicated()?;
118+
120119
// let mut workspaces = list_workspaces(&self.conf, Some(name))
121120
let mut workspaces: Vec<_> = self
122121
.client
@@ -148,9 +147,7 @@ impl BackendClient {
148147
workspace_id: Uuid,
149148
name: &str,
150149
) -> Result<ApplicationDetails> {
151-
if !self.is_authenicated() {
152-
bail!("Please login before running this command.");
153-
}
150+
self.is_authenicated()?;
154151

155152
let mut applications: Vec<_> = self
156153
.client
@@ -200,12 +197,15 @@ pub(super) struct BackendConfig {
200197
}
201198

202199
impl BackendConfig {
203-
pub fn new<T: AsRef<str>>(origin: Option<T>, jwt: Option<String>) -> Self {
200+
pub fn new<T: AsRef<str>>(origin: Option<T>, session: Option<Session>) -> Self {
204201
// • Convert the Option<T> to a String.
205202
let origin = origin.map(|val| val.as_ref().to_owned());
206203
// • Set up the default configuration values.
204+
let jwt = session.and_then(|session| match session {
205+
Session::User(creds) => Some(creds.jwt),
206+
});
207207
let conf = Configuration {
208-
base_path: origin.unwrap(),
208+
base_path: origin.unwrap_or("https://api.multitool.run".to_string()),
209209
bearer_access_token: jwt,
210210
..Configuration::default()
211211
};

src/cmd/login.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ pub struct Login {
1212
}
1313

1414
impl Login {
15-
pub fn new(terminal: Terminal, cli: &Cli, flags: LoginSubcommand) -> Self {
16-
let backend = BackendClient::new(cli).unwrap();
17-
Self {
15+
pub fn new(terminal: Terminal, cli: &Cli, flags: LoginSubcommand) -> Result<Self> {
16+
let backend = BackendClient::new(cli)?;
17+
Ok(Self {
1818
terminal,
1919
flags,
2020
backend,
21-
}
21+
})
2222
}
2323

2424
pub async fn dispatch(self) -> Result<()> {

src/config/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl MultiCommand {
2424
/// dispatch the user-provided arguments to the command handler.
2525
pub async fn dispatch(self, console: Terminal, cli: &Cli) -> Result<()> {
2626
match self {
27-
Self::Login(flags) => Login::new(console, cli, flags).dispatch().await,
27+
Self::Login(flags) => Login::new(console, cli, flags)?.dispatch().await,
2828
Self::Logout => Logout::new(console).dispatch(),
2929
Self::Run(flags) => Run::new(console, cli, flags).dispatch().await,
3030
Self::Version => Version::new(console).dispatch(),

src/fs/session.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ pub enum Session {
1414
}
1515

1616
impl Session {
17-
pub fn is_not_expired(creds: UserCreds) -> Self {
18-
if creds.expiry >= Utc::now() {
19-
Self::User(creds)
20-
} else {
21-
panic!("Login token expired, please login again with \'multitool login\'.");
17+
pub fn is_not_expired(self) -> bool {
18+
match self {
19+
Self::User(creds) => creds.expiry >= Utc::now(),
2220
}
2321
}
2422
}

0 commit comments

Comments
 (0)