From b748ebd619fa16079eadc68e8afa3bf79ca227b2 Mon Sep 17 00:00:00 2001 From: Marvin Steadfast Date: Thu, 31 Dec 2020 10:46:18 +0100 Subject: [PATCH] `Add` function takes hostname this allows the use of cobra flags to configure the hostname. --- add.go | 7 +++---- cmd/dsnet.go | 10 +++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/add.go b/add.go index 2e1c49c..296ab89 100644 --- a/add.go +++ b/add.go @@ -187,17 +187,16 @@ func GetWGPeerTemplate(peerConfType PeerConfType, peer *PeerConfig, conf *DsnetC } // Add prompts for the required information and creates a new peer -func Add() { - if len(os.Args) != 3 { +func Add(hostname string) { + if hostname == "" { // TODO non-red - ExitFail("Hostname argument required: dsnet add ") + ExitFail("Hostname required: dsnet add --hostname ") } // TODO maybe accept flags to avoid prompt and allow programmatic use? // TODO accept existing pubkey conf := MustLoadDsnetConfig() - hostname := os.Args[2] owner := MustPromptString("owner", true) description := MustPromptString("Description", true) // publicKey := MustPromptString("PublicKey (optional)", false) diff --git a/cmd/dsnet.go b/cmd/dsnet.go index e1b1651..0b27fd6 100644 --- a/cmd/dsnet.go +++ b/cmd/dsnet.go @@ -12,7 +12,7 @@ import ( var ( // Flags. - outputType string + hostname string // Commands. rootCmd = &cobra.Command{} @@ -30,7 +30,7 @@ var ( addCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { - dsnet.Add() + dsnet.Add(hostname) }, Use: "add", Short: "Add a new peer + sync", @@ -86,8 +86,11 @@ var ( ) func main() { - rootCmd.PersistentFlags().String("output", "wg-quick", "config file format: vyatta/wg-quick/nixos") + // 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") + // Environment variable handling. viper.AutomaticEnv() viper.SetEnvPrefix("DSNET") viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) @@ -96,6 +99,7 @@ func main() { log.Fatal(err) } + // Adds subcommands. rootCmd.AddCommand(initCmd) rootCmd.AddCommand(addCmd) rootCmd.AddCommand(upCmd)