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

21
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
func Add(hostname string) {
if hostname == "" {
// TODO non-red
ExitFail("Hostname required: dsnet add --hostname <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)

View File

@ -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())
}
}