Skip to content

Commit

Permalink
Merge pull request #1058 from ripienaar/server_generate
Browse files Browse the repository at this point in the history
Adds a generic server scaffolding tool
  • Loading branch information
ripienaar committed May 16, 2024
2 parents 84fff6c + a1b5b31 commit bf4d8ef
Show file tree
Hide file tree
Showing 55 changed files with 1,192 additions and 554 deletions.
6 changes: 3 additions & 3 deletions cli/account_command.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2023 The NATS Authors
// Copyright 2019-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -405,12 +405,12 @@ func (c *actCmd) infoAction(_ *fisk.ParseContext) error {
var ui *server.UserInfo
if serverMinVersion(nc.ConnectedServerVersion(), 2, 10, 0) {
subj := "$SYS.REQ.USER.INFO"
if opts.Trace {
if opts().Trace {
log.Printf(">>> %s: {}\n", subj)
}
resp, err := nc.Request("$SYS.REQ.USER.INFO", nil, time.Second)
if err == nil {
if opts.Trace {
if opts().Trace {
log.Printf("<<< %s", string(resp.Data))
}
var res = struct {
Expand Down
13 changes: 6 additions & 7 deletions cli/auth_account_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
au "github.com/nats-io/natscli/internal/auth"
"github.com/nats-io/natscli/internal/util"
"io"
"net/url"
"os"
Expand Down Expand Up @@ -279,7 +280,7 @@ func configureAuthAccountCommand(auth commandHost) {
}

func (c *authAccountCommand) selectAccount(pick bool) (*ab.AuthImpl, ab.Operator, ab.Account, error) {
auth, oper, acct, err := selectOperatorAccount(c.operatorName, c.accountName, pick)
auth, oper, acct, err := au.SelectOperatorAccount(c.operatorName, c.accountName, pick)
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -291,7 +292,7 @@ func (c *authAccountCommand) selectAccount(pick bool) (*ab.AuthImpl, ab.Operator
}

func (c *authAccountCommand) selectOperator(pick bool) (*ab.AuthImpl, ab.Operator, error) {
auth, oper, err := selectOperator(c.operatorName, pick, true)
auth, oper, err := au.SelectOperator(c.operatorName, pick, true)
if err != nil {
return nil, nil, err
}
Expand All @@ -307,7 +308,7 @@ func (c *authAccountCommand) queryAction(_ *fisk.ParseContext) error {
return err
}

_, oper, err := selectOperator(c.operatorName, true, true)
_, oper, err := au.SelectOperator(c.operatorName, true, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -490,7 +491,7 @@ func (c *authAccountCommand) skAddAction(_ *fisk.ParseContext) error {
}

if c.skRole == "" {
err := askOne(&survey.Input{
err := util.AskOne(&survey.Input{
Message: "Role Name",
Help: "The role to associate with this key",
}, &c.skRole, survey.WithValidator(survey.Required))
Expand Down Expand Up @@ -667,11 +668,9 @@ func (c *authAccountCommand) editAction(_ *fisk.ParseContext) error {
jsl := limits.JetStreamLimits
if c.storeMaxString == "" {
c.storeMaxString = strconv.Itoa(int(jsl.DiskStorage))
log.Printf("set storeMaxString to %s\n", c.storeMaxStreamString)
}
if c.memMaxString == "" {
c.memMaxString = strconv.Itoa(int(jsl.MemoryStorage))
log.Printf("set memMaxString to %s\n", c.memMaxString)
}
if c.memMaxStreamString == "" {
c.memMaxStreamString = strconv.Itoa(int(jsl.MemoryMaxStreamBytes))
Expand Down Expand Up @@ -894,7 +893,7 @@ func (c *authAccountCommand) addAction(_ *fisk.ParseContext) error {
}

if c.accountName == "" {
err := askOne(&survey.Input{
err := util.AskOne(&survey.Input{
Message: "Account Name",
Help: "A unique name for the Account being added",
}, &c.accountName, survey.WithValidator(survey.Required))
Expand Down
18 changes: 16 additions & 2 deletions cli/auth_account_exports.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// Copyright 2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"fmt"
"github.com/nats-io/natscli/internal/util"
"io"
"os"
"sort"
Expand Down Expand Up @@ -206,10 +220,10 @@ func (c *authAccountCommand) exportInfoAction(_ *fisk.ParseContext) error {
return fmt.Errorf("no exports defined")
}

err = askOne(&survey.Select{
err = util.AskOne(&survey.Select{
Message: "Select an Export",
Options: known,
PageSize: selectPageSize(len(known)),
PageSize: util.SelectPageSize(len(known)),
}, &c.subject)
if err != nil {
return err
Expand Down
18 changes: 16 additions & 2 deletions cli/auth_account_imports.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
// Copyright 2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"fmt"
au "github.com/nats-io/natscli/internal/auth"
"github.com/nats-io/natscli/internal/util"
"io"
"os"
"sort"
Expand Down Expand Up @@ -235,10 +249,10 @@ func (c *authAccountCommand) importInfoAction(_ *fisk.ParseContext) error {
return fmt.Errorf("no imports defined")
}

err = askOne(&survey.Select{
err = util.AskOne(&survey.Select{
Message: "Select an Import",
Options: known,
PageSize: selectPageSize(len(known)),
PageSize: util.SelectPageSize(len(known)),
}, &c.subject)
if err != nil {
return err
Expand Down
99 changes: 0 additions & 99 deletions cli/auth_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@

package cli

import (
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
au "github.com/nats-io/natscli/internal/auth"
iu "github.com/nats-io/natscli/internal/util"
ab "github.com/synadia-io/jwt-auth-builder.go"
)

func configureAuthCommand(app commandHost) {
auth := app.Command("auth", "NATS Decentralized Authentication")

Expand All @@ -39,93 +30,3 @@ func configureAuthCommand(app commandHost) {
func init() {
registerCommand("auth", 0, configureAuthCommand)
}

func selectOperator(operatorName string, pick bool, useSelected bool) (*ab.AuthImpl, ab.Operator, error) {
auth, err := au.GetAuthBuilder()
if err != nil {
return nil, nil, err
}

operators := auth.Operators().List()

// if we have the selected operator file we put that as the name if no name were given
if operatorName == "" && useSelected {
cfg, err := loadConfig()
if err == nil {
operatorName = cfg.SelectedOperator
}
}

if operatorName == "" || !au.IsAuthItemKnown(operators, operatorName) {
if !pick {
return nil, nil, fmt.Errorf("unknown operator: %v", operatorName)
}

if len(operators) == 1 {
return auth, operators[0], nil
}

if !iu.IsTerminal() {
return nil, nil, fmt.Errorf("cannot pick an Operator without a terminal and no operator name supplied")
}

names := au.SortedAuthNames(auth.Operators().List())
if len(names) == 0 {
return nil, nil, fmt.Errorf("no operators found")
}

err = askOne(&survey.Select{
Message: "Select an Operator",
Options: names,
PageSize: selectPageSize(len(names)),
}, &operatorName)
if err != nil {
return nil, nil, err
}
}

op, err := auth.Operators().Get(operatorName)
if op == nil || errors.Is(err, ab.ErrNotFound) {
return nil, nil, fmt.Errorf("unknown operator: %v", operatorName)
} else if err != nil {
return nil, nil, err
}

return auth, op, nil
}

func selectOperatorAccount(operatorName string, accountName string, pick bool) (*ab.AuthImpl, ab.Operator, ab.Account, error) {
auth, operator, err := selectOperator(operatorName, pick, true)
if err != nil {
return nil, nil, nil, err
}

if accountName == "" || !au.IsAuthItemKnown(operator.Accounts().List(), accountName) {
if !pick {
return nil, nil, nil, fmt.Errorf("unknown Account: %v", accountName)
}

if !iu.IsTerminal() {
return nil, nil, nil, fmt.Errorf("cannot pick an Account without a terminal and no Account name supplied")
}

names := au.SortedAuthNames(operator.Accounts().List())
err = askOne(&survey.Select{
Message: "Select an Account",
Options: names,
PageSize: selectPageSize(len(names)),
}, &accountName)
if err != nil {
return nil, nil, nil, err
}
}

acct, err := operator.Accounts().Get(accountName)
if acct == nil || errors.Is(err, ab.ErrNotFound) {
return nil, nil, nil, fmt.Errorf("unknown Account: %v", accountName)
} else if err != nil {
return nil, nil, nil, err
}

return auth, operator, acct, nil
}
14 changes: 7 additions & 7 deletions cli/auth_operator_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/choria-io/appbuilder/forms"
"github.com/choria-io/scaffold/forms"
"github.com/ghodss/yaml"
au "github.com/nats-io/natscli/internal/auth"
iu "github.com/nats-io/natscli/internal/util"
Expand Down Expand Up @@ -117,7 +117,7 @@ func configureAuthOperatorCommand(auth commandHost) {
}

func (c *authOperatorCommand) generateAction(_ *fisk.ParseContext) error {
_, oper, err := selectOperator(c.operatorName, true, false)
_, oper, err := au.SelectOperator(c.operatorName, true, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -169,17 +169,17 @@ func (c *authOperatorCommand) generateAction(_ *fisk.ParseContext) error {
}

func (c *authOperatorCommand) selectAction(_ *fisk.ParseContext) error {
_, oper, err := selectOperator(c.operatorName, true, false)
_, oper, err := au.SelectOperator(c.operatorName, true, false)
if err != nil {
return err
}

cfg, err := loadConfig()
cfg, err := iu.LoadConfig()
if err != nil {
return err
}
cfg.SelectedOperator = oper.Name()
err = saveConfig(cfg)
err = iu.SaveConfig(cfg)
if err != nil {
return err
}
Expand All @@ -190,7 +190,7 @@ func (c *authOperatorCommand) selectAction(_ *fisk.ParseContext) error {
}

func (c *authOperatorCommand) selectOperator(pick bool) (*ab.AuthImpl, ab.Operator, error) {
auth, oper, err := selectOperator(c.operatorName, pick, true)
auth, oper, err := au.SelectOperator(c.operatorName, pick, true)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -519,7 +519,7 @@ func (c *authOperatorCommand) lsAction(_ *fisk.ParseContext) error {

func (c *authOperatorCommand) addAction(_ *fisk.ParseContext) error {
if c.operatorName == "" {
err := askOne(&survey.Input{
err := iu.AskOne(&survey.Input{
Message: "Operator Name",
Help: "A unique name for the Operator being added",
}, &c.operatorName, survey.WithValidator(survey.Required))
Expand Down
16 changes: 9 additions & 7 deletions cli/auth_user_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ package cli
import (
"errors"
"fmt"
au "github.com/nats-io/natscli/internal/auth"
"io"
"os"
"sort"
"strconv"
"time"

au "github.com/nats-io/natscli/internal/auth"
iu "github.com/nats-io/natscli/internal/util"

"github.com/AlecAivazis/survey/v2"
"github.com/choria-io/fisk"
ab "github.com/synadia-io/jwt-auth-builder.go"
Expand Down Expand Up @@ -157,7 +159,7 @@ func (c *authUserCommand) editAction(_ *fisk.ParseContext) error {
}

func (c *authUserCommand) credAction(_ *fisk.ParseContext) error {
if !c.force && fileExists(c.credFile) {
if !c.force && iu.FileExists(c.credFile) {
return fmt.Errorf("file %s already exist", c.credFile)
}

Expand Down Expand Up @@ -309,10 +311,10 @@ func (c *authUserCommand) pickUser(acct ab.Account) error {
}
sort.Strings(names)

err := askOne(&survey.Select{
err := iu.AskOne(&survey.Select{
Message: "Select a User",
Options: names,
PageSize: selectPageSize(len(names)),
PageSize: iu.SelectPageSize(len(names)),
}, &c.userName)
if err != nil {
return err
Expand Down Expand Up @@ -343,7 +345,7 @@ func (c *authUserCommand) infoAction(_ *fisk.ParseContext) error {
}

func (c *authUserCommand) addAction(_ *fisk.ParseContext) error {
auth, _, acct, err := selectOperatorAccount(c.operatorName, c.accountName, true)
auth, _, acct, err := au.SelectOperatorAccount(c.operatorName, c.accountName, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -504,7 +506,7 @@ func (c *authUserCommand) showUser(user ab.User, acct ab.Account) (string, error
}

func (c *authUserCommand) selectAccount(pick bool) (*ab.AuthImpl, ab.Operator, ab.Account, error) {
auth, oper, acct, err := selectOperatorAccount(c.operatorName, c.accountName, pick)
auth, oper, acct, err := au.SelectOperatorAccount(c.operatorName, c.accountName, pick)
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -516,7 +518,7 @@ func (c *authUserCommand) selectAccount(pick bool) (*ab.AuthImpl, ab.Operator, a
}

func (c *authUserCommand) writeCred(user ab.User, credFile string, force bool) error {
if !force && fileExists(credFile) {
if !force && iu.FileExists(credFile) {
return fmt.Errorf("file %s already exist", credFile)
}

Expand Down
Loading

0 comments on commit bf4d8ef

Please sign in to comment.