Skip to content

Commit 0d882b8

Browse files
Merge pull request #16525 from smarterclayton/extract_go
Automatic merge from submit-queue (batch tested with PRs 16525, 16602, 16603). Allow oc extract to output to stdout
2 parents 16a400f + 1d215d7 commit 0d882b8

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

pkg/oc/cli/cmd/extract.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var (
2828
Each key in the config map or secret is created as a separate file with the name of the key, as it
2929
is when you mount a secret or config map into a container.
3030
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+
3134
You can limit which keys are extracted with the --keys=NAME flag, or set the directory to extract to
3235
with --to=DIRECTORY.`)
3336

@@ -38,6 +41,9 @@ var (
3841
# extract the config map "nginx" to the /tmp directory
3942
%[1]s extract configmap/nginx --to=/tmp
4043
44+
# extract the config map "nginx" to STDOUT
45+
%[1]s extract configmap/nginx --to=-
46+
4147
# extract only the key "nginx.conf" from config map "nginx" to the /tmp directory
4248
%[1]s extract configmap/nginx --to=/tmp --keys=nginx.conf`)
4349
)
@@ -105,9 +111,11 @@ func (o *ExtractOptions) Complete(f *clientcmd.Factory, in io.Reader, out io.Wri
105111
}
106112

107113
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+
}
111119
}
112120
return nil
113121
}
@@ -135,12 +143,21 @@ func (o *ExtractOptions) Run() error {
135143
var errs []error
136144
for k, v := range contents {
137145
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))
142160
}
143-
errs = append(errs, fmt.Errorf("%s: %v", k, err))
144161
}
145162
}
146163
}

test/cmd/secrets.sh

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ os::cmd::expect_failure_and_text 'oc secrets new bad-name .docker=cfg=${HOME}/do
3131
workingdir="$( mktemp -d )"
3232
os::cmd::try_until_success "oc get secret/dockercfg"
3333
os::cmd::expect_success_and_text "oc extract secret/dockercfg --to '${workingdir}'" '.dockercfg'
34+
os::cmd::expect_success_and_text "oc extract secret/dockercfg --to=-" 'sample-user'
35+
os::cmd::expect_success_and_text "oc extract secret/dockercfg --to=-" 'sample-password'
3436
os::cmd::expect_success_and_text "cat '${workingdir}/.dockercfg'" 'sample-user'
3537
os::cmd::expect_failure_and_text "oc extract secret/dockercfg --to '${workingdir}'" 'error: .dockercfg: file exists, pass --confirm to overwrite'
3638
os::cmd::expect_failure_and_text "oc extract secret/dockercfg secret/dockercfg --to '${workingdir}'" 'error: .dockercfg: file exists, pass --confirm to overwrite'

0 commit comments

Comments
 (0)