From 7c84b4b60b864f722ac373db9f1ad3280edb9bd2 Mon Sep 17 00:00:00 2001 From: Vishesh Agrawal Date: Tue, 16 Apr 2024 09:54:01 +0200 Subject: [PATCH] incus/profile: Add support for creating from YAML Part of #741 Signed-off-by: Vishesh Agrawal --- cmd/incus/profile.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cmd/incus/profile.go b/cmd/incus/profile.go index 57e528fe639..89820641d1e 100644 --- a/cmd/incus/profile.go +++ b/cmd/incus/profile.go @@ -351,6 +351,10 @@ func (c *cmdProfileCreate) Command() *cobra.Command { cmd.Short = i18n.G("Create profiles") cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G( `Create profiles`)) + cmd.Example = cli.FormatSection("", i18n.G(`incus profile create p1 + +incus profile create p1 < config.yaml + Create profile with configuration from config.yaml`)) cmd.RunE = c.Run @@ -366,12 +370,27 @@ func (c *cmdProfileCreate) Command() *cobra.Command { } func (c *cmdProfileCreate) Run(cmd *cobra.Command, args []string) error { + var stdinData api.ProfilePut + // Quick checks. exit, err := c.global.CheckArgs(cmd, args, 1, 1) if exit { return err } + // If stdin isn't a terminal, read text from it + if !termios.IsTerminal(getStdinFd()) { + contents, err := io.ReadAll(os.Stdin) + if err != nil { + return err + } + + err = yaml.Unmarshal(contents, &stdinData) + if err != nil { + return err + } + } + // Parse remote resources, err := c.global.ParseServers(args[0]) if err != nil { @@ -387,6 +406,7 @@ func (c *cmdProfileCreate) Run(cmd *cobra.Command, args []string) error { // Create the profile profile := api.ProfilesPost{} profile.Name = resource.name + profile.ProfilePut = stdinData err = resource.server.CreateProfile(profile) if err != nil {