owner and confirmation flag for add command

This commit is contained in:
Marvin Steadfast 2021-01-02 11:48:35 +01:00
parent b748ebd619
commit cd3f7522e0
2 changed files with 26 additions and 18 deletions

19
add.go
View File

@ -187,21 +187,20 @@ func GetWGPeerTemplate(peerConfType PeerConfType, peer *PeerConfig, conf *DsnetC
} }
// Add prompts for the required information and creates a new peer // Add prompts for the required information and creates a new peer
func Add(hostname string) { func Add(hostname, owner, description string, confirm bool) {
if hostname == "" {
// TODO non-red
ExitFail("Hostname required: dsnet add --hostname <hostname>")
}
// TODO maybe accept flags to avoid prompt and allow programmatic use?
// TODO accept existing pubkey // TODO accept existing pubkey
conf := MustLoadDsnetConfig() conf := MustLoadDsnetConfig()
owner := MustPromptString("owner", true) if owner == "" {
description := MustPromptString("Description", true) owner = MustPromptString("owner", true)
}
if description == "" {
description = MustPromptString("Description", true)
}
// publicKey := MustPromptString("PublicKey (optional)", false) // publicKey := MustPromptString("PublicKey (optional)", false)
if !confirm {
ConfirmOrAbort("\nDo you want to add the above configuration?") ConfirmOrAbort("\nDo you want to add the above configuration?")
}
// newline (not on stdout) to separate config // newline (not on stdout) to separate config
fmt.Fprintln(os.Stderr) fmt.Fprintln(os.Stderr)

View File

@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
"github.com/naggie/dsnet" "github.com/naggie/dsnet"
@ -13,6 +12,9 @@ import (
var ( var (
// Flags. // Flags.
hostname string hostname string
owner string
description string
confirm bool
// Commands. // Commands.
rootCmd = &cobra.Command{} rootCmd = &cobra.Command{}
@ -30,7 +32,7 @@ var (
addCmd = &cobra.Command{ addCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
dsnet.Add(hostname) dsnet.Add(hostname, owner, description, confirm)
}, },
Use: "add", Use: "add",
Short: "Add a new peer + sync", Short: "Add a new peer + sync",
@ -87,8 +89,15 @@ var (
func main() { func main() {
// Flags. // Flags.
rootCmd.PersistentFlags().StringP("output", "o", "wg-quick", "config file format: vyatta/wg-quick/nixos") rootCmd.PersistentFlags().String("output", "wg-quick", "config file format: vyatta/wg-quick/nixos")
addCmd.Flags().StringVarP(&hostname, "hostname", "n", "", "hostname of new peer") 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. // Environment variable handling.
viper.AutomaticEnv() viper.AutomaticEnv()
@ -96,7 +105,7 @@ func main() {
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if err := viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output")); err != nil { if err := viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output")); err != nil {
log.Fatal(err) dsnet.ExitFail(err.Error())
} }
// Adds subcommands. // Adds subcommands.
@ -110,6 +119,6 @@ func main() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
log.Fatal(err) dsnet.ExitFail(err.Error())
} }
} }