shared/util.go
func TextEditor(inPath string, inContent []byte) ([]byte, error) {
	var f *os.File
	var err error
	var path string

	// Detect the text editor to use
	editor := os.Getenv("VISUAL")
	if editor == "" {
		editor = os.Getenv("EDITOR")
		if editor == "" {
			for _, p := range []string{"editor", "vi", "emacs", "nano"} {
				_, err := exec.LookPath(p)
				if err == nil {
					editor = p
					break
				}
			}
			if editor == "" {
				return []byte{}, fmt.Errorf("No text editor found, please set the EDITOR environment variable")
			}
		}
	}

	if inPath == "" {
		// If provided input, create a new file
		f, err = ioutil.TempFile("", "lxd_editor_")
		if err != nil {
			return []byte{}, err
		}

		err = os.Chmod(f.Name(), 0600)
		if err != nil {
			f.Close()
			os.Remove(f.Name())
			return []byte{}, err
		}

		f.Write(inContent)
		f.Close()

		path = fmt.Sprintf("%s.yaml", f.Name())
		os.Rename(f.Name(), path)
		defer os.Remove(path)
	} else {
		path = inPath
	}

	cmdParts := strings.Fields(editor)
	cmd := exec.Command(cmdParts[0], append(cmdParts[1:], path)...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		return []byte{}, err
	}

	content, err := ioutil.ReadFile(path)
	if err != nil {
		return []byte{}, err
	}

	return content, nil
}
func TextEditor(inPath string, inContent []byte) ([]byte, error) {
	var f *os.File
	var err error
	var path string

	// Detect the text editor to use
	editor := os.Getenv("VISUAL")
	if editor == "" {
		editor = os.Getenv("EDITOR")
		if editor == "" {
			for _, p := range []string{"editor", "vi", "emacs", "nano"} {
				_, err := exec.LookPath(p)
				if err == nil {
					editor = p
					break
				}
			}
			if editor == "" {
				return []byte{}, fmt.Errorf("No text editor found, please set the EDITOR environment variable")
			}
		}
	}

	if inPath == "" {
		// If provided input, create a new file
		f, err = ioutil.TempFile("", "lxd_editor_")
		if err != nil {
			return []byte{}, err
		}

		err = os.Chmod(f.Name(), 0600)
		if err != nil {
			f.Close()
			os.Remove(f.Name())
			return []byte{}, err
		}

		f.Write(inContent)
		f.Close()

		path = fmt.Sprintf("%s.yaml", f.Name())
		os.Rename(f.Name(), path)
		defer os.Remove(path)
	} else {
		path = inPath
	}

	cmdParts := strings.Fields(editor)
	cmd := exec.Command(cmdParts[0], append(cmdParts[1:], path)...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		return []byte{}, err
	}

	content, err := ioutil.ReadFile(path)
	if err != nil {
		return []byte{}, err
	}

	return content, nil
}
func TextEditor ( inPath string , inContent [ ] byte ) ( [ ] byte , error ) { var f * os . File var err error var path string // Detect the text editor to use editor := os . Getenv ( " " ) if editor == " " { editor = os . Getenv ( " " ) if editor == " " { for _ , p := range [ ] string { " " , " " , " " , " " } { _ , err := exec . LookPath ( p ) if err == nil { editor = p break } } if editor == " " { return [ ] byte { } , fmt . Errorf ( " " ) } } } if inPath == " " { // If provided input, create a new file f , err = ioutil . TempFile ( " " , " " ) if err != nil { return [ ] byte { } , err } err = os . Chmod ( f . Name ( ) , 0600 ) if err != nil { f . Close ( ) os . Remove ( f . Name ( ) ) return [ ] byte { } , err } f . Write ( inContent ) f . Close ( ) path = fmt . Sprintf ( " " , f . Name ( ) ) os . Rename ( f . Name ( ) , path ) defer os . Remove ( path ) } else { path = inPath } cmdParts := strings . Fields ( editor ) cmd := exec . Command ( cmdParts [ 0 ] , append ( cmdParts [ 1 : ] , path ) ... ) cmd . Stdin = os . Stdin cmd . Stdout = os . Stdout cmd . Stderr = os . Stderr err = cmd . Run ( ) if err != nil { return [ ] byte { } , err } content , err := ioutil . ReadFile ( path ) if err != nil { return [ ] byte { } , err } return content , nil }
// Spawn the editor with a temporary YAML file for editing configs
Spawn the editor with a temporary YAML file for editing configs