Add no edit flag so create instancegroup command is usable on scripts

This commit is contained in:
Julio Chana 2017-11-07 12:43:56 +01:00
parent dac6813ab1
commit e4afcd8869
No known key found for this signature in database
GPG Key ID: 1BC5AA953EFC7D23
1 changed files with 39 additions and 32 deletions

View File

@ -43,6 +43,8 @@ type CreateInstanceGroupOptions struct {
DryRun bool DryRun bool
// Output type during a DryRun // Output type during a DryRun
Output string Output string
// Do not launch editor when creating an instance group
NoEditor bool
} }
var ( var (
@ -96,6 +98,7 @@ func NewCmdCreateInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command {
// DryRun mode that will print YAML or JSON // DryRun mode that will print YAML or JSON
cmd.Flags().BoolVar(&options.DryRun, "dry-run", options.DryRun, "If true, only print the object that would be sent, without sending it. This flag can be used to create a cluster YAML or JSON manifest.") cmd.Flags().BoolVar(&options.DryRun, "dry-run", options.DryRun, "If true, only print the object that would be sent, without sending it. This flag can be used to create a cluster YAML or JSON manifest.")
cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "Ouput format. One of json|yaml") cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "Ouput format. One of json|yaml")
cmd.Flags().BoolVar(&options.NoEditor, "no-editor", options.NoEditor, "If true, The instance group will be created by default with no option of editing it")
return cmd return cmd
} }
@ -179,42 +182,46 @@ func RunCreateInstanceGroup(f *util.Factory, cmd *cobra.Command, args []string,
} }
} }
var ( if !options.NoEditor {
edit = editor.NewDefaultEditor(editorEnvs) var (
) edit = editor.NewDefaultEditor(editorEnvs)
)
raw, err := kopscodecs.ToVersionedYaml(ig) raw, err := kopscodecs.ToVersionedYaml(ig)
if err != nil { if err != nil {
return err return err
}
ext := "yaml"
// launch the editor
edited, file, err := edit.LaunchTempFile(fmt.Sprintf("%s-edit-", filepath.Base(os.Args[0])), ext, bytes.NewReader(raw))
defer func() {
if file != "" {
os.Remove(file)
} }
}() ext := "yaml"
if err != nil {
return fmt.Errorf("error launching editor: %v", err) // launch the editor
edited, file, err := edit.LaunchTempFile(fmt.Sprintf("%s-edit-", filepath.Base(os.Args[0])), ext, bytes.NewReader(raw))
defer func() {
if file != "" {
os.Remove(file)
}
}()
if err != nil {
return fmt.Errorf("error launching editor: %v", err)
}
obj, _, err := kopscodecs.ParseVersionedYaml(edited)
if err != nil {
return fmt.Errorf("error parsing yaml: %v", err)
}
group, ok := obj.(*api.InstanceGroup)
if !ok {
return fmt.Errorf("unexpected object type: %T", obj)
}
err = validation.ValidateInstanceGroup(group)
if err != nil {
return err
}
ig = group
} }
obj, _, err := kopscodecs.ParseVersionedYaml(edited) _, err = clientset.InstanceGroupsFor(cluster).Create(ig)
if err != nil {
return fmt.Errorf("error parsing yaml: %v", err)
}
group, ok := obj.(*api.InstanceGroup)
if !ok {
return fmt.Errorf("unexpected object type: %T", obj)
}
err = validation.ValidateInstanceGroup(group)
if err != nil {
return err
}
_, err = clientset.InstanceGroupsFor(cluster).Create(group)
if err != nil { if err != nil {
return fmt.Errorf("error storing InstanceGroup: %v", err) return fmt.Errorf("error storing InstanceGroup: %v", err)
} }