11use crate :: {
22 constants,
33 options:: { FrameRate , MetaOption , Quality , SType } ,
4- overlay, parser,
4+ overlay, parser, paths ,
55 session:: Session ,
66 Config ,
77} ;
8- use clap:: { ArgMatches , Command } ;
9- use std:: str:: FromStr ;
8+ use clap:: { Arg , ArgMatches , Command } ;
9+ use std:: { fs , io :: Write , process , str:: FromStr } ;
1010
11- fn create_sub_command < ' a > ( st : SType ) -> Command < ' a > {
12- let conf = Config :: new ( st) ;
13- let path = conf. create_path_arg ( ) ;
11+ /* Utils */
12+ // use chrono;
1413
15- Command :: new ( conf. name ) . args ( [
14+ fn create_sub_command ( st : SType ) -> Command < ' static > {
15+ Command :: new ( st. get_name ( ) ) . args ( [
1616 overlay:: create_arg ( ) ,
1717 Quality :: create_arg ( ) ,
1818 FrameRate :: create_arg ( ) ,
19- path,
19+ Arg :: new ( "name" )
20+ . long ( "filename" )
21+ . short ( 'n' )
22+ . takes_value ( true )
23+ . help ( "Name of recorded video" ) ,
2024 ] )
2125}
2226
27+ pub fn get_filename_from_arg ( st : SType , arg_filename : Option < & str > ) -> String {
28+ let name = match arg_filename {
29+ Some ( fname) => fname,
30+ // None => format!("{:?}", chrono::offset::Utc::now()).as_str(), // todo : generate from datetime
31+ None => "123" , // todo : generate from datetime
32+ } ;
33+ match st {
34+ SType :: Record => format ! ( "{}.mkv" , name) ,
35+ SType :: Stream => format ! ( "/{}/{}" , constants:: STREAM_API_PATH , name) ,
36+ }
37+ }
38+
39+ /* Parser */
2340pub fn parse_args ( ) -> ArgMatches {
2441 return Command :: new ( "spur" )
2542 . version ( constants:: VERSION )
@@ -29,16 +46,50 @@ pub fn parse_args() -> ArgMatches {
2946 . subcommands ( [
3047 create_sub_command ( SType :: Record ) ,
3148 create_sub_command ( SType :: Stream ) ,
49+ Command :: new ( "setup" ) . about ( "setting up spur on your machine" ) ,
3250 ] )
3351 . get_matches ( ) ;
3452}
3553
3654pub fn create_session_from_args ( ) -> Session {
37- let mut conf = Config :: default ( ) ;
3855 let matches = parser:: parse_args ( ) ;
3956 match matches. subcommand ( ) {
57+ Some ( ( "setup" , _) ) => {
58+ // todo : abstract to function
59+ // Creating a Videos directory
60+ let videos_path = paths:: get_video_directory_path ( ) ;
61+
62+ fs:: DirBuilder :: new ( )
63+ . recursive ( true )
64+ . create ( & videos_path)
65+ . unwrap_or_else ( |err| {
66+ println ! ( "Couldn't create directory - {}" , videos_path. display( ) ) ;
67+ println ! ( "Error - {:?}" , err) ;
68+ process:: exit ( 1 ) ;
69+ } ) ;
70+
71+ // CURRENTLY, NOT USED
72+ // Saving conf to text file
73+ let home_path = paths:: get_home_path ( ) . expect ( "Couldn't find your Home directory" ) ;
74+ let mut conf_file = fs:: File :: create ( format ! (
75+ "{}/{}" ,
76+ home_path. display( ) ,
77+ constants:: CONFIG_FILE_NAME
78+ ) )
79+ . expect ( "Could not create conf file" ) ;
80+ conf_file
81+ . write_all ( format ! ( "{}" , videos_path. display( ) ) . as_bytes ( ) ) // hacky
82+ . expect ( "Could not write to conf file" ) ;
83+ println ! ( "-------------- Setup is complete ------------------------" ) ;
84+ process:: exit ( 0 ) ;
85+ }
4086 Some ( ( cmd_str, sub_match) ) => {
41- conf. s_type = SType :: from_str ( cmd_str) . unwrap ( ) ;
87+ // Creating config for new session
88+ let st = SType :: from_str ( cmd_str) . unwrap ( ) ;
89+ let arg_filename = sub_match. value_of ( "name" ) ;
90+ let mut conf = Config :: new ( st, get_filename_from_arg ( st, arg_filename) ) ;
91+
92+ // Updating config with parsed parameters
4293 let arg_quality = sub_match
4394 . value_of ( Quality :: COMMAND_NAME )
4495 . unwrap_or_default ( ) ;
0 commit comments