From 0c46a701b874418a8190ea34c2d8f40ec48218b5 Mon Sep 17 00:00:00 2001 From: "Zheao.Li" Date: Tue, 17 Jan 2023 16:22:19 +0800 Subject: [PATCH] [refactor] Adjust code style for apparmor command Signed-off-by: Zheao.Li --- cmd/nerdctl/apparmor_inspect_linux.go | 5 +---- cmd/nerdctl/apparmor_ls_linux.go | 21 ++++++++++++++------- cmd/nerdctl/apparmor_unload_linux.go | 5 +---- pkg/api/types/apparmor_types.go | 14 -------------- pkg/cmd/apparmor/inspect_linux.go | 6 +++--- pkg/cmd/apparmor/list_linux.go | 7 ++++--- pkg/cmd/apparmor/unload_linux.go | 7 +++---- 7 files changed, 26 insertions(+), 39 deletions(-) diff --git a/cmd/nerdctl/apparmor_inspect_linux.go b/cmd/nerdctl/apparmor_inspect_linux.go index 72eb271bd9e..7eeff7dbee6 100644 --- a/cmd/nerdctl/apparmor_inspect_linux.go +++ b/cmd/nerdctl/apparmor_inspect_linux.go @@ -19,7 +19,6 @@ package main import ( "fmt" - "github.com/containerd/nerdctl/pkg/api/types" "github.com/containerd/nerdctl/pkg/cmd/apparmor" "github.com/containerd/nerdctl/pkg/defaults" "github.com/spf13/cobra" @@ -38,7 +37,5 @@ func newApparmorInspectCommand() *cobra.Command { } func apparmorInspectAction(cmd *cobra.Command, args []string) error { - options := &types.ApparmorInspectCommandOptions{} - options.Writer = cmd.OutOrStdout() - return apparmor.Inspect(options) + return apparmor.Inspect(cmd.OutOrStdout()) } diff --git a/cmd/nerdctl/apparmor_ls_linux.go b/cmd/nerdctl/apparmor_ls_linux.go index 79361e72230..a7a34b6e714 100644 --- a/cmd/nerdctl/apparmor_ls_linux.go +++ b/cmd/nerdctl/apparmor_ls_linux.go @@ -41,18 +41,25 @@ func newApparmorLsCommand() *cobra.Command { return cmd } -func apparmorLsAction(cmd *cobra.Command, args []string) error { - options := &types.ApparmorListCommandOptions{} +func processApparmorListCommandOptions(cmd *cobra.Command) (types.ApparmorListCommandOptions, error) { quiet, err := cmd.Flags().GetBool("quiet") if err != nil { - return err + return types.ApparmorListCommandOptions{}, err } - options.Quiet = quiet - options.Writer = cmd.OutOrStdout() format, err := cmd.Flags().GetString("format") + if err != nil { + return types.ApparmorListCommandOptions{}, err + } + return types.ApparmorListCommandOptions{ + Quiet: quiet, + Format: format, + }, nil +} + +func apparmorLsAction(cmd *cobra.Command, args []string) error { + options, err := processApparmorListCommandOptions(cmd) if err != nil { return err } - options.Format = format - return apparmor.List(options) + return apparmor.List(options, cmd.OutOrStdout()) } diff --git a/cmd/nerdctl/apparmor_unload_linux.go b/cmd/nerdctl/apparmor_unload_linux.go index 1ef354ee7c7..92fc289eb31 100644 --- a/cmd/nerdctl/apparmor_unload_linux.go +++ b/cmd/nerdctl/apparmor_unload_linux.go @@ -19,7 +19,6 @@ package main import ( "fmt" - "github.com/containerd/nerdctl/pkg/api/types" "github.com/containerd/nerdctl/pkg/cmd/apparmor" "github.com/containerd/nerdctl/pkg/defaults" "github.com/spf13/cobra" @@ -43,9 +42,7 @@ func apparmorUnloadAction(cmd *cobra.Command, args []string) error { if len(args) > 0 { target = args[0] } - options := &types.ApparmorUnloadCommandOptions{} - options.Target = target - return apparmor.Unload(options) + return apparmor.Unload(target) } func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/pkg/api/types/apparmor_types.go b/pkg/api/types/apparmor_types.go index 46fff2ae124..46e71b9478c 100644 --- a/pkg/api/types/apparmor_types.go +++ b/pkg/api/types/apparmor_types.go @@ -16,23 +16,9 @@ package types -import "io" - -type ApparmorInspectCommandOptions struct { - // Writer is the output writer - Writer io.Writer -} - -type ApparmorUnloadCommandOptions struct { - // Target is the profile name - Target string -} - type ApparmorListCommandOptions struct { // Only display profile names Quiet bool // Format the output using the given go template Format string - // Writer is the output writer - Writer io.Writer } diff --git a/pkg/cmd/apparmor/inspect_linux.go b/pkg/cmd/apparmor/inspect_linux.go index 3fb0de63a27..fb0dda7782d 100644 --- a/pkg/cmd/apparmor/inspect_linux.go +++ b/pkg/cmd/apparmor/inspect_linux.go @@ -18,17 +18,17 @@ package apparmor import ( "fmt" + "io" "github.com/containerd/containerd/contrib/apparmor" - "github.com/containerd/nerdctl/pkg/api/types" "github.com/containerd/nerdctl/pkg/defaults" ) -func Inspect(options *types.ApparmorInspectCommandOptions) error { +func Inspect(stdout io.Writer) error { b, err := apparmor.DumpDefaultProfile(defaults.AppArmorProfileName) if err != nil { return err } - _, err = fmt.Fprint(options.Writer, b) + _, err = fmt.Fprint(stdout, b) return err } diff --git a/pkg/cmd/apparmor/list_linux.go b/pkg/cmd/apparmor/list_linux.go index 901557225e2..d33bb42e087 100644 --- a/pkg/cmd/apparmor/list_linux.go +++ b/pkg/cmd/apparmor/list_linux.go @@ -20,6 +20,7 @@ import ( "bytes" "errors" "fmt" + "io" "text/tabwriter" "text/template" @@ -28,14 +29,14 @@ import ( "github.com/containerd/nerdctl/pkg/formatter" ) -func List(options *types.ApparmorListCommandOptions) error { +func List(options types.ApparmorListCommandOptions, stdout io.Writer) error { quiet := options.Quiet - w := options.Writer + w := stdout var tmpl *template.Template format := options.Format switch format { case "", "table", "wide": - w = tabwriter.NewWriter(options.Writer, 4, 8, 4, ' ', 0) + w = tabwriter.NewWriter(stdout, 4, 8, 4, ' ', 0) if !quiet { fmt.Fprintln(w, "NAME\tMODE") } diff --git a/pkg/cmd/apparmor/unload_linux.go b/pkg/cmd/apparmor/unload_linux.go index 950da7fbdbd..8994834efdf 100644 --- a/pkg/cmd/apparmor/unload_linux.go +++ b/pkg/cmd/apparmor/unload_linux.go @@ -17,12 +17,11 @@ package apparmor import ( - "github.com/containerd/nerdctl/pkg/api/types" "github.com/containerd/nerdctl/pkg/apparmorutil" "github.com/sirupsen/logrus" ) -func Unload(options *types.ApparmorUnloadCommandOptions) error { - logrus.Infof("Unloading profile %q", options.Target) - return apparmorutil.Unload(options.Target) +func Unload(target string) error { + logrus.Infof("Unloading profile %q", target) + return apparmorutil.Unload(target) }