Skip to content

Commit

Permalink
can detect upated
Browse files Browse the repository at this point in the history
  • Loading branch information
enekofb committed Nov 10, 2023
1 parent f5afd3f commit 101b138
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 26 deletions.
27 changes: 23 additions & 4 deletions pkg/bootstrap/steps/admin_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,28 @@ var getPasswordInput = StepInput{
Required: true,
}

var getPasswordInput2 = StepInput{
var getPasswordWithDefaultInput = StepInput{
Name: inPassword,
Type: passwordInput,
Msg: adminPasswordMsg,
DefaultValue: defaultAdminPassword,
}

var getPasswordWithExisting = StepInput{

Check failure on line 43 in pkg/bootstrap/steps/admin_password.go

View workflow job for this annotation

GitHub Actions / lint

var `getPasswordWithExisting` is unused (unused)
Name: inPassword,
Type: passwordInput,
Msg: adminPasswordMsg,
DefaultValue: defaultAdminPassword,
}

var getPasswordWithExistingAndUserInput = StepInput{
Name: inPassword,
Type: passwordInput,
Msg: adminPasswordMsg,
DefaultValue: defaultAdminPassword,
AlreadyExist: true,
}

type ClusterUserAuthConfig struct {
Username string
Password string
Expand All @@ -63,15 +78,19 @@ func NewClusterUserAuthConfig(password string, client k8s_client.Client) (Cluste
// Users will be asked to continue with the current creds or overriding existing credentials during bootstrapping.
func NewAskAdminCredsSecretStep(config ClusterUserAuthConfig, silent bool) (BootstrapStep, error) {
inputs := []StepInput{}

// TODO: refactor as this is general ... if not silent ask the user whether to create or update
if !silent {
// current state layer
if !config.ExistCredentials {
// insert
if config.Password == "" {
inputs = append(inputs, getPasswordInput2)
inputs = append(inputs, getPasswordWithDefaultInput)
}
} else {
// update
inputs = append(inputs, getPasswordWithExistingAndUserInput)
}
}

return BootstrapStep{
Name: "user authentication",
Input: inputs,
Expand Down
69 changes: 60 additions & 9 deletions pkg/bootstrap/steps/admin_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,35 +169,86 @@ func TestNewAskAdminCredsSecretStep(t *testing.T) {
wantErr assert.ErrorAssertionFunc
}{
{
name: "day0/1 interactive - ask suggest default - <no existing, no values, no silent>",
name: "day0/1 interactive - ask suggest default - <no silent, no existing, no values >",
silent: false,
config: ClusterUserAuthConfig{
ExistCredentials: false,
},
silent: false,
want: BootstrapStep{
Name: "user authentication",
Input: []StepInput{
getPasswordInput2,
getPasswordWithDefaultInput,
},
},
},
{
name: "day1 no-interactive - no ask use input - <no existing, values, silent>",
name: "day1 interactive - no ask use input - <no silent, no existing, values>",
silent: false,
config: ClusterUserAuthConfig{
ExistCredentials: false,
Password: "password123",
},
want: BootstrapStep{
Name: "user authentication",
Input: []StepInput{},
},
},
{
name: "day1 no-interactive - no ask use existing - <existing, no values, silent>",
name: "day1 interactive - ask suggest previous value - <no silent, existing, no values>",
silent: false,
config: ClusterUserAuthConfig{
ExistCredentials: true,
},
want: BootstrapStep{
Name: "user authentication",
Input: []StepInput{getPasswordWithExistingAndUserInput},
},
},
{
name: "day1 no-interactive - overwrite - <existing, values, silent>",
name: "day1 interactive - ask conflict - <no silent, existing, values>",
silent: false,
config: ClusterUserAuthConfig{
ExistCredentials: true,
Password: "password123",
},
want: BootstrapStep{
Name: "user authentication",
Input: []StepInput{getPasswordWithExistingAndUserInput},
},
},
{
name: "day1 interactive - ask suggest previous value - <existing, no values, no silent>",
name: "day1 no-interactive - no ask use input - <silent, no existing, values>",
silent: true,
config: ClusterUserAuthConfig{
ExistCredentials: false,
Password: "password123",
},
want: BootstrapStep{
Name: "user authentication",
Input: []StepInput{},
},
},
{
name: "day1 interactive - ask conflict - <existing, values, no silent>",
name: "day1 no-interactive - no ask use existing - <silent, existing, no values>",
silent: true,
config: ClusterUserAuthConfig{
ExistCredentials: true,
},
want: BootstrapStep{
Name: "user authentication",
Input: []StepInput{},
},
},
{
name: "day1 interactive - no ask use input - <no existing, values, no silent>",
name: "day1 no-interactive - overwrite - <silent, existing, values>",
silent: true,
config: ClusterUserAuthConfig{
ExistCredentials: true,
},
want: BootstrapStep{
Name: "user authentication",
Input: []StepInput{},
},
},
}
for _, tt := range tests {
Expand Down
43 changes: 31 additions & 12 deletions pkg/bootstrap/steps/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package steps

import (
"fmt"
"io"
"os"

"github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/utils"
v1 "k8s.io/api/core/v1"
Expand All @@ -24,12 +26,17 @@ type StepInput struct {
Msg string
StepInformation string
Type string
DefaultValue any
Value any
Values []string
Valuesfn func(input []StepInput, c *Config) (interface{}, error)
Enabled func(input []StepInput, c *Config) bool
Required bool

Value any
Values []string
Valuesfn func(input []StepInput, c *Config) (interface{}, error)

Enabled func(input []StepInput, c *Config) bool
Required bool

// indicate whether the value already exists so you could reuse it
AlreadyExist bool
DefaultValue any
}

// StepOutput represents an output generated out of the execution of a step.
Expand Down Expand Up @@ -58,16 +65,27 @@ func (s BootstrapStep) Execute(c *Config) error {
return nil
}

// isUpdate returns true if the input exists and the user wants to update it
// return false otherwise (does not exist or user want to use the existing value)
func isUpdate(input StepInput, stdin io.ReadCloser) bool {
if input.AlreadyExist {
return (utils.GetConfirmInput(input.Msg, stdin) == "y")
}
return false
}

func defaultInputStep(inputs []StepInput, c *Config) ([]StepInput, error) {
processedInputs := []StepInput{}
for _, input := range inputs {

if !isUpdate(input, os.Stdin) {
fmt.Println("will use existing value", input.Name)
processedInputs = append(processedInputs, input)
continue
}

switch input.Type {
case stringInput:
// verify the input is enabled by executing the function
if input.Enabled != nil && !input.Enabled(nil, c) {
continue
}

if input.StepInformation != "" {
c.Logger.Warningf(input.StepInformation)
}
Expand All @@ -80,6 +98,7 @@ func defaultInputStep(inputs []StepInput, c *Config) ([]StepInput, error) {
}
input.Value = paramValue
}

// fill the new inputs
processedInputs = append(processedInputs, input)
case passwordInput:
Expand Down Expand Up @@ -117,7 +136,7 @@ func defaultInputStep(inputs []StepInput, c *Config) ([]StepInput, error) {

// get the value from user otherwise
if input.Value == nil {
input.Value = utils.GetConfirmInput(input.Msg)
input.Value = utils.GetConfirmInput(input.Msg, os.Stdin)
}
processedInputs = append(processedInputs, input)
case multiSelectionChoice:
Expand Down
61 changes: 61 additions & 0 deletions pkg/bootstrap/steps/step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package steps

import (
"bytes"
"fmt"
"io"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_isUpdate(t *testing.T) {
tests := []struct {
name string
input StepInput
in string
want bool
}{
{
name: "test input with no existing value",
input: StepInput{
Name: "idontexist",
AlreadyExist: false,
},
want: false,
},
{
name: "test input with existing value and want to update",
input: StepInput{
Name: "iexist",
AlreadyExist: true,
},
in: "y",
want: true,
},
{
name: "test input with existing value and dont want to update",
input: StepInput{
Name: "iexist",
AlreadyExist: true,
},
in: "n",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a buffer to store the input string.
var buf bytes.Buffer

// Create a new io.ReaderCloser using the buffer.
reader := io.NopCloser(&buf)

// Write the input string to the buffer.
buf.WriteString(fmt.Sprintf("%s\n", tt.in))

update := isUpdate(tt.input, reader)
assert.Equal(t, tt.want, update)
})
}
}
5 changes: 4 additions & 1 deletion pkg/bootstrap/utils/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"errors"
"io"
"strings"

"github.com/manifoldco/promptui"
Expand Down Expand Up @@ -85,10 +86,12 @@ func GetStringInput(msg string, defaultValue string) (string, error) {
}

// GetConfirmInput prompt to get yes or no input.
func GetConfirmInput(msg string) string {
func GetConfirmInput(msg string, stdin io.ReadCloser) string {

prompt := promptui.Prompt{
Label: msg,
IsConfirm: true,
Stdin: stdin,
}

result, err := prompt.Run()
Expand Down

0 comments on commit 101b138

Please sign in to comment.