Skip to content

Commit

Permalink
Merge pull request #95 from briandowns/flag_values_not_being_set
Browse files Browse the repository at this point in the history
Flag values not being set
  • Loading branch information
briandowns authored Jul 20, 2020
2 parents e2d3656 + a2679b4 commit f0d3a06
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 29 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ require (
github.com/rancher/spur v0.0.0-20200617165101-8702c8e4ce7a
github.com/rancher/wrangler v0.6.1
github.com/sirupsen/logrus v1.4.2
github.com/urfave/cli v1.22.2
google.golang.org/grpc v1.26.0
k8s.io/api v0.18.0
k8s.io/apimachinery v0.18.0
Expand Down
21 changes: 12 additions & 9 deletions pkg/cli/cmds/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (

var (
k3sAgentBase = mustCmdFromK3S(cmds.NewAgentCommand(AgentRun), map[string]*K3SFlagOption{
"config": copy,
"debug": copy,
"v": hide,
"vmodule": hide,
"log": hide,
"alsologtostderr": hide,
"data-dir": copy,
"config": copy,
"debug": copy,
"v": hide,
"vmodule": hide,
"log": hide,
"alsologtostderr": hide,
"data-dir": {
Usage: "(data) Folder to hold state",
Default: rke2Path,
},
"token": copy,
"token-file": copy,
"disable-selinux": drop,
Expand Down Expand Up @@ -46,6 +49,6 @@ func NewAgentCommand() *cli.Command {
return cmd
}

func AgentRun(app *cli.Context) error {
return rke2.Agent(app, config)
func AgentRun(ctx *cli.Context) error {
return rke2.Agent(ctx, config)
}
74 changes: 60 additions & 14 deletions pkg/cli/cmds/k3sopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ func mustCmdFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) *cli.C
return cmd
}

func flagValue(flag cli.Flag, field string) reflect.Value {
return reflect.Indirect(reflect.ValueOf(flag)).FieldByName(field)
}

func commandFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) (*cli.Command, error) {
var (
newFlags []cli.Flag
Expand All @@ -66,21 +62,14 @@ func commandFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) (*cli.
}

if opt.Usage != "" {
if v := flagValue(flag, "Usage"); v.IsValid() {
v.SetString(opt.Usage)
}
flagSetUsage(flag, opt)
}
if opt.Default != "" {
if v := flagValue(flag, "Default"); v.IsValid() {
v.SetString(opt.Default)
}
flagSetDefault(flag, opt)
}
if opt.Hide {
if v := flagValue(flag, "Hidden"); v.IsValid() {
v.SetBool(true)
}
flagSetHide(flag, opt)
}

newFlags = append(newFlags, flag)
}

Expand All @@ -94,3 +83,60 @@ func commandFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) (*cli.
cmd.Flags = newFlags
return cmd, errs.Err()
}

// flagSetUsage receives a flag and a K3S flag option, parses
// both and sets the necessary fields based on the underlying
// flag type.
func flagSetUsage(flag cli.Flag, opt *K3SFlagOption) {
v := reflect.ValueOf(flag).Elem()
if v.CanSet() {
switch t := flag.(type) {
case *cli.StringFlag:
t.Usage = opt.Usage
case *cli.StringSliceFlag:
t.Usage = opt.Usage
case *cli.BoolFlag:
t.Usage = opt.Usage
}
}
}

// flagSetDefault receives a flag and a K3S flag option, parses
// both and sets the necessary fields based on the underlying
// flag type.
func flagSetDefault(flag cli.Flag, opt *K3SFlagOption) {
v := reflect.ValueOf(flag).Elem()
if v.CanSet() {
switch t := flag.(type) {
case *cli.StringFlag:
t.DefaultText = opt.Default
t.Destination = &opt.Default
t.Value = opt.Default
case *cli.StringSliceFlag:
t.DefaultText = opt.Default
t.Destination = &([]string{opt.Default})
t.Value = []string{opt.Default}
case *cli.BoolFlag:
t.DefaultText = opt.Default
t.Destination = &opt.Hide
t.Value = opt.Hide
}
}
}

// flagSetHide receives a flag and a K3S flag option, parses
// both and sets the necessary fields based on the underlying
// flag type.
func flagSetHide(flag cli.Flag, opt *K3SFlagOption) {
v := reflect.ValueOf(flag).Elem()
if v.CanSet() {
switch t := flag.(type) {
case *cli.StringFlag:
t.Hidden = opt.Hide
case *cli.StringSliceFlag:
t.Hidden = opt.Hide
case *cli.BoolFlag:
t.Hidden = opt.Hide
}
}
}
4 changes: 2 additions & 2 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ func NewServerCommand() *cli.Command {
return cmd
}

func ServerRun(app *cli.Context) error {
return rke2.Server(app, config)
func ServerRun(ctx *cli.Context) error {
return rke2.Server(ctx, config)
}
12 changes: 9 additions & 3 deletions pkg/rke2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rke2
import (
"os"
"path/filepath"
"strings"

"github.com/rancher/k3s/pkg/agent/config"
"github.com/rancher/k3s/pkg/cli/agent"
Expand Down Expand Up @@ -43,9 +44,14 @@ func Agent(ctx *cli.Context, cfg Config) error {
}

func setup(ctx *cli.Context, cfg Config) error {
dataDir := cmds.ServerConfig.DataDir
if dataDir == "" {
dataDir = cmds.AgentConfig.DataDir
var dataDir string
for _, f := range ctx.Command.Flags {
switch t := f.(type) {
case *cli.StringFlag:
if strings.Contains(t.Name, "data-dir") {
dataDir = t.DefaultText
}
}
}

images := images.New(cfg.Repo)
Expand Down

0 comments on commit f0d3a06

Please sign in to comment.