Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
git_borg_linker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LBMC
Hub
git_borg_linker
Commits
f1542f28
Verified
Commit
f1542f28
authored
Jan 13, 2023
by
nfontrod
Browse files
Options
Downloads
Patches
Plain Diff
src/remote.rs: add update_config and remove_config function
parent
a5401655
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/remote.rs
+71
-7
71 additions, 7 deletions
src/remote.rs
with
71 additions
and
7 deletions
src/remote.rs
+
71
−
7
View file @
f1542f28
use
crate
::
config_structure
::{
Config
,
RemoteConfig
};
use
crate
::
configt
::{
get_borgconfig
,
get_global_config
,
parse_toml
};
use
crate
::
configt
::{
get_borgconfig
,
get_global_config
,
write_config
};
use
colored
::
Colorize
;
use
regex
::
Regex
;
use
std
::
collections
::
BTreeMap
as
Dict
;
use
std
::
path
::
PathBuf
;
...
...
@@ -30,7 +31,7 @@ pub fn parse_config(global: bool) -> Config {
return
Config
{
repository
:
None
,
gblk_prune
:
None
,
remote
:
None
,
gblk_
remote
:
None
,
};
}
Some
(
c
)
=>
c
,
...
...
@@ -50,7 +51,7 @@ pub fn parse_config(global: bool) -> Config {
return
Config
{
repository
:
None
,
gblk_prune
:
None
,
remote
:
None
,
gblk_
remote
:
None
,
};
}
let
re
=
Regex
::
new
(
"id = (?P<first>[0-9a-z]+)
\\
W"
)
.unwrap
();
...
...
@@ -97,12 +98,75 @@ fn combine_remote(
dic
}
/// Get the remote config of a configuration file
/// # Arguments
/// - global: a boolean indicating to returns the remotes the the global or
/// local configuration file
/// # Return
/// Return the remotes of the local or the global configuration file
fn
get_remotes
(
global
:
bool
)
->
Option
<
Vec
<
RemoteConfig
>>
{
let
my_config
=
parse_config
(
global
);
my_config
.gblk_remote
}
/// Function that show the current gblk config of the borg folder
pub
fn
show
()
->
()
{
let
global_config
=
parse_config
(
true
);
let
local_config
=
parse_config
(
false
);
let
remote_global
=
global_config
.remote
;
let
remote_local
=
local_config
.remote
;
let
remote_global
=
get_remotes
(
true
);
let
remote_local
=
get_remotes
(
false
);
let
dic_remote
=
combine_remote
(
remote_global
,
remote_local
);
display_remotes
(
dic_remote
);
}
/// Function that update the current gblk config file for the project folder
/// # Arguments
/// - `key` : The remote name to update
/// - `value`: The value of the key to update
/// - `global: A bolean indicating whether to update the global or the local configuration
pub
fn
update_config
(
key
:
&
str
,
value
:
&
str
,
global
:
bool
)
->
()
{
let
mut
my_config
=
parse_config
(
global
);
let
mut
remotes
=
match
my_config
.gblk_remote
{
Some
(
r
)
=>
r
,
None
=>
Vec
::
new
(),
};
let
mut
updated
=
false
;
for
r
in
remotes
.iter_mut
()
{
if
r
.name
==
key
{
r
.url
=
value
.to_owned
();
updated
=
true
;
}
}
if
!
updated
{
remotes
.push
(
RemoteConfig
{
name
:
key
.to_owned
(),
url
:
value
.to_owned
(),
})
}
my_config
.gblk_remote
=
Some
(
remotes
);
write_config
(
&
my_config
,
global
);
}
/// Function that remove a given remote in the current gblk config file for the project folder
/// # Arguments
/// - `key` : The remote name to delete
/// - `global: A bolean indicating whether to remove the key in the global or
/// the local configuration file
pub
fn
remove_config
(
key
:
&
str
,
global
:
bool
)
->
()
{
let
mut
my_config
=
parse_config
(
global
);
let
mut
remotes
=
match
my_config
.gblk_remote
{
Some
(
r
)
=>
r
,
None
=>
Vec
::
new
(),
};
let
init_size
=
remotes
.len
();
remotes
.retain
(|
x
|
&
x
.name
!=
&
key
);
let
end_size
=
remotes
.len
();
if
init_size
==
end_size
{
eprintln!
(
"{}: The remote {} doesn't exists !"
,
"error"
.red
(),
key
);
exit
(
1
);
}
my_config
.gblk_remote
=
if
remotes
.len
()
>
0
{
Some
(
remotes
)
}
else
{
None
};
write_config
(
&
my_config
,
global
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment