Skip to content

Commit

Permalink
remove cmdContext, parse env in Cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
kkga committed Sep 12, 2021
1 parent 9e8862c commit 8fe88b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 91 deletions.
19 changes: 15 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"errors"
"flag"
"fmt"
"os"
"strings"
)

type Runner interface {
Init([]string, CmdContext) error
Init([]string) error
Run() error
Name() string
Alias() []string
Expand All @@ -28,14 +29,22 @@ type Cmd struct {
bufferReq bool
}

type EnvContext struct {
Session string `json:"session"`
Client string `json:"client"`
}

func (c *Cmd) Run() error { return nil }
func (c *Cmd) Name() string { return c.fs.Name() }
func (c *Cmd) Alias() []string { return c.alias }

func (c *Cmd) Init(args []string, cc CmdContext) error {
c.session, c.client = cc.Session, cc.Client
func (c *Cmd) Init(args []string) error {
env := EnvContext{
Session: os.Getenv("KKS_SESSION"),
Client: os.Getenv("KKS_CLIENT"),
}

c.fs.Usage = c.usage
c.session, c.client = env.Session, env.Client

if err := c.fs.Parse(args); err != nil {
return err
Expand All @@ -48,6 +57,8 @@ func (c *Cmd) Init(args []string, cc CmdContext) error {
return errors.New("no client in context")
}

c.fs.Usage = c.usage

return nil
}

Expand Down
82 changes: 0 additions & 82 deletions cmd/cmdContext.go

This file was deleted.

10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func Root(args []string) error {

subcommand := os.Args[1]

cmdCtx, err := NewCmdContext()
if err != nil {
return err
}
// cmdCtx, err := NewCmdContext()
// if err != nil {
// return err
// }

for _, cmd := range cmds {
if cmd.Name() == subcommand || containsString(cmd.Alias(), subcommand) {
if err := cmd.Init(os.Args[2:], *cmdCtx); err != nil {
if err := cmd.Init(os.Args[2:]); err != nil {
return err
}
return cmd.Run()
Expand Down

0 comments on commit 8fe88b6

Please sign in to comment.