From cd3f7522e0a049f8e740405b929e0eade0209469 Mon Sep 17 00:00:00 2001 From: Marvin Steadfast Date: Sat, 2 Jan 2021 11:48:35 +0100 Subject: [PATCH] owner and confirmation flag for add command --- add.go | 21 ++++++++++----------- cmd/dsnet.go | 23 ++++++++++++++++------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/add.go b/add.go index 296ab89..8fcc136 100644 --- a/add.go +++ b/add.go @@ -187,21 +187,20 @@ func GetWGPeerTemplate(peerConfType PeerConfType, peer *PeerConfig, conf *DsnetC } // Add prompts for the required information and creates a new peer -func Add(hostname string) { - if hostname == "" { - // TODO non-red - ExitFail("Hostname required: dsnet add --hostname ") - } - - // TODO maybe accept flags to avoid prompt and allow programmatic use? +func Add(hostname, owner, description string, confirm bool) { // TODO accept existing pubkey conf := MustLoadDsnetConfig() - owner := MustPromptString("owner", true) - description := MustPromptString("Description", true) + if owner == "" { + owner = MustPromptString("owner", true) + } + if description == "" { + description = MustPromptString("Description", true) + } // publicKey := MustPromptString("PublicKey (optional)", false) - ConfirmOrAbort("\nDo you want to add the above configuration?") - + if !confirm { + ConfirmOrAbort("\nDo you want to add the above configuration?") + } // newline (not on stdout) to separate config fmt.Fprintln(os.Stderr) diff --git a/cmd/dsnet.go b/cmd/dsnet.go index 0b27fd6..a073fae 100644 --- a/cmd/dsnet.go +++ b/cmd/dsnet.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "log" "strings" "github.com/naggie/dsnet" @@ -12,7 +11,10 @@ import ( var ( // Flags. - hostname string + hostname string + owner string + description string + confirm bool // Commands. rootCmd = &cobra.Command{} @@ -30,7 +32,7 @@ var ( addCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { - dsnet.Add(hostname) + dsnet.Add(hostname, owner, description, confirm) }, Use: "add", Short: "Add a new peer + sync", @@ -87,8 +89,15 @@ var ( func main() { // Flags. - rootCmd.PersistentFlags().StringP("output", "o", "wg-quick", "config file format: vyatta/wg-quick/nixos") - addCmd.Flags().StringVarP(&hostname, "hostname", "n", "", "hostname of new peer") + rootCmd.PersistentFlags().String("output", "wg-quick", "config file format: vyatta/wg-quick/nixos") + addCmd.Flags().StringVar(&hostname, "hostname", "", "hostname of new peer") + addCmd.Flags().StringVar(&owner, "owner", "", "owner of the new peer") + addCmd.Flags().StringVar(&description, "description", "", "description of the new peer") + addCmd.Flags().BoolVar(&confirm, "confirm", false, "confirm") + + if err := addCmd.MarkFlagRequired("hostname"); err != nil { + dsnet.ExitFail(err.Error()) + } // Environment variable handling. viper.AutomaticEnv() @@ -96,7 +105,7 @@ func main() { viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) if err := viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output")); err != nil { - log.Fatal(err) + dsnet.ExitFail(err.Error()) } // Adds subcommands. @@ -110,6 +119,6 @@ func main() { rootCmd.AddCommand(versionCmd) if err := rootCmd.Execute(); err != nil { - log.Fatal(err) + dsnet.ExitFail(err.Error()) } }