2015-09-04 23:33:11 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2016-03-02 18:36:43 +00:00
|
|
|
"path"
|
2015-12-21 05:37:49 +00:00
|
|
|
gruntime "runtime"
|
2015-09-04 23:33:11 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2015-12-01 16:52:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-09-04 23:33:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/util/editor"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/util/jsonmerge"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
2015-12-21 05:37:49 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-10-23 15:31:03 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-09-04 23:33:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/strategicpatch"
|
|
|
|
"k8s.io/kubernetes/pkg/util/yaml"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
editLong = `Edit a resource from the default editor.
|
|
|
|
|
|
|
|
The edit command allows you to directly edit any API resource you can retrieve via the
|
2015-11-11 11:29:52 +00:00
|
|
|
command line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR
|
|
|
|
environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows.
|
|
|
|
You can edit multiple objects, although changes are applied one at a time. The command
|
2015-10-23 15:31:03 +00:00
|
|
|
accepts filenames as well as command line arguments, although the files you point to must
|
|
|
|
be previously saved versions of resources.
|
2015-09-04 23:33:11 +00:00
|
|
|
|
|
|
|
The files to edit will be output in the default API version, or a version specified
|
|
|
|
by --output-version. The default format is YAML - if you would like to edit in JSON
|
2015-10-23 15:31:03 +00:00
|
|
|
pass -o json. The flag --windows-line-endings can be used to force Windows line endings,
|
|
|
|
otherwise the default for your operating system will be used.
|
2015-09-04 23:33:11 +00:00
|
|
|
|
|
|
|
In the event an error occurs while updating, a temporary file will be created on disk
|
|
|
|
that contains your unapplied changes. The most common error when updating a resource
|
|
|
|
is another editor changing the resource on the server. When this occurs, you will have
|
|
|
|
to apply your changes to the newer version of the resource, or update your temporary
|
|
|
|
saved copy to include the latest resource version.`
|
|
|
|
|
|
|
|
editExample = ` # Edit the service named 'docker-registry':
|
2016-02-29 14:41:09 +00:00
|
|
|
kubectl edit svc/docker-registry
|
2015-09-04 23:33:11 +00:00
|
|
|
|
|
|
|
# Use an alternative editor
|
2016-02-29 14:41:09 +00:00
|
|
|
KUBE_EDITOR="nano" kubectl edit svc/docker-registry
|
2015-09-04 23:33:11 +00:00
|
|
|
|
|
|
|
# Edit the service 'docker-registry' in JSON using the v1 API format:
|
2016-02-29 14:41:09 +00:00
|
|
|
kubectl edit svc/docker-registry --output-version=v1 -o json`
|
2015-09-04 23:33:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errExit = fmt.Errorf("exit directly")
|
|
|
|
|
|
|
|
func NewCmdEdit(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
|
|
|
filenames := []string{}
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "edit (RESOURCE/NAME | -f FILENAME)",
|
|
|
|
Short: "Edit a resource on the server",
|
|
|
|
Long: editLong,
|
|
|
|
Example: fmt.Sprintf(editExample),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
err := RunEdit(f, out, cmd, args, filenames)
|
|
|
|
if err == errExit {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cmdutil.CheckErr(err)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
usage := "Filename, directory, or URL to file to use to edit the resource"
|
|
|
|
kubectl.AddJsonFilenameFlag(cmd, &filenames, usage)
|
|
|
|
cmd.Flags().StringP("output", "o", "yaml", "Output format. One of: yaml|json.")
|
|
|
|
cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
|
2015-12-21 05:37:49 +00:00
|
|
|
cmd.Flags().Bool("windows-line-endings", gruntime.GOOS == "windows", "Use Windows line-endings (default Unix line-endings)")
|
2015-11-04 21:47:08 +00:00
|
|
|
cmdutil.AddApplyAnnotationFlags(cmd)
|
2016-01-22 18:33:23 +00:00
|
|
|
cmdutil.AddRecordFlag(cmd)
|
2015-09-04 23:33:11 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunEdit(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, filenames []string) error {
|
|
|
|
var printer kubectl.ResourcePrinter
|
|
|
|
var ext string
|
|
|
|
switch format := cmdutil.GetFlagString(cmd, "output"); format {
|
|
|
|
case "json":
|
|
|
|
printer = &kubectl.JSONPrinter{}
|
|
|
|
ext = ".json"
|
|
|
|
case "yaml":
|
|
|
|
printer = &kubectl.YAMLPrinter{}
|
|
|
|
ext = ".yaml"
|
|
|
|
default:
|
|
|
|
return cmdutil.UsageError(cmd, "The flag 'output' must be one of yaml|json")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mapper, typer := f.Object()
|
2015-12-21 05:37:49 +00:00
|
|
|
resourceMapper := &resource.Mapper{
|
2015-09-04 23:33:11 +00:00
|
|
|
ObjectTyper: typer,
|
|
|
|
RESTMapper: mapper,
|
2015-12-21 05:37:49 +00:00
|
|
|
ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
|
|
|
|
Decoder: f.Decoder(true),
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 05:37:49 +00:00
|
|
|
r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
|
2015-09-04 23:33:11 +00:00
|
|
|
NamespaceParam(cmdNamespace).DefaultNamespace().
|
|
|
|
FilenameParam(enforceNamespace, filenames...).
|
|
|
|
ResourceTypeOrNameArgs(true, args...).
|
|
|
|
Latest().
|
|
|
|
Flatten().
|
|
|
|
Do()
|
|
|
|
err = r.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
infos, err := r.Infos()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConfig, err := f.ClientConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-21 05:37:49 +00:00
|
|
|
encoder := f.JSONEncoder()
|
2015-12-01 16:52:11 +00:00
|
|
|
defaultVersion, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
objs, err := resource.AsVersionedObjects(infos, defaultVersion.String(), encoder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-20 22:22:54 +00:00
|
|
|
|
2016-02-02 12:30:12 +00:00
|
|
|
var (
|
|
|
|
windowsLineEndings = cmdutil.GetFlagBool(cmd, "windows-line-endings")
|
|
|
|
edit = editor.NewDefaultEditor(f.EditorEnvs())
|
|
|
|
results = editResults{}
|
|
|
|
original = []byte{}
|
|
|
|
edited = []byte{}
|
|
|
|
file string
|
|
|
|
)
|
|
|
|
|
|
|
|
outter:
|
|
|
|
for i := range objs {
|
|
|
|
obj := objs[i]
|
|
|
|
// some bookkeeping
|
|
|
|
results.header.flush()
|
|
|
|
containsError := false
|
|
|
|
|
|
|
|
for {
|
2015-10-20 22:22:54 +00:00
|
|
|
// generate the file to edit
|
|
|
|
buf := &bytes.Buffer{}
|
2015-10-23 15:31:03 +00:00
|
|
|
var w io.Writer = buf
|
|
|
|
if windowsLineEndings {
|
|
|
|
w = util.NewCRLFWriter(w)
|
|
|
|
}
|
|
|
|
if err := results.header.writeTo(w); err != nil {
|
2015-10-20 22:22:54 +00:00
|
|
|
return preservedFile(err, results.file, out)
|
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
if !containsError {
|
|
|
|
if err := printer.PrintObj(obj, w); err != nil {
|
|
|
|
return preservedFile(err, results.file, out)
|
|
|
|
}
|
|
|
|
original = buf.Bytes()
|
|
|
|
} else {
|
|
|
|
// In case of an error, preserve the edited file.
|
|
|
|
// Remove the comments (header) from it since we already
|
|
|
|
// have included the latest header in the buffer above.
|
|
|
|
buf.Write(manualStrip(edited))
|
2015-10-20 22:22:54 +00:00
|
|
|
}
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
// launch the editor
|
2016-02-02 12:30:12 +00:00
|
|
|
editedDiff := edited
|
2016-03-02 18:36:43 +00:00
|
|
|
edited, file, err = edit.LaunchTempFile(fmt.Sprintf("%s-edit-", path.Base(os.Args[0])), ext, buf)
|
2015-10-20 22:22:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return preservedFile(err, results.file, out)
|
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
if bytes.Equal(stripComments(editedDiff), stripComments(edited)) {
|
|
|
|
// Ugly hack right here. We will hit this either (1) when we try to
|
|
|
|
// save the same changes we tried to save in the previous iteration
|
|
|
|
// which means our changes are invalid or (2) when we exit the second
|
|
|
|
// time. The second case is more usual so we can probably live with it.
|
|
|
|
// TODO: A less hacky fix would be welcome :)
|
|
|
|
fmt.Fprintln(out, "Edit cancelled, no valid changes were saved.")
|
|
|
|
continue outter
|
|
|
|
}
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
// cleanup any file from the previous pass
|
|
|
|
if len(results.file) > 0 {
|
|
|
|
os.Remove(results.file)
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("User edited:\n%s", string(edited))
|
2016-02-02 12:30:12 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
// Compare content without comments
|
|
|
|
if bytes.Equal(stripComments(original), stripComments(edited)) {
|
2016-02-02 12:30:12 +00:00
|
|
|
os.Remove(file)
|
2015-10-20 22:22:54 +00:00
|
|
|
fmt.Fprintln(out, "Edit cancelled, no changes made.")
|
2016-02-02 12:30:12 +00:00
|
|
|
continue outter
|
|
|
|
}
|
|
|
|
lines, err := hasLines(bytes.NewBuffer(edited))
|
|
|
|
if err != nil {
|
|
|
|
return preservedFile(err, file, out)
|
2015-10-20 22:22:54 +00:00
|
|
|
}
|
|
|
|
if !lines {
|
2016-02-02 12:30:12 +00:00
|
|
|
os.Remove(file)
|
2015-10-20 22:22:54 +00:00
|
|
|
fmt.Fprintln(out, "Edit cancelled, saved file was empty.")
|
2016-02-02 12:30:12 +00:00
|
|
|
continue outter
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
results = editResults{
|
|
|
|
file: file,
|
|
|
|
}
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
// parse the edited file
|
2015-12-21 05:37:49 +00:00
|
|
|
updates, err := resourceMapper.InfoForData(edited, "edited-file")
|
2015-10-20 22:22:54 +00:00
|
|
|
if err != nil {
|
2016-02-02 12:30:12 +00:00
|
|
|
// syntax error
|
|
|
|
containsError = true
|
|
|
|
results.header.reasons = append(results.header.reasons, editReason{head: fmt.Sprintf("The edited file had a syntax error: %v", err)})
|
|
|
|
continue
|
2015-10-20 22:22:54 +00:00
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
// not a syntax error as it turns out...
|
|
|
|
containsError = false
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-11-04 21:47:08 +00:00
|
|
|
// put configuration annotation in "updates"
|
2015-12-21 05:37:49 +00:00
|
|
|
if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), updates, encoder); err != nil {
|
2015-11-04 21:47:08 +00:00
|
|
|
return preservedFile(err, file, out)
|
|
|
|
}
|
2016-01-22 18:33:23 +00:00
|
|
|
if cmdutil.ShouldRecord(cmd, updates) {
|
|
|
|
err = cmdutil.RecordChangeCause(updates.Object, f.Command())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
editedCopy := edited
|
|
|
|
if editedCopy, err = runtime.Encode(encoder, updates.Object); err != nil {
|
2015-10-20 22:22:54 +00:00
|
|
|
return preservedFile(err, file, out)
|
|
|
|
}
|
2015-09-10 21:32:57 +00:00
|
|
|
|
2015-12-21 05:37:49 +00:00
|
|
|
visitor := resource.NewFlattenListVisitor(updates, resourceMapper)
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
// need to make sure the original namespace wasn't changed while editing
|
|
|
|
if err = visitor.Visit(resource.RequireNamespace(cmdNamespace)); err != nil {
|
|
|
|
return preservedFile(err, file, out)
|
|
|
|
}
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
// use strategic merge to create a patch
|
|
|
|
originalJS, err := yaml.ToJSON(original)
|
|
|
|
if err != nil {
|
|
|
|
return preservedFile(err, file, out)
|
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
editedJS, err := yaml.ToJSON(editedCopy)
|
2015-10-20 22:22:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return preservedFile(err, file, out)
|
|
|
|
}
|
|
|
|
patch, err := strategicpatch.CreateStrategicMergePatch(originalJS, editedJS, obj)
|
|
|
|
// TODO: change all jsonmerge to strategicpatch
|
|
|
|
// for checking preconditions
|
|
|
|
preconditions := []jsonmerge.PreconditionFunc{}
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("Unable to calculate diff, no merge is possible: %v", err)
|
|
|
|
return preservedFile(err, file, out)
|
|
|
|
} else {
|
|
|
|
preconditions = append(preconditions, jsonmerge.RequireKeyUnchanged("apiVersion"))
|
|
|
|
preconditions = append(preconditions, jsonmerge.RequireKeyUnchanged("kind"))
|
|
|
|
preconditions = append(preconditions, jsonmerge.RequireMetadataKeyUnchanged("name"))
|
|
|
|
results.version = defaultVersion
|
|
|
|
}
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
if hold, msg := jsonmerge.TestPreconditionsHold(patch, preconditions); !hold {
|
|
|
|
fmt.Fprintf(out, "error: %s", msg)
|
|
|
|
return preservedFile(nil, file, out)
|
|
|
|
}
|
2015-09-04 23:33:11 +00:00
|
|
|
|
2015-10-20 22:22:54 +00:00
|
|
|
err = visitor.Visit(func(info *resource.Info, err error) error {
|
|
|
|
patched, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, api.StrategicMergePatchType, patch)
|
|
|
|
if err != nil {
|
2016-02-02 12:30:12 +00:00
|
|
|
glog.V(4).Infof(results.addError(err, info))
|
|
|
|
return err
|
2015-10-20 22:22:54 +00:00
|
|
|
}
|
|
|
|
info.Refresh(patched, true)
|
|
|
|
cmdutil.PrintSuccess(mapper, false, out, info.Mapping.Resource, info.Name, "edited")
|
2015-09-04 23:33:11 +00:00
|
|
|
return nil
|
2015-10-20 22:22:54 +00:00
|
|
|
})
|
2016-02-02 12:30:12 +00:00
|
|
|
if err == nil {
|
|
|
|
os.Remove(file)
|
|
|
|
continue outter
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
// Handle all possible errors
|
|
|
|
//
|
|
|
|
// 1. retryable: propose kubectl replace -f
|
|
|
|
// 2. notfound: indicate the location of the saved configuration of the deleted resource
|
|
|
|
// 3. invalid: retry those on the spot by looping ie. reloading the editor
|
2015-10-20 22:22:54 +00:00
|
|
|
if results.retryable > 0 {
|
2016-02-02 12:30:12 +00:00
|
|
|
fmt.Fprintf(out, "You can run `%s replace -f %s` to try this update again.\n", os.Args[0], file)
|
|
|
|
continue outter
|
2015-10-20 22:22:54 +00:00
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
if results.notfound > 0 {
|
|
|
|
fmt.Fprintf(out, "The edits you made on deleted resources have been saved to %q\n", file)
|
|
|
|
continue outter
|
2015-10-20 22:22:54 +00:00
|
|
|
}
|
2016-02-02 12:30:12 +00:00
|
|
|
// validation error
|
|
|
|
containsError = true
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// editReason preserves a message about the reason this file must be edited again
|
|
|
|
type editReason struct {
|
|
|
|
head string
|
|
|
|
other []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// editHeader includes a list of reasons the edit must be retried
|
|
|
|
type editHeader struct {
|
|
|
|
reasons []editReason
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeTo outputs the current header information into a stream
|
|
|
|
func (h *editHeader) writeTo(w io.Writer) error {
|
|
|
|
fmt.Fprint(w, `# Please edit the object below. Lines beginning with a '#' will be ignored,
|
|
|
|
# and an empty file will abort the edit. If an error occurs while saving this file will be
|
|
|
|
# reopened with the relevant failures.
|
|
|
|
#
|
|
|
|
`)
|
|
|
|
for _, r := range h.reasons {
|
|
|
|
if len(r.other) > 0 {
|
|
|
|
fmt.Fprintf(w, "# %s:\n", r.head)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "# %s\n", r.head)
|
|
|
|
}
|
|
|
|
for _, o := range r.other {
|
|
|
|
fmt.Fprintf(w, "# * %s\n", o)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "#")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-02 12:30:12 +00:00
|
|
|
func (h *editHeader) flush() {
|
|
|
|
h.reasons = []editReason{}
|
|
|
|
}
|
|
|
|
|
2015-09-04 23:33:11 +00:00
|
|
|
// editResults capture the result of an update
|
|
|
|
type editResults struct {
|
|
|
|
header editHeader
|
|
|
|
retryable int
|
|
|
|
notfound int
|
|
|
|
edit []*resource.Info
|
|
|
|
file string
|
|
|
|
|
2015-12-01 16:52:11 +00:00
|
|
|
version unversioned.GroupVersion
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *editResults) addError(err error, info *resource.Info) string {
|
|
|
|
switch {
|
|
|
|
case errors.IsInvalid(err):
|
|
|
|
r.edit = append(r.edit, info)
|
|
|
|
reason := editReason{
|
2016-02-02 12:30:12 +00:00
|
|
|
head: fmt.Sprintf("%s %q was not valid", info.Mapping.Resource, info.Name),
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
2015-11-30 22:43:52 +00:00
|
|
|
if err, ok := err.(errors.APIStatus); ok {
|
2015-09-04 23:33:11 +00:00
|
|
|
if details := err.Status().Details; details != nil {
|
|
|
|
for _, cause := range details.Causes {
|
2016-02-02 12:30:12 +00:00
|
|
|
reason.other = append(reason.other, fmt.Sprintf("%s: %s", cause.Field, cause.Message))
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.header.reasons = append(r.header.reasons, reason)
|
2016-02-02 12:30:12 +00:00
|
|
|
return fmt.Sprintf("Error: %s %q is invalid", info.Mapping.Resource, info.Name)
|
2015-09-04 23:33:11 +00:00
|
|
|
case errors.IsNotFound(err):
|
|
|
|
r.notfound++
|
2016-02-02 12:30:12 +00:00
|
|
|
return fmt.Sprintf("Error: %s %q could not be found on the server", info.Mapping.Resource, info.Name)
|
2015-09-04 23:33:11 +00:00
|
|
|
default:
|
|
|
|
r.retryable++
|
2016-02-02 12:30:12 +00:00
|
|
|
return fmt.Sprintf("Error: %s %q could not be patched: %v", info.Mapping.Resource, info.Name, err)
|
2015-09-04 23:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// preservedFile writes out a message about the provided file if it exists to the
|
|
|
|
// provided output stream when an error happens. Used to notify the user where
|
|
|
|
// their updates were preserved.
|
|
|
|
func preservedFile(err error, path string, out io.Writer) error {
|
|
|
|
if len(path) > 0 {
|
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
|
|
fmt.Fprintf(out, "A copy of your changes has been stored to %q\n", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasLines returns true if any line in the provided stream is non empty - has non-whitespace
|
|
|
|
// characters, or the first non-whitespace character is a '#' indicating a comment. Returns
|
|
|
|
// any errors encountered reading the stream.
|
|
|
|
func hasLines(r io.Reader) (bool, error) {
|
|
|
|
// TODO: if any files we read have > 64KB lines, we'll need to switch to bytes.ReadLine
|
|
|
|
// TODO: probably going to be secrets
|
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
for s.Scan() {
|
|
|
|
if line := strings.TrimSpace(s.Text()); len(line) > 0 && line[0] != '#' {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := s.Err(); err != nil && err != io.EOF {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2015-10-20 22:22:54 +00:00
|
|
|
|
|
|
|
// stripComments will transform a YAML file into JSON, thus dropping any comments
|
|
|
|
// in it. Note that if the given file has a syntax error, the transformation will
|
|
|
|
// fail and we will manually drop all comments from the file.
|
|
|
|
func stripComments(file []byte) []byte {
|
2016-02-02 12:30:12 +00:00
|
|
|
stripped := file
|
|
|
|
stripped, err := yaml.ToJSON(stripped)
|
2015-10-20 22:22:54 +00:00
|
|
|
if err != nil {
|
|
|
|
stripped = manualStrip(file)
|
|
|
|
}
|
|
|
|
return stripped
|
|
|
|
}
|
|
|
|
|
|
|
|
// manualStrip is used for dropping comments from a YAML file
|
|
|
|
func manualStrip(file []byte) []byte {
|
|
|
|
stripped := []byte{}
|
2016-02-02 12:30:12 +00:00
|
|
|
lines := bytes.Split(file, []byte("\n"))
|
|
|
|
for i, line := range lines {
|
2015-10-20 22:22:54 +00:00
|
|
|
if bytes.HasPrefix(bytes.TrimSpace(line), []byte("#")) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
stripped = append(stripped, line...)
|
2016-02-02 12:30:12 +00:00
|
|
|
if i < len(lines)-1 {
|
|
|
|
stripped = append(stripped, '\n')
|
|
|
|
}
|
2015-10-20 22:22:54 +00:00
|
|
|
}
|
|
|
|
return stripped
|
|
|
|
}
|