From 8deae65d730f515b2fe3a94499d24b152fb65d2c Mon Sep 17 00:00:00 2001 From: mellonnen Date: Wed, 22 Feb 2023 22:53:00 +0100 Subject: [PATCH] chore, feat: cleanup in runner code, pass flags to sender ui --- cmd/portal/main.go | 33 +++++++++++++++++---------------- cmd/portal/receive.go | 30 +++++++++++++++--------------- cmd/portal/send.go | 39 ++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/cmd/portal/main.go b/cmd/portal/main.go index 7aa0043..95b2538 100644 --- a/cmd/portal/main.go +++ b/cmd/portal/main.go @@ -20,6 +20,21 @@ import ( // injected at link time using -ldflags. var version string +// Initialization of cobra and viper. +func init() { + cobra.OnInitialize(initViperConfig) + + rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Specifes if portal logs debug information to a file on the format `.portal-[command].log` in the current directory") + // Setup viper config. + // Add cobra subcommands. + rootCmd.AddCommand(sendCmd) + rootCmd.AddCommand(receiveCmd) + rootCmd.AddCommand(serveCmd) + rootCmd.AddCommand(versionCmd) +} + +// ------------------------------------------------------ Command ------------------------------------------------------ + // rootCmd is the top level `portal` command on which the other subcommands are attached to. var rootCmd = &cobra.Command{ Use: "portal", @@ -45,20 +60,7 @@ func main() { } } -// Initialization of cobra and viper. -func init() { - cobra.OnInitialize(initViperConfig) - - rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Specifes if portal logs debug information to a file on the format `.portal-[command].log` in the current directory") - // Setup viper config. - // Add cobra subcommands. - rootCmd.AddCommand(sendCmd) - rootCmd.AddCommand(receiveCmd) - rootCmd.AddCommand(serveCmd) - rootCmd.AddCommand(versionCmd) -} - -// HELPER FUNCTIONS +// -------------------------------------------------- Helper Functions ------------------------------------------------- // initViperConfig initializes the viper config. // It creates a `.portal.yml` file at the home directory if it has not been created earlier @@ -67,8 +69,7 @@ func init() { func initViperConfig() { // Set default values viper.SetDefault("verbose", false) - viper.SetDefault("rendezvousPort", DEFAULT_RENDEZVOUS_PORT) - viper.SetDefault("rendezvousAddress", DEFAULT_RENDEZVOUS_ADDRESS) + viper.SetDefault("relay", fmt.Sprintf("%s:%d", DEFAULT_RENDEZVOUS_ADDRESS, DEFAULT_RENDEZVOUS_PORT)) // Find home directory. home, err := homedir.Dir() diff --git a/cmd/portal/receive.go b/cmd/portal/receive.go index b39d203..5694b73 100644 --- a/cmd/portal/receive.go +++ b/cmd/portal/receive.go @@ -16,6 +16,14 @@ import ( "golang.org/x/exp/slices" ) +// Setup flags. +func init() { + // Add subcommand flags (dummy default values as default values are handled through viper). + receiveCmd.Flags().StringP("relay", "r", "", "address of the relay server") +} + +// ------------------------------------------------------ Command ------------------------------------------------------ + // receiveCmd is the cobra command for `portal receive` var receiveCmd = &cobra.Command{ Use: "receive", @@ -23,12 +31,12 @@ var receiveCmd = &cobra.Command{ Long: "The receive command receives files from the sender with the matching password.", Args: cobra.ExactArgs(1), ValidArgsFunction: passwordCompletion, - PreRun: func(cmd *cobra.Command, args []string) { + PreRunE: func(cmd *cobra.Command, args []string) error { // Bind flags to viper - //nolint - viper.BindPFlag("rendezvousPort", cmd.Flags().Lookup("rendezvous-port")) - //nolint - viper.BindPFlag("rendezvousAddress", cmd.Flags().Lookup("rendezvous-address")) + if err := viper.BindPFlag("relay", cmd.Flags().Lookup("relay")); err != nil { + return fmt.Errorf("binding relay flag: %w", err) + } + return nil }, RunE: func(cmd *cobra.Command, args []string) error { file.RemoveTemporaryFiles(file.RECEIVE_TEMP_FILE_NAME_PREFIX) @@ -50,24 +58,16 @@ var receiveCmd = &cobra.Command{ }, } -// Setup flags. -func init() { - // Add subcommand flags (dummy default values as default values are handled through viper). - // TODO: recactor this into a single flag for providing a TCPAddr. - receiveCmd.Flags().IntP("rendezvous-port", "p", 0, "port on which the rendezvous server is running") - receiveCmd.Flags().StringP("rendezvous-address", "a", "", "host address for the rendezvous server") -} +// ------------------------------------------------------ Handler ------------------------------------------------------ // handleReceiveCommand is the receive application. func handleReceiveCommand(password string) { - addr := viper.GetString("rendezvousAddress") - port := viper.GetInt("rendezvousPort") var opts []receiver.Option ver, err := semver.Parse(version) if err == nil { opts = append(opts, receiver.WithVersion(ver)) } - receiver := receiver.New(fmt.Sprintf("%s:%d", addr, port), password, opts...) + receiver := receiver.New(viper.GetString("relay"), password, opts...) if _, err := receiver.Run(); err != nil { fmt.Println("Error initializing UI", err) diff --git a/cmd/portal/send.go b/cmd/portal/send.go index 277f561..83d1bbb 100644 --- a/cmd/portal/send.go +++ b/cmd/portal/send.go @@ -12,18 +12,27 @@ import ( "github.com/spf13/viper" ) +// Set flags. +func init() { + // Add subcommand flags (dummy default values as default values are handled through viper) + sendCmd.Flags().StringP("relay", "r", "", "address of the relay server") +} + +// ------------------------------------------------------ Command ------------------------------------------------------ + // sendCmd cobra command for `portal send`. var sendCmd = &cobra.Command{ Use: "send", Short: "Send one or more files", Long: "The send command adds one or more files to be sent. Files are archived and compressed before sending.", Args: cobra.MinimumNArgs(1), - PreRun: func(cmd *cobra.Command, args []string) { + PreRunE: func(cmd *cobra.Command, args []string) error { // Bind flags to viper - //nolint:errcheck - viper.BindPFlag("rendezvousPort", cmd.Flags().Lookup("rendezvous-port")) - //nolint:errcheck - viper.BindPFlag("rendezvousAddress", cmd.Flags().Lookup("rendezvous-address")) + if err := viper.BindPFlag("relay", cmd.Flags().Lookup("relay")); err != nil { + return fmt.Errorf("binding relay flag: %w", err) + } + return nil + }, RunE: func(cmd *cobra.Command, args []string) error { if err := sender.Init(); err != nil { @@ -40,30 +49,26 @@ var sendCmd = &cobra.Command{ } defer logFile.Close() - handleSendCommand(args) + handleSendCommand(args, cmd.Flag("relay").Changed) return nil }, } -// Set flags. -func init() { - // Add subcommand flags (dummy default values as default values are handled through viper) - //TODO: refactor into a single flag providing a string - sendCmd.Flags().IntP("rendezvous-port", "p", 0, "port on which the rendezvous server is running") - sendCmd.Flags().StringP("rendezvous-address", "a", "", "host address for the rendezvous server") -} +// ------------------------------------------------------ Handler ------------------------------------------------------ // handleSendCommand is the sender application. -func handleSendCommand(fileNames []string) { - addr := viper.GetString("rendezvousAddress") - port := viper.GetInt("rendezvousPort") +func handleSendCommand(fileNames []string, selfHostedRelay bool) { var opts []senderui.Option ver, err := semver.Parse(version) // Conditionally add option to sender ui if err == nil { opts = append(opts, senderui.WithVersion(ver)) } - sender := senderui.New(fileNames, fmt.Sprintf("%s:%d", addr, port), opts...) + relayAddr := viper.GetString("relay") + if selfHostedRelay { + opts = append(opts, senderui.WithCopyFlags(map[string]string{"--relay": relayAddr})) + } + sender := senderui.New(fileNames, relayAddr, opts...) if _, err := sender.Run(); err != nil { fmt.Println("Error initializing UI", err) os.Exit(1)