Skip to content

Commit

Permalink
Merge pull request #1273 from smallstep/herman/use-renamed-cli-utils-…
Browse files Browse the repository at this point in the history
…module

Use `github.com/smallstep/cli-utils` and reorder imports
  • Loading branch information
hslatman authored Sep 30, 2024
2 parents bec3072 + 24f94b0 commit e3c3f20
Show file tree
Hide file tree
Showing 136 changed files with 785 additions and 511 deletions.
98 changes: 54 additions & 44 deletions cmd/step/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ package main
import (
"errors"
"fmt"
"io"
"os"
"reflect"
"regexp"
"strings"
"time"

"github.com/urfave/cli"

"github.com/smallstep/certificates/ca"
"github.com/smallstep/cli-utils/command"
"github.com/smallstep/cli-utils/step"
"github.com/smallstep/cli-utils/ui"
"github.com/smallstep/cli-utils/usage"
"go.step.sm/crypto/jose"
"go.step.sm/crypto/pemutil"

"github.com/smallstep/cli/command/version"
"github.com/smallstep/cli/internal/plugin"
"github.com/smallstep/cli/utils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/command"
"go.step.sm/cli-utils/step"
"go.step.sm/cli-utils/ui"
"go.step.sm/cli-utils/usage"
"go.step.sm/crypto/jose"
"go.step.sm/crypto/pemutil"

// Enabled cas interfaces.
_ "github.com/smallstep/certificates/cas/cloudcas"
_ "github.com/smallstep/certificates/cas/softcas"
_ "github.com/smallstep/certificates/cas/stepcas"

// Enabled commands
_ "github.com/smallstep/cli/command/api"
Expand All @@ -35,11 +43,6 @@ import (
_ "github.com/smallstep/cli/command/oauth"
_ "github.com/smallstep/cli/command/path"
_ "github.com/smallstep/cli/command/ssh"

// Enabled cas interfaces.
_ "github.com/smallstep/certificates/cas/cloudcas"
_ "github.com/smallstep/certificates/cas/softcas"
_ "github.com/smallstep/certificates/cas/stepcas"
)

// Version is set by an LDFLAG at build time representing the git tag or commit
Expand All @@ -64,6 +67,42 @@ func main() {

defer panicHandler()

// create new instance of app
app := newApp(os.Stdout, os.Stderr)

if err := app.Run(os.Args); err != nil {
var messenger interface {
Message() string
}
if errors.As(err, &messenger) {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%+v\n\n%s", err, messenger.Message())
} else {
fmt.Fprintln(os.Stderr, messenger.Message())
fmt.Fprintln(os.Stderr, "Re-run with STEPDEBUG=1 for more info.")
}
} else {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%+v\n", err)
} else {
fmt.Fprintln(os.Stderr, err)
}
}
//nolint:gocritic // ignore exitAfterDefer error because the defer is required for recovery.
os.Exit(1)
}
}

func newApp(stdout, stderr io.Writer) *cli.App {
// Define default file writers and prompters for go.step.sm/crypto
pemutil.WriteFile = utils.WriteFile
pemutil.PromptPassword = func(msg string) ([]byte, error) {
return ui.PromptPassword(msg)
}
jose.PromptPassword = func(msg string) ([]byte, error) {
return ui.PromptPassword(msg)
}

// Override global framework components
cli.VersionPrinter = func(c *cli.Context) {
version.Command(c)
Expand Down Expand Up @@ -109,39 +148,10 @@ func main() {
}

// All non-successful output should be written to stderr
app.Writer = os.Stdout
app.ErrWriter = os.Stderr
app.Writer = stdout
app.ErrWriter = stderr

// Define default file writers and prompters for go.step.sm/crypto
pemutil.WriteFile = utils.WriteFile
pemutil.PromptPassword = func(msg string) ([]byte, error) {
return ui.PromptPassword(msg)
}
jose.PromptPassword = func(msg string) ([]byte, error) {
return ui.PromptPassword(msg)
}

if err := app.Run(os.Args); err != nil {
var messenger interface {
Message() string
}
if errors.As(err, &messenger) {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%+v\n\n%s", err, messenger.Message())
} else {
fmt.Fprintln(os.Stderr, messenger.Message())
fmt.Fprintln(os.Stderr, "Re-run with STEPDEBUG=1 for more info.")
}
} else {
if os.Getenv("STEPDEBUG") == "1" {
fmt.Fprintf(os.Stderr, "%+v\n", err)
} else {
fmt.Fprintln(os.Stderr, err)
}
}
//nolint:gocritic // ignore exitAfterDefer error because the defer is required for recovery.
os.Exit(1)
}
return app
}

func panicHandler() {
Expand Down
46 changes: 46 additions & 0 deletions cmd/step/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"bytes"
"regexp"
"testing"

"github.com/stretchr/testify/require"
)

func TestAppHasAllCommands(t *testing.T) {
app := newApp(&bytes.Buffer{}, &bytes.Buffer{})
require.NotNil(t, app)

require.Equal(t, "step", app.Name)
require.Equal(t, "step", app.HelpName)

var names = make([]string, 0, len(app.Commands))
for _, c := range app.Commands {
names = append(names, c.Name)
}
require.Equal(t, []string{
"help", "api", "path", "base64", "fileserver",
"certificate", "completion", "context", "crl",
"crypto", "oauth", "version", "ca", "beta", "ssh",
}, names)
}

const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"

var ansiRegex = regexp.MustCompile(ansi)

func TestAppRuns(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}

app := newApp(stdout, stderr)
require.NotNil(t, app)

err := app.Run([]string{"step"})
require.NoError(t, err)
require.Empty(t, stderr.Bytes())

output := ansiRegex.ReplaceAllString(stdout.String(), "")
require.Contains(t, output, "step -- plumbing for distributed systems")
}
6 changes: 3 additions & 3 deletions command/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ should exist within its own package if possible. For example, `version` and
Any package used by a command but does not contain explicit business logic
directly related to the command should exist in the top-level of this
repository. For example, the `github.com/smallstep/cli/flags` and
`go.step.sm/cli-utils/errs` package are used by many different commands and
`github.com/smallstep/cli-utils/errs` package are used by many different commands and
contain functionality for defining flags and creating/manipulating errors.

### Adding a Command
Expand Down Expand Up @@ -70,8 +70,8 @@ There are three packages which contain functionality to make writing commands ea

- `github.com/smallstep/cli/flags`
- `github.com/smallstep/cli/prompts`
- `go.step.sm/cli-utils/errs`
- `go.step.sm/cli-utils/usage`
- `github.com/smallstep/cli-utils/errs`
- `github.com/smallstep/cli-utils/usage`

The usage package is used to extend the default documentation provided by
`urfave/cli` by enabling us to document arguments, whether they are optional or
Expand Down
6 changes: 4 additions & 2 deletions command/api/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package api

import (
"github.com/smallstep/cli/command/api/token"
"github.com/urfave/cli"
"go.step.sm/cli-utils/command"

"github.com/smallstep/cli-utils/command"

"github.com/smallstep/cli/command/api/token"
)

func init() {
Expand Down
5 changes: 3 additions & 2 deletions command/api/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (

"github.com/google/uuid"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"
"go.step.sm/cli-utils/ui"

"github.com/smallstep/cli-utils/errs"
"github.com/smallstep/cli-utils/ui"
)

func createCommand() cli.Command {
Expand Down
6 changes: 4 additions & 2 deletions command/base64/base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/smallstep/cli/utils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/command"

"github.com/smallstep/cli-utils/command"

"github.com/smallstep/cli/utils"
)

func init() {
Expand Down
6 changes: 4 additions & 2 deletions command/beta/beta.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package beta

import (
"github.com/smallstep/cli/command/ca"
"github.com/urfave/cli"
"go.step.sm/cli-utils/command"

"github.com/smallstep/cli-utils/command"

"github.com/smallstep/cli/command/ca"
)

// init creates and registers the ca command
Expand Down
4 changes: 2 additions & 2 deletions command/ca/acme/eab/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/pkg/errors"
"github.com/urfave/cli"

"go.step.sm/cli-utils/errs"

adminAPI "github.com/smallstep/certificates/authority/admin/api"
"github.com/smallstep/cli-utils/errs"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
)
Expand Down
3 changes: 2 additions & 1 deletion command/ca/acme/eab/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

"github.com/pkg/errors"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"

"github.com/smallstep/certificates/ca"
"github.com/smallstep/cli-utils/errs"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
)
Expand Down
2 changes: 1 addition & 1 deletion command/ca/acme/eab/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/pkg/errors"
"github.com/urfave/cli"

"go.step.sm/cli-utils/errs"
"github.com/smallstep/cli-utils/errs"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
Expand Down
8 changes: 5 additions & 3 deletions command/ca/admin/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"os"
"text/tabwriter"

"github.com/urfave/cli"

adminAPI "github.com/smallstep/certificates/authority/admin/api"
"github.com/smallstep/cli-utils/errs"
"go.step.sm/linkedca"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"
"go.step.sm/linkedca"
)

func addCommand() cli.Command {
Expand Down
6 changes: 3 additions & 3 deletions command/ca/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"errors"
"fmt"

"github.com/smallstep/certificates/ca"
"github.com/urfave/cli"

"go.step.sm/cli-utils/errs"
"go.step.sm/cli-utils/ui"
"github.com/smallstep/certificates/ca"
"github.com/smallstep/cli-utils/errs"
"github.com/smallstep/cli-utils/ui"
"go.step.sm/linkedca"
)

Expand Down
8 changes: 5 additions & 3 deletions command/ca/admin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"os"
"text/tabwriter"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"

"github.com/smallstep/cli-utils/errs"
"go.step.sm/linkedca"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
)

func listCommand() cli.Command {
Expand Down
6 changes: 4 additions & 2 deletions command/ca/admin/remove.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package admin

import (
"github.com/urfave/cli"

"github.com/smallstep/cli-utils/errs"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"
)

func removeCommand() cli.Command {
Expand Down
8 changes: 5 additions & 3 deletions command/ca/admin/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"os"
"text/tabwriter"

"github.com/urfave/cli"

adminAPI "github.com/smallstep/certificates/authority/admin/api"
"github.com/smallstep/cli-utils/errs"
"go.step.sm/linkedca"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"
"go.step.sm/linkedca"
)

func updateCommand() cli.Command {
Expand Down
8 changes: 5 additions & 3 deletions command/ca/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package ca
import (
"strings"

"github.com/urfave/cli"

"github.com/smallstep/cli-utils/command"
"github.com/smallstep/cli-utils/errs"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/command"
"go.step.sm/cli-utils/errs"
)

func bootstrapCommand() cli.Command {
Expand Down
Loading

0 comments on commit e3c3f20

Please sign in to comment.