Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
ASR-195: add campaign confirmation to cli (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Weems authored Oct 6, 2020
1 parent 2a489b5 commit 3706b6d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pkg/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -49,6 +51,20 @@ var (
flagProvider string
)

const (
campaignSummary = `
[Campaign Summary]
Not Before: %s
Not After: %s
Interval: %s
Username count: %d
Password count: %d
Provider: %s
Metadata: %v
`
)

var campaignCreateCmd = &cobra.Command{
Use: "create",
Short: "campaign management subcommand",
Expand Down Expand Up @@ -117,6 +133,22 @@ func readLines(path string) ([]string, error) {
return lines, scanner.Err()
}

func confirm(s string) bool {
fmt.Printf("%s [y/N]: ", s)

reader := bufio.NewReader(os.Stdin)
r, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}

r = strings.ToLower(strings.TrimSpace(r))
if r == "y" || r == "yes" {
return true
}
return false
}

func campaignCreate(cmd *cobra.Command, args []string) {
orchestrator := viper.GetString("orchestrator-url")
providers := viper.GetStringMap("providers")
Expand Down Expand Up @@ -152,6 +184,14 @@ func campaignCreate(cmd *cobra.Command, args []string) {
log.Fatalf("error during JSON marshalling for request body: %s", err)
}

// print summary of campaign and prompt user to accept
fmt.Printf(campaignSummary, parsedNotBefore, parsedNotAfter, flagScheduleInterval,
len(users), len(passwords), flagProvider, providers[flagProvider])
if !confirm("Send campaign?") {
log.Printf("not sending campaign")
return
}

req, err := http.NewRequest("POST", orchestrator+"/campaign", bytes.NewBuffer(requestBody))
if err != nil {
log.Fatalf("error during request creation: %s", err)
Expand Down

0 comments on commit 3706b6d

Please # to comment.