Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI Upgrade Subcommand #384

Open
wants to merge 3 commits into
base: vnext
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/nex/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ type Globals struct {
GlobalLogger `prefix:"logger." group:"Logger Configuration"`
GlobalNats `prefix:"nats." group:"NATS Configuration"`

Config kong.ConfigFlag `help:"Configuration file to load" placeholder:"./nex.config.json"`
Version kong.VersionFlag `help:"Print version information"`
Namespace string `env:"NEX_NAMESPACE" placeholder:"default" help:"Specifies namespace when running nex commands"`
Check bool `help:"Print the current configuration"`
Config kong.ConfigFlag `help:"Configuration file to load" placeholder:"./nex.config.json"`
Version kong.VersionFlag `help:"Print version information"`
Namespace string `env:"NEX_NAMESPACE" placeholder:"default" help:"Specifies namespace when running nex commands"`
jordan-rash marked this conversation as resolved.
Show resolved Hide resolved
Check bool `help:"Print the current values of all options without running a command"`
DisableUpgradeCheck bool `env:"NEX_DISABLE_UPGRADE_CHECK" name:"disable-upgrade-check" help:"Disable the upgrade check"`
AutoUpgrade bool `env:"NEX_AUTO_UPGRADE" name:"auto-upgrade" help:"Automatically upgrade the nex CLI when a new version is available"`
}

type GlobalLogger struct {
Expand Down
23 changes: 23 additions & 0 deletions cmd/nex/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "context"

func checkVer(globals *Globals) error {
if globals.Check {
return nil
}
if !globals.DisableUpgradeCheck {
iVer, err := versionCheck()
if err != nil {
return err
}
if globals.AutoUpgrade {
u := Upgrade{
installVersion: iVer,
}
globals.Check = false
return u.Run(context.Background(), globals)
}
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/nex/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/nats-io/nats.go"
)

func configureLogger(cfg Globals, nc *nats.Conn, serverPublicKey string) *slog.Logger {
func configureLogger(cfg *Globals, nc *nats.Conn, serverPublicKey string) *slog.Logger {
var handlerOpts []shandler.HandlerOption

switch cfg.LogLevel {
Expand Down
5 changes: 2 additions & 3 deletions cmd/nex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ func main() {
"versionOnly": VERSION,
"defaultResourcePath": userResourcePath,
},
kong.BindTo(context.Background(), (*context.Context)(nil)),
kong.Bind(&nex.Globals),
)

ctx.BindTo(context.Background(), (*context.Context)(nil))
ctx.BindTo(nex.Globals, (*Globals)(nil))

err = ctx.Run()
ctx.FatalIfErrorf(err)
}
20 changes: 11 additions & 9 deletions cmd/nex/main_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -12,9 +10,12 @@ import (
)

func TestCLISimple(t *testing.T) {
nex := new(NexCLI)
nex := NexCLI{}

parser := kong.Must(nex, kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": "."}))
parser := kong.Must(&nex,
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": "."}),
kong.Bind(&nex.Globals),
)
kp, err := nkeys.CreatePair(nkeys.PrefixByteServer)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -63,18 +64,19 @@ func TestCLIWithConfig(t *testing.T) {
f.WriteString(config)
defer f.Close()

nex := new(NexCLI)
parser := kong.Must(nex, kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": "."}), kong.Configuration(kong.JSON, f.Name()))
nex := NexCLI{}
parser := kong.Must(&nex,
kong.Vars(map[string]string{"versionOnly": "testing", "defaultResourcePath": "."}),
kong.Configuration(kong.JSON, f.Name()),
kong.Bind(&nex.Globals),
)
parser.LoadConfig(f.Name())

_, err = parser.Parse([]string{"node", "up", "--config", f.Name()})
if err != nil {
t.Fatal(err)
}

jsonData, _ := json.MarshalIndent(nex, "", " ")
fmt.Println(string(jsonData))

if string(nex.Globals.Config) != f.Name() {
t.Fatal("Expected config to be loaded")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/nex/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/nats-io/nats.go"
)

func configureNatsConnection(cfg Globals) (*nats.Conn, error) {
func configureNatsConnection(cfg *Globals) (*nats.Conn, error) {
if cfg.Check {
return nil, nil
}
Expand Down
31 changes: 25 additions & 6 deletions cmd/nex/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type Preflight struct {
GithubPAT string `optional:"" help:"GitHub Personal Access Token. Can be provided if rate limits are hit pulling data from Github" placeholder:"ghp_abc123..."`
}

func (Preflight) AfterApply(globals *Globals) error {
return checkVer(globals)
}

func (p Preflight) Validate() error {
var errs error
if p.InstallVersion != "" {
Expand Down Expand Up @@ -81,11 +85,10 @@ func (p Preflight) Validate() error {
return errs
}

func (p Preflight) Run(ctx context.Context, globals Globals) error {
func (p Preflight) Run(ctx context.Context, globals *Globals) error {
if globals.Check {
return printTable("Node Preflight Configuration", append(globals.Table(), p.Table()...)...)
}
fmt.Println("run preflight")
return nil
}

Expand All @@ -95,6 +98,10 @@ type LameDuck struct {
Label map[string]string `optional:"" help:"Put all nodes with label in lameduck. Only 1 label allowed" placeholder:"nex.nexus=mynexus"`
}

func (LameDuck) AfterApply(globals *Globals) error {
return checkVer(globals)
}

func (l LameDuck) Validate() error {
if l.NodeID == "" && len(l.Label) == 0 {
return errors.New("must provide a node ID or label")
Expand All @@ -118,7 +125,7 @@ func (l LameDuck) Validate() error {
return nil
}

func (l LameDuck) Run(ctx context.Context, globals Globals) error {
func (l LameDuck) Run(ctx context.Context, globals *Globals) error {
if globals.Check {
return printTable("Node Lameduck Configuration", append(globals.Table(), l.Table()...)...)
}
Expand All @@ -132,11 +139,15 @@ type List struct {
JSON bool `optional:"" help:"Output in JSON format"`
}

func (List) AfterApply(globals *Globals) error {
return checkVer(globals)
}

func (l List) Validate() error {
return nil
}

func (l List) Run(ctx context.Context, globals Globals) error {
func (l List) Run(ctx context.Context, globals *Globals) error {
if globals.Check {
return printTable("Node List Configuration", append(globals.Table(), l.Table()...)...)
}
Expand All @@ -150,6 +161,10 @@ type Info struct {
JSON bool `optional:"" help:"Output in JSON format"`
}

func (Info) AfterApply(globals *Globals) error {
return checkVer(globals)
}

func (i Info) Validate() error {
var errs error
if !nkeys.IsValidPublicServerKey(i.NodeID) {
Expand All @@ -158,7 +173,7 @@ func (i Info) Validate() error {
return errs
}

func (i Info) Run(ctx context.Context, globals Globals) error {
func (i Info) Run(ctx context.Context, globals *Globals) error {
if globals.Check {
return printTable("Node Info Configuration", append(globals.Table(), i.Table()...)...)
}
Expand All @@ -179,6 +194,10 @@ type Up struct {
OtelConfig OtelConfig `embed:"" prefix:"otel." group:"OpenTelemetry Configuration"`
}

func (u *Up) AfterApply(globals *Globals) error {
return checkVer(globals)
}

func (u Up) Validate() error {
var errs error
if u.WorkloadTypes == nil || len(u.WorkloadTypes) < 1 {
Expand All @@ -187,7 +206,7 @@ func (u Up) Validate() error {
return errs
}

func (u Up) Run(ctx context.Context, globals Globals, n *Node) error {
func (u Up) Run(ctx context.Context, globals *Globals, n *Node) error {
if globals.Check {
return printTable("Node Up Configuration", append(globals.Table(), u.Table()...)...)
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/nex/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func printTable(title string, in ...table.Row) error {
func (g Globals) Table() []table.Row {
return []table.Row{
{"Config File", g.Config, reflect.TypeOf(g.Config).String()},
{"Disable Upgrade Check", g.DisableUpgradeCheck, reflect.TypeOf(g.DisableUpgradeCheck).String()},
{"Enable Auto Upgrade ", g.AutoUpgrade, reflect.TypeOf(g.AutoUpgrade).String()},
{"Nex Namespace", g.Namespace, reflect.TypeOf(g.Namespace).String()},
{"NATS Server", g.NatsServers, reflect.TypeOf(g.NatsServers).String()},
{"NATS Context", g.NatsContext, reflect.TypeOf(g.NatsContext).String()},
Expand Down Expand Up @@ -100,3 +102,9 @@ func (i Info) Table() []table.Row {
{"JSON Output", i.JSON, reflect.TypeOf(i.JSON).String()},
}
}

func (u Upgrade) Table() []table.Row {
return []table.Row{
{"Git Tag", u.GitTag, reflect.TypeOf(u.GitTag).String()},
}
}
Loading
Loading