Skip to content

Commit

Permalink
chore, feat: cleanup in runner code, pass flags to sender ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mellonnen committed Feb 22, 2023
1 parent eb252de commit 8deae65
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
33 changes: 17 additions & 16 deletions cmd/portal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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()
Expand Down
30 changes: 15 additions & 15 deletions cmd/portal/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ 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",
Short: "Receive files",
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)
Expand All @@ -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)
Expand Down
39 changes: 22 additions & 17 deletions cmd/portal/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 8deae65

Please sign in to comment.