コード例 #1
0
ファイル: config.go プロジェクト: vahe/lxd
func (c *configCmd) doDaemonConfigEdit(client *lxd.Client) error {
	// If stdin isn't a terminal, read text from it
	if !termios.IsTerminal(int(syscall.Stdin)) {
		contents, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			return err
		}

		newdata := shared.BriefServerState{}
		err = yaml.Unmarshal(contents, &newdata)
		if err != nil {
			return err
		}

		_, err = client.UpdateServerConfig(newdata)
		return err
	}

	// Extract the current value
	config, err := client.ServerStatus()
	if err != nil {
		return err
	}

	brief := config.Brief()
	data, err := yaml.Marshal(&brief)
	if err != nil {
		return err
	}

	// Spawn the editor
	content, err := shared.TextEditor("", []byte(c.configEditHelp()+"\n\n"+string(data)))
	if err != nil {
		return err
	}

	for {
		// Parse the text received from the editor
		newdata := shared.BriefServerState{}
		err = yaml.Unmarshal(content, &newdata)
		if err == nil {
			_, err = client.UpdateServerConfig(newdata)
		}

		// Respawn the editor
		if err != nil {
			fmt.Fprintf(os.Stderr, i18n.G("Config parsing error: %s")+"\n", err)
			fmt.Println(i18n.G("Press enter to start the editor again"))

			_, err := os.Stdin.Read(make([]byte, 1))
			if err != nil {
				return err
			}

			content, err = shared.TextEditor("", content)
			if err != nil {
				return err
			}
			continue
		}
		break
	}
	return nil
}