Skip to content

Commit

Permalink
feat: add config map (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
wingkwong committed Mar 8, 2020
1 parent 2a40ef5 commit a26fcd6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 18 deletions.
7 changes: 6 additions & 1 deletion internal/pkg/cli/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
inputGlobalDefaultPrompt = "Do you want this PriorityClass to be considered as the default priority?"
inputPreemptionPolicyPrompt = "What preemption-policy you want to have?"
inputNamespacePrompt = "What namespace you want to name?"
inputConfigMapNamePrompt = "What config map you want to name?"
inputAppenHashPrompt = "Do you want to append a hash to its name?"
// select
inputOutputFormatPrompt = "Please select an output format:"
inputSecretCmdNamePrompt = "Please select the type of secret:"
Expand Down Expand Up @@ -80,13 +82,16 @@ var questions = map[string]Question{
"Value": {"Value", "int", inputValuePrompt, "", "Prompt for value", nil /*no validation*/, nil, "AskGet"},
"Description": {"Description", "string", inputDescriptionPrompt, "", "Prompt for description", nil /*no validation*/, nil, "AskGet"},
"GlobalDefault": {"GlobalDefault", "bool", inputGlobalDefaultPrompt, "", "Prompt for global default", nil /*no validation*/, yesOrNo, "AskSelect"},
"AppendHash": {"AppendHash", "bool", inputAppenHashPrompt, "", "Prompt for append hash", nil /*no validation*/, yesOrNo, "AskSelect"},
"PreemptionPolicy": {"PreemptionPolicy", "string", inputPreemptionPolicyPrompt, "", "Prompt for preemption policy", nil /*no validation*/, nil, "AskGet"},
"NamespaceName": {"NamespaceName", "string", inputNamespacePrompt, "", "Prompt for namespace", nil /*no validation*/, nil, "AskGet"},
"ConfigMapName": {"ConfigMapName", "string", inputConfigMapNamePrompt, "", "Prompt for config map", nil /*no validation*/, nil, "AskGet"},
// Iteration
"FromFileIteration": {"Iterator", "int", inputFromFileIterationPrompt, "", "Prompt for from-file iteration", nil /*no validation*/, nil, "AskGet"},
"FromLiteralIteration": {"Iterator", "int", inputFromLiteralIterationPrompt, "", "Prompt for from-literal iteration", nil /*no validation*/, nil, "AskGet"},
//Array
"FromFile": {"FromFile", "array", inputFromFilePrompt, "", "Prompt for from-file", nil /*no validation*/, nil, "AskGet"},
"FromFile": {"FromFile", "array", inputFromFilePrompt, "", "Prompt for from-file", nil /*no validation*/, nil, "AskGet"},
"FromLiteral": {"FromLiteral", "array", inputFromLiteralPrompt, "", "Prompt for from-literal", nil /*no validation*/, nil, "AskGet"},
}

var yesOrNo = []string{"Yes", "No"}
Expand Down
68 changes: 65 additions & 3 deletions internal/pkg/cli/jumpstart_configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,75 @@ import (
)

func (o *askOpts) AskConfigMapCmdOpts() error {
// TODO:
if err := o.Ask("ConfigMapName"); err != nil {
return err
}

if err := o.Ask("AppendHash"); err != nil {
return err
}

if err := o.AskWithIterator("FromFile"); err != nil {
return err
}

if err := o.AskWithIterator("FromLiteral"); err != nil {
return err
}

// from-env-file cannot be combined with from-file or from-literal
if len(o.FromLiteral) == 0 && len(o.FromFile) == 0 {
if err := o.Ask("FromEnvFile"); err != nil {
return err
}
}

if err := o.AskOutputInfo(); err != nil {
return err
}

return nil
}

func (o *askOpts) ExecuteConfigMapCmd() error {
// TODO:
cmd := ""
var cmd string

// Example:

// # Create a new configmap named my-config based on folder bar
// kubectl create configmap my-config --from-file=path/to/bar

// # Create a new configmap named my-config with specified keys instead of file basenames on disk
// kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

// # Create a new configmap named my-config with key1=config1 and key2=config2
// kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

// # Create a new configmap named my-config from the key=value pairs in the file
// kubectl create configmap my-config --from-file=path/to/bar

// # Create a new configmap named my-config from an env file
// kubectl create configmap my-config --from-env-file=path/to/bar.env

cmd = fmt.Sprintf("kubectl create configmap %s ", o.ConfigMapName)

for i := 0; i < len(o.FromFile); i++ {
cmd = cmd + fmt.Sprintf("--from-file=%s ", o.FromFile[i])
}

for i := 0; i < len(o.FromLiteral); i++ {
cmd = cmd + fmt.Sprintf("--from-literal=%s ", o.FromLiteral[i])
}

if len(o.FromLiteral) == 0 && len(o.FromFile) == 0 && o.FromEnvFile != "" {
cmd = cmd + fmt.Sprintf("--from-env-file=%s ", o.FromEnvFile)
}

if o.AppendHash {
cmd = cmd + fmt.Sprintf("--append-hash=%t ", o.AppendHash)
}

cmd = cmd + fmt.Sprintf("--output=%s > %s", o.OutputFormat, o.OutputPath)

if err := ExecCmd(cmd); err != nil {
return fmt.Errorf("Failed To execute command `%s` \n %w", cmd, err)
Expand Down
38 changes: 24 additions & 14 deletions internal/pkg/cli/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type askVars struct {
QuotaCmdOpts
PriorityClassCmtOpts
NamespaceCmtOpts
ConfigMapCmtOpts
FileOpts
}

type askOpts struct {
Expand Down Expand Up @@ -53,24 +55,10 @@ type SecretCmdOpts struct {
DockerUserPassword string
// Email for Docker registry
DockerEmail string
// Key files can be specified using their file path, in which case a default name will be given to them
// or optionally with a name and file path, in which case the given name will be used
// Specifying a directory will iterate each named file in the directory that is a valid secret key
FromFile []string
// Append a hash of the secret to its name
AppendHash bool
// Secret cert path
CertPath string
// Secret key path
KeyPath string
// Secret from Literal input
FromLiteral []string
// Secret from environment file
FromEnvFile string
// Number of iteration for the same question for fromFile
FromFileIteration int
// Number of iteration for the same question for fromLiteral
FromLiteralIteration int
}

type RoleCmdOpts struct {
Expand Down Expand Up @@ -110,3 +98,25 @@ type NamespaceCmtOpts struct {
// Name Of Namespace
NamespaceName string
}

type ConfigMapCmtOpts struct {
// Name Of Config Map
ConfigMapName string
}

type FileOpts struct {
// Key files can be specified using their file path, in which case a default name will be given to them
// or optionally with a name and file path, in which case the given name will be used
// Specifying a directory will iterate each named file in the directory that is a valid secret key
FromFile []string
// Specify a key and literal value to insert (i.e. mykey=somevalue)
FromLiteral []string
// Specify the path to a file to read lines of key=val pairs (i.e. a Docker .env file).
FromEnvFile string
// Number of iteration for the same question for fromFile
FromFileIteration int
// Number of iteration for the same question for fromLiteral
FromLiteralIteration int
// Append a hash to its name
AppendHash bool
}

0 comments on commit a26fcd6

Please sign in to comment.