The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/4967
This e-mail was sent by the LXC bot, direct replies will not reach the author unless they happen to be subscribed to this list. === Description (from pull-request) === Closes #4945 Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
From 1416bcbad3101eb48577f3ed9ece6c9b7d4e3537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgra...@ubuntu.com> Date: Thu, 23 Aug 2018 15:05:41 -0400 Subject: [PATCH] lxc: Add support for quiet mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #4945 Signed-off-by: Stéphane Graber <stgra...@ubuntu.com> --- lxc/action.go | 4 +++- lxc/cluster.go | 10 ++++++++-- lxc/config_device.go | 13 ++++++++++--- lxc/copy.go | 6 +++++- lxc/export.go | 5 ++++- lxc/file.go | 4 ++++ lxc/image.go | 24 ++++++++++++++++++++---- lxc/import.go | 1 + lxc/init.go | 16 +++++++++++----- lxc/launch.go | 5 ++++- lxc/main.go | 2 ++ lxc/network.go | 20 ++++++++++++++------ lxc/operation.go | 5 ++++- lxc/profile.go | 26 ++++++++++++++++++++------ lxc/storage.go | 15 ++++++++++----- lxc/storage_volume.go | 18 ++++++++++++++---- lxc/utils/progress.go | 11 +++++++++++ 17 files changed, 145 insertions(+), 40 deletions(-) diff --git a/lxc/action.go b/lxc/action.go index fd16c82d23..9c74fe668e 100644 --- a/lxc/action.go +++ b/lxc/action.go @@ -180,7 +180,9 @@ func (c *cmdAction) doAction(action string, conf *config.Config, nameArg string) return err } - progress := utils.ProgressRenderer{} + progress := utils.ProgressRenderer{ + Quiet: c.global.flagQuiet, + } _, err = op.AddHandler(progress.UpdateOp) if err != nil { progress.Done("") diff --git a/lxc/cluster.go b/lxc/cluster.go index ec20cfe6b8..afc13e6274 100644 --- a/lxc/cluster.go +++ b/lxc/cluster.go @@ -223,7 +223,10 @@ func (c *cmdClusterRename) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Member %s renamed to %s")+"\n", resource.name, args[1]) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Member %s renamed to %s")+"\n", resource.name, args[1]) + } + return nil } @@ -270,7 +273,10 @@ func (c *cmdClusterRemove) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Member %s removed")+"\n", resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Member %s removed")+"\n", resource.name) + } + return nil } diff --git a/lxc/config_device.go b/lxc/config_device.go index f0f42e1454..0cb5b0e5b4 100644 --- a/lxc/config_device.go +++ b/lxc/config_device.go @@ -160,7 +160,10 @@ func (c *cmdConfigDeviceAdd) Run(cmd *cobra.Command, args []string) error { } } - fmt.Printf(i18n.G("Device %s added to %s")+"\n", devname, resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Device %s added to %s")+"\n", devname, resource.name) + } + return nil } @@ -384,7 +387,9 @@ func (c *cmdConfigDeviceOverride) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Device %s overridden for %s")+"\n", devname, resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Device %s overridden for %s")+"\n", devname, resource.name) + } return nil } @@ -473,7 +478,9 @@ func (c *cmdConfigDeviceRemove) Run(cmd *cobra.Command, args []string) error { } } - fmt.Printf(i18n.G("Device %s removed from %s")+"\n", strings.Join(args[1:], ", "), resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Device %s removed from %s")+"\n", strings.Join(args[1:], ", "), resource.name) + } return nil } diff --git a/lxc/copy.go b/lxc/copy.go index fff2d7040c..eef1b28be6 100644 --- a/lxc/copy.go +++ b/lxc/copy.go @@ -311,7 +311,11 @@ func (c *cmdCopy) copyContainer(conf *config.Config, sourceResource string, } // Watch the background operation - progress := utils.ProgressRenderer{Format: i18n.G("Transferring container: %s")} + progress := utils.ProgressRenderer{ + Format: i18n.G("Transferring container: %s"), + Quiet: c.global.flagQuiet, + } + _, err = op.AddHandler(progress.UpdateOp) if err != nil { progress.Done("") diff --git a/lxc/export.go b/lxc/export.go index 6afc32c186..90fc968e29 100644 --- a/lxc/export.go +++ b/lxc/export.go @@ -106,7 +106,10 @@ func (c *cmdExport) Run(cmd *cobra.Command, args []string) error { defer target.Close() // Prepare the download request - progress := utils.ProgressRenderer{Format: i18n.G("Exporting the backup: %s")} + progress := utils.ProgressRenderer{ + Format: i18n.G("Exporting the backup: %s"), + Quiet: c.global.flagQuiet, + } backupFileRequest := lxd.BackupFileRequest{ BackupFile: io.WriteSeeker(target), ProgressHandler: progress.UpdateProgress, diff --git a/lxc/file.go b/lxc/file.go index c12154085a..b81869f482 100644 --- a/lxc/file.go +++ b/lxc/file.go @@ -332,6 +332,7 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error { progress := utils.ProgressRenderer{ Format: fmt.Sprintf(i18n.G("Pulling %s from %s: %%s"), targetPath, pathSpec[1]), + Quiet: c.global.flagQuiet, } writer := &ioprogress.ProgressWriter{ @@ -590,6 +591,7 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error { progress := utils.ProgressRenderer{ Format: fmt.Sprintf(i18n.G("Pushing %s to %s: %%s"), f.Name(), fpath), + Quiet: c.global.flagQuiet, } args.Content = shared.NewReadSeeker(&ioprogress.ProgressReader{ @@ -653,6 +655,7 @@ func (c *cmdFile) recursivePullFile(d lxd.ContainerServer, container string, p s progress := utils.ProgressRenderer{ Format: fmt.Sprintf(i18n.G("Pulling %s from %s: %%s"), p, target), + Quiet: c.global.flagQuiet, } writer := &ioprogress.ProgressWriter{ @@ -744,6 +747,7 @@ func (c *cmdFile) recursivePushFile(d lxd.ContainerServer, container string, sou progress := utils.ProgressRenderer{ Format: fmt.Sprintf(i18n.G("Pushing %s to %s: %%s"), p, targetPath), + Quiet: c.global.flagQuiet, } if args.Type != "directory" { diff --git a/lxc/image.go b/lxc/image.go index 9e98ac1545..6e47f65853 100644 --- a/lxc/image.go +++ b/lxc/image.go @@ -214,7 +214,11 @@ func (c *cmdImageCopy) Run(cmd *cobra.Command, args []string) error { } // Register progress handler - progress := utils.ProgressRenderer{Format: i18n.G("Copying the image: %s")} + progress := utils.ProgressRenderer{ + Format: i18n.G("Copying the image: %s"), + Quiet: c.global.flagQuiet, + } + _, err = op.AddHandler(progress.UpdateOp) if err != nil { progress.Done("") @@ -491,7 +495,11 @@ func (c *cmdImageExport) Run(cmd *cobra.Command, args []string) error { defer destRootfs.Close() // Prepare the download request - progress := utils.ProgressRenderer{Format: i18n.G("Exporting the image: %s")} + progress := utils.ProgressRenderer{ + Format: i18n.G("Exporting the image: %s"), + Quiet: c.global.flagQuiet, + } + req := lxd.ImageFileRequest{ MetaFile: io.WriteSeeker(dest), RootfsFile: io.WriteSeeker(destRootfs), @@ -683,7 +691,11 @@ func (c *cmdImageImport) Run(cmd *cobra.Command, args []string) error { image.Properties[strings.TrimSpace(fields[0])] = strings.TrimSpace(fields[1]) } - progress := utils.ProgressRenderer{Format: i18n.G("Transferring image: %s")} + progress := utils.ProgressRenderer{ + Format: i18n.G("Transferring image: %s"), + Quiet: c.global.flagQuiet, + } + if strings.HasPrefix(imageFile, "https://") { image.Source = &api.ImagesPostSource{} image.Source.Type = "url" @@ -1236,7 +1248,11 @@ func (c *cmdImageRefresh) Run(cmd *cobra.Command, args []string) error { } image := c.image.dereferenceAlias(resource.server, resource.name) - progress := utils.ProgressRenderer{Format: i18n.G("Refreshing the image: %s")} + progress := utils.ProgressRenderer{ + Format: i18n.G("Refreshing the image: %s"), + Quiet: c.global.flagQuiet, + } + op, err := resource.server.RefreshImage(image) if err != nil { return err diff --git a/lxc/import.go b/lxc/import.go index c7163a7af0..4f86e854be 100644 --- a/lxc/import.go +++ b/lxc/import.go @@ -66,6 +66,7 @@ func (c *cmdImport) Run(cmd *cobra.Command, args []string) error { progress := utils.ProgressRenderer{ Format: i18n.G("Importing container: %s"), + Quiet: c.global.flagQuiet, } createArgs := lxd.ContainerBackupArgs{} diff --git a/lxc/init.go b/lxc/init.go index 4b562fc0e7..2960a35c9c 100644 --- a/lxc/init.go +++ b/lxc/init.go @@ -96,10 +96,12 @@ func (c *cmdInit) create(conf *config.Config, args []string) (lxd.ContainerServe profiles = append(profiles, p) } - if name == "" { - fmt.Printf(i18n.G("Creating the container") + "\n") - } else { - fmt.Printf(i18n.G("Creating %s")+"\n", name) + if !c.global.flagQuiet { + if name == "" { + fmt.Printf(i18n.G("Creating the container") + "\n") + } else { + fmt.Printf(i18n.G("Creating %s")+"\n", name) + } } devicesMap := map[string]map[string]string{} @@ -202,7 +204,11 @@ func (c *cmdInit) create(conf *config.Config, args []string) (lxd.ContainerServe } // Watch the background operation - progress := utils.ProgressRenderer{Format: i18n.G("Retrieving image: %s")} + progress := utils.ProgressRenderer{ + Format: i18n.G("Retrieving image: %s"), + Quiet: c.global.flagQuiet, + } + _, err = op.AddHandler(progress.UpdateOp) if err != nil { progress.Done("") diff --git a/lxc/launch.go b/lxc/launch.go index 8345e55273..2f51cea8c5 100644 --- a/lxc/launch.go +++ b/lxc/launch.go @@ -60,7 +60,10 @@ func (c *cmdLaunch) Run(cmd *cobra.Command, args []string) error { } // Start the container - fmt.Printf(i18n.G("Starting %s")+"\n", name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Starting %s")+"\n", name) + } + req := api.ContainerStatePut{ Action: "start", Timeout: -1, diff --git a/lxc/main.go b/lxc/main.go index 59cbe52ad0..1ec8c08a9f 100644 --- a/lxc/main.go +++ b/lxc/main.go @@ -33,6 +33,7 @@ type cmdGlobal struct { flagHelpAll bool flagLogDebug bool flagLogVerbose bool + flagQuiet bool flagVersion bool } @@ -58,6 +59,7 @@ For help with any of those, simply call them with --help.`)) app.PersistentFlags().BoolVar(&globalCmd.flagForceLocal, "force-local", false, i18n.G("Force using the local unix socket")) app.PersistentFlags().BoolVar(&globalCmd.flagLogDebug, "debug", false, i18n.G("Show all debug messages")) app.PersistentFlags().BoolVarP(&globalCmd.flagLogVerbose, "verbose", "v", false, i18n.G("Show all information messages")) + app.PersistentFlags().BoolVarP(&globalCmd.flagQuiet, "quiet", "q", false, i18n.G("Don't show progress information")) // Wrappers app.PersistentPreRunE = globalCmd.PreRun diff --git a/lxc/network.go b/lxc/network.go index 3eea68535a..0547548749 100644 --- a/lxc/network.go +++ b/lxc/network.go @@ -303,10 +303,12 @@ func (c *cmdNetworkCreate) Run(cmd *cobra.Command, args []string) error { return err } - if c.network.flagTarget != "" { - fmt.Printf(i18n.G("Network %s pending on member %s")+"\n", resource.name, c.network.flagTarget) - } else { - fmt.Printf(i18n.G("Network %s created")+"\n", resource.name) + if !c.global.flagQuiet { + if c.network.flagTarget != "" { + fmt.Printf(i18n.G("Network %s pending on member %s")+"\n", resource.name, c.network.flagTarget) + } else { + fmt.Printf(i18n.G("Network %s created")+"\n", resource.name) + } } return nil @@ -356,7 +358,10 @@ func (c *cmdNetworkDelete) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Network %s deleted")+"\n", resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Network %s deleted")+"\n", resource.name) + } + return nil } @@ -1009,7 +1014,10 @@ func (c *cmdNetworkRename) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Network %s renamed to %s")+"\n", resource.name, args[1]) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Network %s renamed to %s")+"\n", resource.name, args[1]) + } + return nil } diff --git a/lxc/operation.go b/lxc/operation.go index a38935615a..e950227121 100644 --- a/lxc/operation.go +++ b/lxc/operation.go @@ -80,7 +80,10 @@ func (c *cmdOperationDelete) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Operation %s deleted")+"\n", resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Operation %s deleted")+"\n", resource.name) + } + return nil } diff --git a/lxc/profile.go b/lxc/profile.go index 3328b09ac8..00c7fdf677 100644 --- a/lxc/profile.go +++ b/lxc/profile.go @@ -144,7 +144,9 @@ func (c *cmdProfileAdd) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Profile %s added to %s")+"\n", args[1], resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Profile %s added to %s")+"\n", args[1], resource.name) + } return nil } @@ -222,7 +224,9 @@ func (c *cmdProfileAssign) Run(cmd *cobra.Command, args []string) error { args[1] = i18n.G("(none)") } - fmt.Printf(i18n.G("Profiles %s applied to %s")+"\n", args[1], resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Profiles %s applied to %s")+"\n", args[1], resource.name) + } return nil } @@ -330,7 +334,10 @@ func (c *cmdProfileCreate) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Profile %s created")+"\n", resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Profile %s created")+"\n", resource.name) + } + return nil } @@ -378,7 +385,9 @@ func (c *cmdProfileDelete) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Profile %s deleted")+"\n", resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Profile %s deleted")+"\n", resource.name) + } return nil } @@ -687,7 +696,10 @@ func (c *cmdProfileRemove) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Profile %s removed from %s")+"\n", args[1], resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Profile %s removed from %s")+"\n", args[1], resource.name) + } + return nil } @@ -735,7 +747,9 @@ func (c *cmdProfileRename) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Profile %s renamed to %s")+"\n", resource.name, args[1]) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Profile %s renamed to %s")+"\n", resource.name, args[1]) + } return nil } diff --git a/lxc/storage.go b/lxc/storage.go index d958c0b78f..0a6b85107c 100644 --- a/lxc/storage.go +++ b/lxc/storage.go @@ -138,10 +138,12 @@ func (c *cmdStorageCreate) Run(cmd *cobra.Command, args []string) error { return err } - if c.storage.flagTarget != "" { - fmt.Printf(i18n.G("Storage pool %s pending on member %s")+"\n", resource.name, c.storage.flagTarget) - } else { - fmt.Printf(i18n.G("Storage pool %s created")+"\n", resource.name) + if !c.global.flagQuiet { + if c.storage.flagTarget != "" { + fmt.Printf(i18n.G("Storage pool %s pending on member %s")+"\n", resource.name, c.storage.flagTarget) + } else { + fmt.Printf(i18n.G("Storage pool %s created")+"\n", resource.name) + } } return nil @@ -191,7 +193,10 @@ func (c *cmdStorageDelete) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Storage pool %s deleted")+"\n", resource.name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Storage pool %s deleted")+"\n", resource.name) + } + return nil } diff --git a/lxc/storage_volume.go b/lxc/storage_volume.go index 872be04b2d..2ab438bca8 100644 --- a/lxc/storage_volume.go +++ b/lxc/storage_volume.go @@ -373,7 +373,11 @@ func (c *cmdStorageVolumeCopy) Run(cmd *cobra.Command, args []string) error { } // Register progress handler - progress := utils.ProgressRenderer{Format: opMsg} + progress := utils.ProgressRenderer{ + Format: opMsg, + Quiet: c.global.flagQuiet, + } + _, err = op.AddHandler(progress.UpdateOp) if err != nil { progress.Done("") @@ -467,7 +471,9 @@ func (c *cmdStorageVolumeCreate) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Storage volume %s created")+"\n", args[1]) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Storage volume %s created")+"\n", args[1]) + } return nil } @@ -528,7 +534,9 @@ func (c *cmdStorageVolumeDelete) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G("Storage volume %s deleted")+"\n", args[1]) + if !c.global.flagQuiet { + fmt.Printf(i18n.G("Storage volume %s deleted")+"\n", args[1]) + } return nil } @@ -1056,7 +1064,9 @@ func (c *cmdStorageVolumeRename) Run(cmd *cobra.Command, args []string) error { return err } - fmt.Printf(i18n.G(`Renamed storage volume from "%s" to "%s"`)+"\n", volName, vol.Name) + if !c.global.flagQuiet { + fmt.Printf(i18n.G(`Renamed storage volume from "%s" to "%s"`)+"\n", volName, vol.Name) + } return nil } diff --git a/lxc/utils/progress.go b/lxc/utils/progress.go index ec88141467..3b5ebcf48d 100644 --- a/lxc/utils/progress.go +++ b/lxc/utils/progress.go @@ -13,6 +13,7 @@ import ( // ProgressRenderer tracks the progress information type ProgressRenderer struct { Format string + Quiet bool maxLength int wait time.Time @@ -34,6 +35,11 @@ func (p *ProgressRenderer) Done(msg string) { // Mark this renderer as done p.done = true + // Handle quiet mode + if p.Quiet { + msg = "" + } + // If we're not printing a completion message and nothing was printed before just return if msg == "" && p.maxLength == 0 { return @@ -71,6 +77,11 @@ func (p *ProgressRenderer) Update(status string) { return } + // Handle quiet mode + if p.Quiet { + return + } + // Print the new message msg := "%s" if p.Format != "" {
_______________________________________________ lxc-devel mailing list lxc-devel@lists.linuxcontainers.org http://lists.linuxcontainers.org/listinfo/lxc-devel