|
28 | 28 | Each key in the config map or secret is created as a separate file with the name of the key, as it
|
29 | 29 | is when you mount a secret or config map into a container.
|
30 | 30 |
|
| 31 | + You may extract the contents of a secret or config map to standard out by passing '-' to --to. The |
| 32 | + names of each key will be written to stdandard error. |
| 33 | +
|
31 | 34 | You can limit which keys are extracted with the --keys=NAME flag, or set the directory to extract to
|
32 | 35 | with --to=DIRECTORY.`)
|
33 | 36 |
|
|
38 | 41 | # extract the config map "nginx" to the /tmp directory
|
39 | 42 | %[1]s extract configmap/nginx --to=/tmp
|
40 | 43 |
|
| 44 | + # extract the config map "nginx" to STDOUT |
| 45 | + %[1]s extract configmap/nginx --to=- |
| 46 | +
|
41 | 47 | # extract only the key "nginx.conf" from config map "nginx" to the /tmp directory
|
42 | 48 | %[1]s extract configmap/nginx --to=/tmp --keys=nginx.conf`)
|
43 | 49 | )
|
@@ -105,9 +111,11 @@ func (o *ExtractOptions) Complete(f *clientcmd.Factory, in io.Reader, out io.Wri
|
105 | 111 | }
|
106 | 112 |
|
107 | 113 | func (o *ExtractOptions) Validate() error {
|
108 |
| - // determine if output location is valid before continuing |
109 |
| - if _, err := os.Stat(o.TargetDirectory); err != nil { |
110 |
| - return err |
| 114 | + if o.TargetDirectory != "-" { |
| 115 | + // determine if output location is valid before continuing |
| 116 | + if _, err := os.Stat(o.TargetDirectory); err != nil { |
| 117 | + return err |
| 118 | + } |
111 | 119 | }
|
112 | 120 | return nil
|
113 | 121 | }
|
@@ -135,12 +143,21 @@ func (o *ExtractOptions) Run() error {
|
135 | 143 | var errs []error
|
136 | 144 | for k, v := range contents {
|
137 | 145 | if contains.Len() == 0 || contains.Has(k) {
|
138 |
| - target := filepath.Join(o.TargetDirectory, k) |
139 |
| - if err := writeToDisk(target, v, o.Overwrite, o.Out); err != nil { |
140 |
| - if os.IsExist(err) { |
141 |
| - err = fmt.Errorf("file exists, pass --confirm to overwrite") |
| 146 | + switch { |
| 147 | + case o.TargetDirectory == "-": |
| 148 | + fmt.Fprintf(o.Err, "# %s\n", k) |
| 149 | + o.Out.Write(v) |
| 150 | + if !bytes.HasSuffix(v, []byte("\n")) { |
| 151 | + fmt.Fprintln(o.Out) |
| 152 | + } |
| 153 | + default: |
| 154 | + target := filepath.Join(o.TargetDirectory, k) |
| 155 | + if err := writeToDisk(target, v, o.Overwrite, o.Out); err != nil { |
| 156 | + if os.IsExist(err) { |
| 157 | + err = fmt.Errorf("file exists, pass --confirm to overwrite") |
| 158 | + } |
| 159 | + errs = append(errs, fmt.Errorf("%s: %v", k, err)) |
142 | 160 | }
|
143 |
| - errs = append(errs, fmt.Errorf("%s: %v", k, err)) |
144 | 161 | }
|
145 | 162 | }
|
146 | 163 | }
|
|
0 commit comments