Skip to content

Commit

Permalink
Configure flags of the ctr command
Browse files Browse the repository at this point in the history
Signed-off-by: Jacek Ewertowski <[email protected]>
  • Loading branch information
jewertow committed May 23, 2021
1 parent 764a2fa commit 59ab76d
Showing 1 changed file with 96 additions and 1 deletion.
97 changes: 96 additions & 1 deletion cmd/ctr/ctr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package ctr

import (
"fmt"
"path/filepath"
"strings"

"github.com/containerd/containerd/cmd/ctr/app"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/urfave/cli"
)

Expand All @@ -12,7 +17,7 @@ func NewCtrCommand() *cobra.Command {
internalArgs := []string{"ctr"}
ctrCommand := &cobra.Command{
Use: originalCtr.Name,
Short: originalCtr.Usage,
Short: "containerd CLI",
Long: originalCtr.Description,
RunE: func(cmd *cobra.Command, args []string) error {
internalArgs = append(internalArgs, args...)
Expand All @@ -35,15 +40,105 @@ func configureCommand(originalCmd *cli.App, parentCmd *cobra.Command, cmd cli.Co
Short: cmd.Usage,
Long: cmd.Description,
RunE: func(cmd *cobra.Command, args []string) error {
flags := cmdFlagsToArgs(cmd)
internalArgs = append(internalArgs, flags...)
internalArgs = append(internalArgs, args...)
return originalCmd.Run(internalArgs)
},
}
parentCmd.AddCommand(command)

configureFlags(command, cmd.Flags)

for _, subCmd := range cmd.Subcommands {
configureCommand(originalCmd, command, subCmd, internalArgs)
}
}

func configureFlags(cmd *cobra.Command, flags []cli.Flag) {
for _, f := range flags {
switch flag := f.(type) {
case cli.BoolFlag:
if name, shorthand := splitNames(flag.Name); shorthand == "" {
cmd.Flags().Bool(name, false, flag.Usage)
} else {
cmd.Flags().BoolP(name, shorthand, false, flag.Usage)
}
case cli.IntFlag:
if name, shorthand := splitNames(flag.Name); shorthand == "" {
cmd.Flags().Int(name, flag.Value, flag.Usage)
} else {
cmd.Flags().IntP(name, shorthand, flag.Value, flag.Usage)
}
case cli.Uint64Flag:
if name, shorthand := splitNames(flag.Name); shorthand == "" {
cmd.Flags().Uint64(name, flag.Value, flag.Usage)
} else {
cmd.Flags().Uint64P(name, shorthand, flag.Value, flag.Usage)
}
case cli.Float64Flag:
if name, shorthand := splitNames(flag.Name); shorthand == "" {
cmd.Flags().Float64(name, flag.Value, flag.Usage)
} else {
cmd.Flags().Float64P(name, shorthand, flag.Value, flag.Usage)
}
case cli.StringFlag:
if name, shorthand := splitNames(flag.Name); shorthand == "" {
cmd.Flags().String(name, flag.Value, flag.Usage)
} else {
cmd.Flags().StringP(name, shorthand, flag.Value, flag.Usage)
}
case cli.StringSliceFlag:
if name, shorthand := splitNames(flag.Name); shorthand == "" {
cmd.Flags().StringSlice(name, getValueSafely(flag.Value), flag.Usage)
} else {
cmd.Flags().StringSliceP(name, shorthand, getValueSafely(flag.Value), flag.Usage)
}
case cli.DurationFlag:
if name, shorthand := splitNames(flag.Name); shorthand == "" {
cmd.Flags().Duration(name, flag.Value, flag.Usage)
} else {
cmd.Flags().DurationP(name, shorthand, flag.Value, flag.Usage)
}
}
}
}

func splitNames(name string) (string, string) {
names := strings.SplitN(name, ",", 2)
for i, name := range names {
names[i] = strings.TrimSpace(name)
}

if len(names) == 2 {
return names[0], names[1]
} else {
return names[0], ""
}
}

func getValueSafely(s *cli.StringSlice) []string {
if s != nil {
return s.Value()
} else {
return []string{}
}
}

func cmdFlagsToArgs(cmd *cobra.Command) []string {
var flagsAndVals []string
// Use visitor to collect all flags and vals into slice
cmd.Flags().Visit(func(f *pflag.Flag) {
val := f.Value.String()
switch f.Value.Type() {
case "stringSlice", "stringToString":
flagsAndVals = append(flagsAndVals, fmt.Sprintf(`--%s="%s"`, f.Name, strings.Trim(val, "[]")))
default:
if f.Name == "data-dir" || f.Name == "token-file" || f.Name == "config-file" {
val, _ = filepath.Abs(val)
}
flagsAndVals = append(flagsAndVals, fmt.Sprintf("--%s=%s", f.Name, val))
}
})
return flagsAndVals
}

0 comments on commit 59ab76d

Please sign in to comment.