Add output parameter to the docker-env none shell
Allows structured output as json and yaml, in addition to the previous text output format. Note that keys might not be sorted.pull/12263/head
parent
a85e155490
commit
4f8adb6f6d
|
@ -20,6 +20,7 @@ limitations under the License.
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
@ -33,6 +34,7 @@ import (
|
||||||
apiWait "k8s.io/apimachinery/pkg/util/wait"
|
apiWait "k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
kconst "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
kconst "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
|
@ -384,12 +386,94 @@ func dockerSetScript(ec DockerEnvConfig, w io.Writer) error {
|
||||||
dockerSetEnvTmpl = dockerEnvTCPTmpl
|
dockerSetEnvTmpl = dockerEnvTCPTmpl
|
||||||
}
|
}
|
||||||
envVars := dockerEnvVars(ec)
|
envVars := dockerEnvVars(ec)
|
||||||
|
if ec.Shell == "none" {
|
||||||
|
switch outputFormat {
|
||||||
|
case "":
|
||||||
|
// shell "none"
|
||||||
|
break
|
||||||
|
case "text":
|
||||||
|
for k, v := range envVars {
|
||||||
|
_, err := fmt.Fprintf(w, "%s=%s\n", k, v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case "json":
|
||||||
|
json, err := json.Marshal(envVars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write(json)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write([]byte{'\n'})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case "yaml":
|
||||||
|
yaml, err := yaml.Marshal(envVars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write(yaml)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
exit.Message(reason.InternalOutputUsage, "error: --output must be 'text', 'yaml' or 'json'")
|
||||||
|
}
|
||||||
|
}
|
||||||
return shell.SetScript(ec.EnvConfig, w, dockerSetEnvTmpl, dockerShellCfgSet(ec, envVars))
|
return shell.SetScript(ec.EnvConfig, w, dockerSetEnvTmpl, dockerShellCfgSet(ec, envVars))
|
||||||
}
|
}
|
||||||
|
|
||||||
// dockerSetScript writes out a shell-compatible 'docker-env unset' script
|
// dockerSetScript writes out a shell-compatible 'docker-env unset' script
|
||||||
func dockerUnsetScript(ec DockerEnvConfig, w io.Writer) error {
|
func dockerUnsetScript(ec DockerEnvConfig, w io.Writer) error {
|
||||||
vars := dockerEnvNames(ec)
|
vars := dockerEnvNames(ec)
|
||||||
|
if ec.Shell == "none" {
|
||||||
|
switch outputFormat {
|
||||||
|
case "":
|
||||||
|
// shell "none"
|
||||||
|
break
|
||||||
|
case "text":
|
||||||
|
for _, n := range vars {
|
||||||
|
_, err := fmt.Fprintf(w, "%s\n", n)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case "json":
|
||||||
|
json, err := json.Marshal(vars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write(json)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write([]byte{'\n'})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case "yaml":
|
||||||
|
yaml, err := yaml.Marshal(vars)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write(yaml)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
exit.Message(reason.InternalOutputUsage, "error: --output must be 'text', 'yaml' or 'json'")
|
||||||
|
}
|
||||||
|
}
|
||||||
return shell.UnsetScript(ec.EnvConfig, w, vars)
|
return shell.UnsetScript(ec.EnvConfig, w, vars)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -508,5 +592,6 @@ func init() {
|
||||||
dockerEnvCmd.Flags().BoolVar(&sshHost, "ssh-host", false, "Use SSH connection instead of HTTPS (port 2376)")
|
dockerEnvCmd.Flags().BoolVar(&sshHost, "ssh-host", false, "Use SSH connection instead of HTTPS (port 2376)")
|
||||||
dockerEnvCmd.Flags().BoolVar(&sshAdd, "ssh-add", false, "Add SSH identity key to SSH authentication agent")
|
dockerEnvCmd.Flags().BoolVar(&sshAdd, "ssh-add", false, "Add SSH identity key to SSH authentication agent")
|
||||||
dockerEnvCmd.Flags().StringVar(&shell.ForceShell, "shell", "", "Force environment to be configured for a specified shell: [fish, cmd, powershell, tcsh, bash, zsh], default is auto-detect")
|
dockerEnvCmd.Flags().StringVar(&shell.ForceShell, "shell", "", "Force environment to be configured for a specified shell: [fish, cmd, powershell, tcsh, bash, zsh], default is auto-detect")
|
||||||
|
dockerEnvCmd.Flags().StringVarP(&outputFormat, "output", "o", "", "One of 'text', 'yaml' or 'json'.")
|
||||||
dockerEnvCmd.Flags().BoolVarP(&dockerUnset, "unset", "u", false, "Unset variables instead of setting them")
|
dockerEnvCmd.Flags().BoolVarP(&dockerUnset, "unset", "u", false, "Unset variables instead of setting them")
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,12 @@ minikube docker-env [flags]
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
--no-proxy Add machine IP to NO_PROXY environment variable
|
--no-proxy Add machine IP to NO_PROXY environment variable
|
||||||
--shell string Force environment to be configured for a specified shell: [fish, cmd, powershell, tcsh, bash, zsh], default is auto-detect
|
-o, --output string One of 'text', 'yaml' or 'json'.
|
||||||
--ssh-add Add SSH identity key to SSH authentication agent
|
--shell string Force environment to be configured for a specified shell: [fish, cmd, powershell, tcsh, bash, zsh], default is auto-detect
|
||||||
--ssh-host Use SSH connection instead of HTTPS (port 2376)
|
--ssh-add Add SSH identity key to SSH authentication agent
|
||||||
-u, --unset Unset variables instead of setting them
|
--ssh-host Use SSH connection instead of HTTPS (port 2376)
|
||||||
|
-u, --unset Unset variables instead of setting them
|
||||||
```
|
```
|
||||||
|
|
||||||
### Options inherited from parent commands
|
### Options inherited from parent commands
|
||||||
|
|
Loading…
Reference in New Issue