Permalink
Browse files

Fix some gofmt problems and restrict codeclimate

  • Loading branch information...
FabianKramm committed Aug 24, 2018
1 parent b8699c4 commit 4b4ef91f2f4c5d3aabdd4a81671244671f270053
Showing with 43 additions and 44 deletions.
  1. +10 −10 .codeclimate.yml
  2. +14 −14 pkg/util/stdinutil/stdin.go
  3. +19 −20 pkg/util/stdinutil/stdin_test.go
View
@@ -1,25 +1,25 @@
version: "2"
checks:
argument-count:
enabled: true
enabled: false
complex-logic:
enabled: true
enabled: false
file-lines:
enabled: true
enabled: false
method-complexity:
enabled: true
enabled: false
method-count:
enabled: true
enabled: false
method-lines:
enabled: true
enabled: false
nested-control-flow:
enabled: true
enabled: false
return-statements:
enabled: true
enabled: false
similar-code:
enabled: true
enabled: false
identical-code:
enabled: true
enabled: false
plugins:
gofmt:
enabled: true
@@ -10,14 +10,14 @@ import (
"github.com/covexo/devspace/pkg/util/paramutil"
)
type GetFromStdin_params struct {
type GetFromStdinParams struct {
Question string
DefaultValue string
ValidationRegexPattern string
InputTerminationString string
}
var defaultParams = &GetFromStdin_params{
var defaultParams = &GetFromStdinParams{
ValidationRegexPattern: ".*",
InputTerminationString: "\n",
}
@@ -26,14 +26,14 @@ var reader *bufio.Reader
const changeQuestion = "Would you like to change it? (yes, no/ENTER))"
func GetFromStdin(params *GetFromStdin_params) string {
func GetFromStdin(params *GetFromStdinParams) string {
paramutil.SetDefaults(params, defaultParams)
validationRegexp, _ := regexp.Compile(params.ValidationRegexPattern)
if reader == nil {
reader = bufio.NewReader(os.Stdin)
defer func() {
reader = nil
}()
@@ -60,8 +60,8 @@ func GetFromStdin(params *GetFromStdin_params) string {
}
input += nextLine + "\n"
if strings.HasSuffix(input, params.InputTerminationString + "\n") {
input = strings.TrimSuffix(input, params.InputTerminationString + "\n")
if strings.HasSuffix(input, params.InputTerminationString+"\n") {
input = strings.TrimSuffix(input, params.InputTerminationString+"\n")
break
}
}
@@ -80,20 +80,20 @@ func GetFromStdin(params *GetFromStdin_params) string {
return input
}
func AskChangeQuestion(params *GetFromStdin_params) string{
func AskChangeQuestion(params *GetFromStdinParams) string {
paramutil.SetDefaults(params, defaultParams)
if reader == nil {
reader = bufio.NewReader(os.Stdin)
defer func() {
reader = nil
}()
}
shouldValueChangeQuestion := GetFromStdin_params{
Question: params.Question + "\nThis is the current value:\n#################\n" + strings.TrimRight(params.DefaultValue, "\r\n") + "\n#################\n" + changeQuestion,
DefaultValue: "no",
shouldValueChangeQuestion := GetFromStdinParams{
Question: params.Question + "\nThis is the current value:\n#################\n" + strings.TrimRight(params.DefaultValue, "\r\n") + "\n#################\n" + changeQuestion,
DefaultValue: "no",
ValidationRegexPattern: "yes|no",
}
@@ -103,9 +103,9 @@ func AskChangeQuestion(params *GetFromStdin_params) string{
return params.DefaultValue
}
newValueQuestion := GetFromStdin_params{
Question: "Please enter the new value:",
DefaultValue: params.DefaultValue,
newValueQuestion := GetFromStdinParams{
Question: "Please enter the new value:",
DefaultValue: params.DefaultValue,
ValidationRegexPattern: params.ValidationRegexPattern,
InputTerminationString: params.InputTerminationString,
}
@@ -9,10 +9,9 @@ import (
)
func TestGetFromStdin_NoChangeQuestion_Default(t *testing.T) {
params := GetFromStdin_params{
Question: "Is this a test?",
DefaultValue: "Yes",
Question: "Is this a test?",
DefaultValue: "Yes",
ValidationRegexPattern: "No|Yes",
}
@@ -32,8 +31,8 @@ func TestGetFromStdin_NoChangeQuestion_Default(t *testing.T) {
func TestGetFromStdin_NoChangeQuestion_NonDefault(t *testing.T) {
params := GetFromStdin_params{
Question: "Is this a test?",
DefaultValue: "No",
Question: "Is this a test?",
DefaultValue: "No",
ValidationRegexPattern: "No|Yes",
}
@@ -53,8 +52,8 @@ func TestGetFromStdin_NoChangeQuestion_NonDefault(t *testing.T) {
func TestGetFromStdin_ChangeQuestion_DontChange(t *testing.T) {
params := GetFromStdin_params{
Question: "Hello?",
DefaultValue: "World",
Question: "Hello?",
DefaultValue: "World",
ValidationRegexPattern: "World|Universe",
InputTerminationString: " ",
}
@@ -75,8 +74,8 @@ func TestGetFromStdin_ChangeQuestion_DontChange(t *testing.T) {
func TestGetFromStdin_ChangeQuestion_DoChange(t *testing.T) {
params := GetFromStdin_params{
Question: "Hello?",
DefaultValue: "World",
Question: "Hello?",
DefaultValue: "World",
ValidationRegexPattern: "World|Universe",
InputTerminationString: "!",
}
@@ -97,24 +96,24 @@ func TestGetFromStdin_ChangeQuestion_DoChange(t *testing.T) {
var tmpfile *os.File
var oldStdin *os.File
func mockStdin(inputString string) error{
func mockStdin(inputString string) error {
//Code from https://stackoverflow.com/a/46365584 (modified)
input := []byte(inputString)
var err error
tmpfile, err = ioutil.TempFile("", "testGetFromStdin")
if err != nil {
return errors.Trace(err)
}
if err != nil {
return errors.Trace(err)
}
if _, err := tmpfile.Write(input); err != nil {
return errors.Trace(err)
}
if _, err := tmpfile.Write(input); err != nil {
return errors.Trace(err)
}
if _, err := tmpfile.Seek(0, 0); err != nil {
return errors.Trace(err)
}
if _, err := tmpfile.Seek(0, 0); err != nil {
return errors.Trace(err)
}
oldStdin = os.Stdin
oldStdin = os.Stdin
os.Stdin = tmpfile
return nil

0 comments on commit 4b4ef91

Please sign in to comment.