22//
33// SPDX-License-Identifier: GPL-3.0-or-later
44
5+ use etcetera:: AppStrategy as _;
56use eyre:: { eyre, Result } ;
67use std:: path:: { Path , PathBuf } ;
78use tokio:: fs;
@@ -28,68 +29,33 @@ pub struct Userchrome {
2829 pub configs : Vec < UserchromeConfig > ,
2930}
3031
31- #[ derive( Deserialize , Serialize , Debug ) ]
32+ #[ derive( Deserialize , Serialize , Debug , Default ) ]
3233pub struct Config {
3334 pub profile : Option < PathBuf > ,
3435
3536 #[ serde( default ) ]
3637 pub userchromes : Vec < Userchrome > ,
3738}
3839
39- pub fn get_old_config_path ( ) -> Result < PathBuf > {
40- dirs:: config_dir ( )
41- . ok_or_else ( || eyre ! ( "Unable to locate config dirs" ) )
42- . map ( |dir| dir. join ( "nyoom.toml" ) )
40+ fn strategy ( ) -> Result < impl etcetera:: AppStrategy > {
41+ etcetera:: choose_app_strategy ( etcetera:: AppStrategyArgs {
42+ top_level_domain : "dev.ryanccn" . to_owned ( ) ,
43+ author : "Ryan Cao" . to_owned ( ) ,
44+ app_name : "nyoom" . to_owned ( ) ,
45+ } )
46+ . map_err ( |e| e. into ( ) )
4347}
4448
4549pub fn get_default_config_path ( ) -> Result < PathBuf > {
46- dirs:: config_dir ( )
47- . ok_or_else ( || eyre ! ( "Unable to locate config dirs" ) )
48- . map ( |dir| dir. join ( "nyoom" ) . join ( "nyoom.toml" ) )
49- }
50-
51- pub async fn migrate_config ( ) -> Result < ( ) > {
52- let old = get_old_config_path ( ) ?;
53- let new = get_default_config_path ( ) ?;
54-
55- if old. exists ( ) && !new. exists ( ) {
56- fs:: create_dir_all (
57- new. parent ( )
58- . ok_or_else ( || eyre ! ( "Could not obtain parent directory of config" ) ) ?,
59- )
60- . await ?;
61- fs:: copy ( old, new) . await ?;
62- }
63-
64- Ok ( ( ) )
50+ Ok ( strategy ( ) ?. config_dir ( ) . join ( "nyoom.toml" ) )
6551}
6652
6753pub async fn get_config ( path : & Path ) -> Result < Config > {
68- let f = if Path :: new ( path) . exists ( ) {
69- fs:: read_to_string ( path) . await ?
70- } else {
71- String :: new ( )
72- } ;
73- let mut config: Config = toml:: from_str ( & f) ?;
74-
75- let mut migrated = false ;
76-
77- for uc in & mut config. userchromes {
78- if let Some ( old_clone_url) = & uc. clone_url {
79- uc. source = old_clone_url
80- . replace ( "https://github.com/" , "github:" )
81- . replace ( ".git" , "" ) ;
82- uc. clone_url = None ;
83-
84- migrated = true ;
85- }
54+ match fs:: read_to_string ( path) . await {
55+ Ok ( s) => toml:: from_str ( & s) . map_err ( Into :: into) ,
56+ Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound => Ok ( Config :: default ( ) ) ,
57+ Err ( e) => Err ( e. into ( ) ) ,
8658 }
87-
88- if migrated {
89- set_config ( path, & config) . await ?;
90- }
91-
92- Ok ( config)
9359}
9460
9561pub async fn set_config ( path : & Path , config : & Config ) -> Result < ( ) > {
0 commit comments