Skip to content

Commit

Permalink
Add --variables param
Browse files Browse the repository at this point in the history
  • Loading branch information
outdead committed Feb 11, 2024
1 parent c28c2e2 commit d52bb14
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
20 changes: 19 additions & 1 deletion internal/config/session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package config

import "time"
import (
"encoding/json"
"fmt"
"io"
"time"
)

// Allowed protocols.
const (
Expand All @@ -26,4 +31,17 @@ type Session struct {
Type string `json:"type" yaml:"type"`
SkipErrors bool `json:"skip_errors" yaml:"skip_errors"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Variables bool `json:"-" yaml:"-"`
}

func (s *Session) Print(w io.Writer) error {
js, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}

_, _ = fmt.Fprint(w, "Print session:\n")
_, _ = fmt.Fprint(w, string(js)+"\n")

return nil
}
22 changes: 22 additions & 0 deletions internal/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (executor *Executor) NewSession(c *cli.Context) (*config.Session, error) {
Log: c.String("log"),
SkipErrors: c.Bool("skip"),
Timeout: c.Duration("timeout"),
Variables: c.Bool("variables"),
}

if ses.Address != "" && ses.Password != "" {
Expand Down Expand Up @@ -307,6 +308,12 @@ func (executor *Executor) getFlags() []cli.Flag {
Usage: "Set dial and execute timeout",
Value: config.DefaultTimeout,
},
&cli.BoolFlag{
Name: "variables",
Aliases: []string{"V"},
Usage: "Print stored variables and exit",
Value: false,
},
}
}

Expand All @@ -317,6 +324,12 @@ func (executor *Executor) action(c *cli.Context) error {
return err
}

if ses.Variables {
executor.printVariables(ses, c)

return nil
}

commands := c.Args().Slice()
if len(commands) == 0 {
return executor.Interactive(executor.r, executor.w, ses)
Expand Down Expand Up @@ -362,3 +375,12 @@ func (executor *Executor) execute(w io.Writer, ses *config.Session, command stri

return nil
}

func (executor *Executor) printVariables(ses *config.Session, c *cli.Context) {
_, _ = fmt.Fprint(executor.w, "Got Print Variables param.\n")
_ = ses.Print(executor.w)

_, _ = fmt.Fprint(executor.w, "\nPrint other variables:\n")
_, _ = fmt.Fprintf(executor.w, "Path to config file (if used): %s\n", c.String("config"))
_, _ = fmt.Fprintf(executor.w, "Cofig environment: %s\n", c.String("env"))
}

0 comments on commit d52bb14

Please sign in to comment.