@@ -4,16 +4,29 @@ use anyhow::{Context, Result};
4
4
use serde:: { Deserialize , Serialize } ;
5
5
6
6
use crate :: {
7
- connect_builder:: { IsUnset , SetApiToken , State } ,
7
+ connect_builder:: { IsUnset , SetApiToken , SetBaseurl , State } ,
8
8
ConnectBuilder ,
9
9
} ;
10
10
11
- #[ derive( Debug , Clone , Default , Deserialize , Serialize ) ]
11
+ #[ derive( Debug , Deserialize , Default , Serialize , Clone ) ]
12
+ pub struct Config {
13
+ #[ serde( default ) ]
14
+ api_token : Option < String > ,
15
+ #[ serde( default ) ]
16
+ url : Option < String > ,
17
+
18
+ #[ serde( flatten) ]
19
+ profiles : std:: collections:: HashMap < String , Credentials > ,
20
+ }
21
+
22
+ #[ derive( Debug , Deserialize , Default , Serialize , Clone ) ]
12
23
pub struct Credentials {
13
- pub api_token : Option < String > ,
24
+ pub api_token : String ,
25
+ #[ serde( default ) ]
26
+ pub url : Option < String > ,
14
27
}
15
28
16
- impl Credentials {
29
+ impl Config {
17
30
pub fn path ( ) -> Result < PathBuf > {
18
31
let config_dir = dirs:: config_dir ( )
19
32
. ok_or_else ( || anyhow:: anyhow!( "Could not get user config directory" ) ) ?
@@ -66,23 +79,52 @@ impl Credentials {
66
79
. context ( "Could not write credentials data" )
67
80
}
68
81
69
- pub fn check_api_token ( & self ) -> Result < ( ) > {
70
- let Some ( _api_token) = self . api_token . as_deref ( ) else {
71
- anyhow:: bail!( "Not logged in" ) ;
72
- } ;
82
+ pub fn get ( & self , profile : & Option < String > ) -> Option < Credentials > {
83
+ match profile {
84
+ Some ( profile) => self . profiles . get ( profile) . cloned ( ) ,
85
+ None => match ( self . api_token . clone ( ) , self . url . clone ( ) ) {
86
+ ( Some ( api_token) , Some ( url) ) => Some ( Credentials {
87
+ api_token,
88
+ url : Some ( url) ,
89
+ } ) ,
90
+ ( Some ( api_token) , _) => Some ( Credentials {
91
+ api_token,
92
+ url : Some ( "https://api.edgee.app" . to_string ( ) ) ,
93
+ } ) ,
94
+ _ => None ,
95
+ } ,
96
+ }
97
+ }
73
98
74
- // TODO: Check API token is valid using the API
99
+ pub fn set ( & mut self , profile : Option < String > , creds : Credentials ) {
100
+ match profile {
101
+ Some ( profile) => {
102
+ self . profiles . insert ( profile, creds) ;
103
+ }
104
+ None => {
105
+ self . api_token = Some ( creds. api_token ) ;
106
+ self . url = creds. url ;
107
+ }
108
+ }
109
+ }
110
+ }
75
111
112
+ impl Credentials {
113
+ pub fn check_api_token ( & self ) -> Result < ( ) > {
114
+ // TODO: Check API token is valid using the API
76
115
Ok ( ( ) )
77
116
}
78
117
}
79
118
80
- impl < ' a , S : State > ConnectBuilder < ' a , S > {
81
- pub fn credentials ( self , creds : & Credentials ) -> ConnectBuilder < ' a , SetApiToken < S > >
119
+ impl < S : State > ConnectBuilder < S > {
120
+ pub fn credentials ( self , creds : & Credentials ) -> ConnectBuilder < SetApiToken < SetBaseurl < S > > >
82
121
where
83
122
S :: ApiToken : IsUnset ,
123
+ S :: Baseurl : IsUnset ,
84
124
{
85
- let api_token = creds. api_token . as_deref ( ) . unwrap ( ) ;
86
- self . api_token ( api_token)
125
+ let api_token = creds. api_token . clone ( ) ;
126
+ let url = creds. url . clone ( ) ;
127
+ self . baseurl ( url. unwrap_or ( "https://api.edgee.app" . to_string ( ) ) )
128
+ . api_token ( api_token)
87
129
}
88
130
}
0 commit comments