44 "context"
55 "os"
66 "os/signal"
7+ "strconv"
8+ "strings"
79 "syscall"
810 "time"
911
@@ -26,6 +28,31 @@ func main() {
2628 rootCmd := & cobra.Command {
2729 Use : "parrot" ,
2830 Short : "A server that can register and parrot back dynamic requests" ,
31+ PreRun : func (cmd * cobra.Command , args []string ) {
32+ // Check environment variables if flags are not set
33+ if ! cmd .Flags ().Changed ("port" ) {
34+ if envPort , err := strconv .Atoi (os .Getenv ("PARROT_PORT" )); err == nil {
35+ port = envPort
36+ }
37+ }
38+ if ! cmd .Flags ().Changed ("debug" ) {
39+ debug = os .Getenv ("PARROT_DEBUG" ) == "true"
40+ }
41+ if ! cmd .Flags ().Changed ("trace" ) {
42+ trace = os .Getenv ("PARROT_TRACE" ) == "true"
43+ }
44+ if ! cmd .Flags ().Changed ("silent" ) {
45+ silent = os .Getenv ("PARROT_SILENT" ) == "true"
46+ }
47+ if ! cmd .Flags ().Changed ("json" ) {
48+ json = os .Getenv ("PARROT_JSON" ) == "true"
49+ }
50+ if ! cmd .Flags ().Changed ("recorders" ) {
51+ if envRecorders := os .Getenv ("PARROT_RECORDERS" ); envRecorders != "" {
52+ recorders = strings .Split (envRecorders , "," )
53+ }
54+ }
55+ },
2956 RunE : func (cmd * cobra.Command , args []string ) error {
3057 options := []parrot.ServerOption {parrot .WithPort (port )}
3158 logLevel := zerolog .InfoLevel
@@ -47,7 +74,7 @@ func main() {
4774 ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
4875 defer cancel ()
4976
50- p , err := parrot .Wake (options ... )
77+ p , err := parrot .NewServer (options ... )
5178 if err != nil {
5279 return err
5380 }
@@ -63,12 +90,12 @@ func main() {
6390 },
6491 }
6592
66- rootCmd .Flags ().IntVarP (& port , "port" , "p" , 0 , "Port to run the parrot on" )
67- rootCmd .Flags ().BoolVarP (& debug , "debug" , "d" , false , "Enable debug output" )
68- rootCmd .Flags ().BoolVarP (& trace , "trace" , "t" , false , "Enable trace and debug output" )
69- rootCmd .Flags ().BoolVarP (& silent , "silent" , "s" , false , "Disable all output" )
70- rootCmd .Flags ().BoolVarP (& json , "json" , "j" , false , "Output logs in JSON format" )
71- rootCmd .Flags ().StringSliceVarP (& recorders , "recorders" , "r" , nil , "Existing recorders to use" )
93+ rootCmd .Flags ().IntVarP (& port , "port" , "p" , 0 , "Port to run the parrot on (env: PARROT_PORT) " )
94+ rootCmd .Flags ().BoolVarP (& debug , "debug" , "d" , false , "Enable debug output (env: PARROT_DEBUG) " )
95+ rootCmd .Flags ().BoolVarP (& trace , "trace" , "t" , false , "Enable trace and debug output (env: PARROT_TRACE) " )
96+ rootCmd .Flags ().BoolVarP (& silent , "silent" , "s" , false , "Disable all output (env: PARROT_SILENT) " )
97+ rootCmd .Flags ().BoolVarP (& json , "json" , "j" , false , "Output logs in JSON format (env: PARROT_JSON) " )
98+ rootCmd .Flags ().StringSliceVarP (& recorders , "recorders" , "r" , nil , "Existing recorders to use (env: PARROT_RECORDERS) " )
7299
73100 if err := rootCmd .Execute (); err != nil {
74101 log .Error ().Err (err ).Msg ("error executing command" )
0 commit comments