@@ -19,40 +19,32 @@ use crate::{
1919 utils:: binary_metadata:: binary_metadata,
2020} ;
2121use clap:: Parser ;
22+ use std:: error:: Error ;
23+ use std:: process:: ExitCode ;
2224use std:: sync:: Arc ;
23- use thiserror:: Error ;
24- use tracing:: info;
25+ use tracing:: { error, info} ;
2526
2627/// All possible errors that can happen while running the initialization.
27- #[ derive( Debug , Error ) ]
28+ #[ derive( Debug , thiserror :: Error ) ]
2829pub enum InitError {
2930 /// Could not initialize tracer
30- #[ error( "could not initialize tracer: ` {0}` " ) ]
31+ #[ error( "could not initialize tracer: {0}" ) ]
3132 TracerError ( #[ from] TracingError ) ,
3233 /// K8s config is missing
33- #[ error( "k8s config missing while running on k8s " ) ]
34+ #[ error( "k8s config missing while running on k8s" ) ]
3435 K8sConfig ( ) ,
3536 /// The config could not be read
36- #[ error( "could not read Agent Control config from ` {0}`: ` {1}` " ) ]
37+ #[ error( "could not read Agent Control config from {0}: {1}" ) ]
3738 LoaderError ( String , String ) ,
3839 /// The configuration is invalid
39- #[ error( "invalid configuration: ` {0}` " ) ]
40+ #[ error( "invalid configuration: {0}" ) ]
4041 InvalidConfig ( String ) ,
4142}
4243
43- /// What action was requested from the initialization?
44- pub enum Command {
45- /// Normal operation requested. Get the required config and continue.
46- InitAgentControl ( Box < AgentControlRunConfig > , Vec < TracingGuardBox > ) ,
47- /// Do a "one-shot" operation and exit successfully.
48- /// In the future, many different operations could be added here.
49- OneShot ( OneShotCommand ) ,
50- }
51-
5244/// Command line arguments for Agent Control, as parsed by [`clap`].
5345#[ derive( Parser , Debug ) ]
5446#[ command( author, about, long_about = None ) ] // Read from `Cargo.toml`
55- pub struct Flags {
47+ pub struct Command {
5648 #[ arg( long) ]
5749 print_debug_info : bool ,
5850
@@ -75,9 +67,22 @@ pub struct Flags {
7567 pub logs_dir : Option < std:: path:: PathBuf > ,
7668}
7769
78- impl Flags {
79- /// Parses command line arguments and decides how the application runs.
80- pub fn init ( mode : Environment ) -> Result < Command , InitError > {
70+ impl Command {
71+ /// Checks if the flag to show the version was set
72+ fn print_version ( & self ) -> bool {
73+ self . version
74+ }
75+
76+ /// Checks if the flag to show debug information was set
77+ fn print_debug_info ( & self ) -> bool {
78+ self . print_debug_info
79+ }
80+
81+ /// Runs the provided main function or shows the binary information according to flags
82+ pub fn run < F : Fn ( AgentControlRunConfig , Vec < TracingGuardBox > ) -> Result < ( ) , Box < dyn Error > > > (
83+ mode : Environment ,
84+ main_fn : F ,
85+ ) -> ExitCode {
8186 // Get command line args
8287 let flags = Self :: parse ( ) ;
8388
@@ -87,14 +92,44 @@ impl Flags {
8792 #[ cfg( debug_assertions) ]
8893 let base_paths = set_debug_dirs ( base_paths, & flags) ;
8994
90- // If the version flag is set, print the version and exit
95+ // Handle flags requiring different execution mode
9196 if flags. print_version ( ) {
92- return Ok ( Command :: OneShot ( OneShotCommand :: PrintVersion ) ) ;
97+ println ! ( "{}" , binary_metadata( mode) ) ;
98+ return ExitCode :: SUCCESS ;
9399 }
94100 if flags. print_debug_info ( ) {
95- return Ok ( Command :: OneShot ( OneShotCommand :: PrintDebugInfo ( flags) ) ) ;
101+ println ! ( "Printing debug info" ) ;
102+ println ! ( "Agent Control Mode: {mode:?}" ) ;
103+ println ! ( "FLAGS: {flags:#?}" ) ;
104+ return ExitCode :: SUCCESS ;
105+ }
106+
107+ let Ok ( ( run_config, tracer) ) =
108+ Self :: init_agent_control ( mode, base_paths) . inspect_err ( |err| {
109+ // Using print because the tracer might have failed to start
110+ println ! ( "Error on Agent Control initialization: {err}" ) ;
111+ } )
112+ else {
113+ return ExitCode :: FAILURE ;
114+ } ;
115+
116+ match main_fn ( run_config, tracer) {
117+ Ok ( _) => {
118+ info ! ( "The agent control main process exited successfully" ) ;
119+ ExitCode :: SUCCESS
120+ }
121+ Err ( err) => {
122+ error ! ( "The agent control main process exited with an error: {err}" ) ;
123+ ExitCode :: FAILURE
124+ }
96125 }
126+ }
97127
128+ /// Builds the Agent Control configuration required to execute the application.
129+ fn init_agent_control (
130+ mode : Environment ,
131+ base_paths : BasePaths ,
132+ ) -> Result < ( AgentControlRunConfig , Vec < TracingGuardBox > ) , InitError > {
98133 let agent_control_repository =
99134 ConfigRepositoryFile :: new ( base_paths. local_dir . clone ( ) , base_paths. remote_dir . clone ( ) ) ;
100135
@@ -148,39 +183,6 @@ impl Flags {
148183 } ,
149184 agent_type_var_constraints,
150185 } ;
151-
152- Ok ( Command :: InitAgentControl ( Box :: new ( run_config) , tracer) )
153- }
154-
155- fn print_version ( & self ) -> bool {
156- self . version
157- }
158-
159- fn print_debug_info ( & self ) -> bool {
160- self . print_debug_info
161- }
162- }
163-
164- /// One-shot operations that can be performed by the agent-control
165- pub enum OneShotCommand {
166- /// Print the version of the agent-control and exits
167- PrintVersion ,
168- /// Print debug information and exits
169- PrintDebugInfo ( Flags ) ,
170- }
171-
172- impl OneShotCommand {
173- /// Runs the one-shot operation
174- pub fn run_one_shot ( & self , env : Environment ) {
175- match self {
176- OneShotCommand :: PrintVersion => {
177- println ! ( "{}" , binary_metadata( env) ) ;
178- }
179- OneShotCommand :: PrintDebugInfo ( flags) => {
180- println ! ( "Printing debug info" ) ;
181- println ! ( "Agent Control Mode: {env:?}" ) ;
182- println ! ( "FLAGS: {flags:#?}" ) ;
183- }
184- }
186+ Ok ( ( run_config, tracer) )
185187 }
186188}
0 commit comments