Skip to content

Commit

Permalink
added check TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
psihachina committed Oct 7, 2022
1 parent 9153744 commit f1c767f
Showing 1 changed file with 44 additions and 33 deletions.
77 changes: 44 additions & 33 deletions internal/commands/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package commands

import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/hazelops/ize/internal/schema"
"github.com/hazelops/ize/internal/version"
"golang.org/x/sys/unix"
"os"
"path/filepath"
"sort"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/hazelops/ize/examples"
"github.com/hazelops/ize/internal/generate"
"github.com/hazelops/ize/pkg/templates"
Expand Down Expand Up @@ -102,6 +103,11 @@ func (o *InitOptions) Validate() error {
}

func (o *InitOptions) Run() error {
isTTY := true
_, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
if err != nil {
isTTY = false
}
if len(o.Template) != 0 {
dest, err := generate.GenerateFiles(o.Template, o.Output)
if err != nil {
Expand Down Expand Up @@ -129,57 +135,62 @@ func (o *InitOptions) Run() error {
}
}

dir, err := filepath.Abs(o.Output)
dir, err = filepath.Abs(o.Output)
if err != nil {
return fmt.Errorf("can't init: %w", err)
}

namespace = filepath.Base(dir)
err = survey.AskOne(
&survey.Input{
Message: fmt.Sprintf("Namespace:"),
Default: namespace,
},
&namespace,
survey.WithValidator(survey.Required),
)
if err != nil {
return fmt.Errorf("can't init: %w", err)
}

err = survey.AskOne(
&survey.Input{
Message: fmt.Sprintf("Environment:"),
Default: env,
},
&env,
survey.WithValidator(survey.Required),
)
if err != nil {
return fmt.Errorf("can't init: %w", err)
}

envList = append(envList, env)
env = ""
if isTTY {
err = survey.AskOne(
&survey.Input{
Message: fmt.Sprintf("Namespace:"),
Default: namespace,
},
&namespace,
survey.WithValidator(survey.Required),
)
if err != nil {
return fmt.Errorf("can't init: %w", err)
}

for {
err = survey.AskOne(
&survey.Input{
Message: fmt.Sprintf("Another environment? [enter - skip]"),
Message: fmt.Sprintf("Environment:"),
Default: env,
},
&env,
survey.WithValidator(survey.Required),
)
if err != nil {
return fmt.Errorf("can't init: %w", err)
}

if env == "" {
break
}

envList = append(envList, env)
env = ""

for {
err = survey.AskOne(
&survey.Input{
Message: fmt.Sprintf("Another environment? [enter - skip]"),
Default: env,
},
&env,
)
if err != nil {
return fmt.Errorf("can't init: %w", err)
}

if env == "" {
break
}

envList = append(envList, env)
env = ""
}
} else {
envList = append(envList, env)
}

for _, v := range envList {
Expand Down

0 comments on commit f1c767f

Please sign in to comment.