8000 coder licenses add CLI command by spikecurtis · Pull Request #3632 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

coder licenses add CLI command #3632

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

Merged
merged 5 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments. 8000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix up lint
Signed-off-by: Spike Curtis <spike@coder.com>
  • Loading branch information
spikecurtis committed Aug 22, 2022
commit 26309c4a0a8da60d699ad86c212559c8f2ecf747
11 changes: 4 additions & 7 deletions enterprise/cli/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/coder/coder/codersdk"
)

var jwtRegexp = regexp.MustCompile("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+$")
var jwtRegexp = regexp.MustCompile(`^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$`)

func licenses() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -65,11 +65,11 @@ func licenseAdd() *cobra.Command {
r = cmd.InOrStdin()
} else {
f, err := os.Open(filename)
defer f.Close()
r = f
if err != nil {
return err
}
defer f.Close()
r = f
}
lb, err := io.ReadAll(r)
if err != nil {
Expand All @@ -93,10 +93,7 @@ func licenseAdd() *cobra.Command {
enc := json.NewEncoder(cmd.OutOrStdout())
return enc.Encode(licResp)
}
_, _ = fmt.Fprintln(
cmd.OutOrStdout(),
fmt.Sprintf("License with ID %d added", licResp.ID),
)
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "License with ID %d added\n", licResp.ID)
return nil
},
}
Expand Down
33 changes: 19 additions & 14 deletions enterprise/cli/licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func TestLicensesAddSuccess(t *testing.T) {
// so instead we have to fake the HTTP interaction. t.Parallel()
t.Run("LFlag", func(t *testing.T) {
t.Parallel()
cmd, pty := setupFakeLicenseServerTest(t, true, "licenses", "add", "-l", fakeLicenseJWT)
cmd := setupFakeLicenseServerTest(t, "licenses", "add", "-l", fakeLicenseJWT)
pty := attachPty(t, cmd)
errC := make(chan error)
go func() {
errC <- cmd.Execute()
Expand All @@ -43,7 +44,8 @@ func TestLicensesAddSuccess(t *testing.T) {
})
t.Run("Prompt", func(t *testing.T) {
t.Parallel()
cmd, pty := setupFakeLicenseServerTest(t, true, "license", "add")
cmd := setupFakeLicenseServerTest(t, "license", "add")
pty := attachPty(t, cmd)
errC := make(chan error)
go func() {
errC <- cmd.Execute()
Expand All @@ -57,9 +59,10 @@ func TestLicensesAddSuccess(t *testing.T) {
t.Parallel()
dir := t.TempDir()
filename := filepath.Join(dir, "license.jwt")
err := os.WriteFile(filename, []byte(fakeLicenseJWT), 0666)
err := os.WriteFile(filename, []byte(fakeLicenseJWT), 0600)
require.NoError(t, err)
cmd, pty := setupFakeLicenseServerTest(t, true, "license", "add", "-f", filename)
cmd := setupFakeLicenseServerTest(t, "license", "add", "-f", filename)
pty := attachPty(t, cmd)
errC := make(chan error)
go func() {
errC <- cmd.Execute()
Expand All @@ -69,7 +72,7 @@ func TestLicensesAddSuccess(t *testing.T) {
})
t.Run("StdIn", func(t *testing.T) {
t.Parallel()
cmd, _ := setupFakeLicenseServerTest(t, false, "license", "add", "-f", "-")
cmd := setupFakeLicenseServerTest(t, "license", "add", "-f", "-")
r, w := io.Pipe()
cmd.SetIn(r)
stdout := new(bytes.Buffer)
Expand All @@ -94,7 +97,8 @@ func TestLicensesAddSuccess(t *testing.T) {
})
t.Run("DebugOutput", func(t *testing.T) {
t.Parallel()
cmd, pty := setupFakeLicenseServerTest(t, true, "licenses", "add", "-l", fakeLicenseJWT, "--debug")
cmd := setupFakeLicenseServerTest(t, "licenses", "add", "-l", fakeLicenseJWT, "--debug")
pty := attachPty(t, cmd)
errC := make(chan error)
go func() {
errC <- cmd.Execute()
Expand Down Expand Up @@ -126,7 +130,7 @@ func TestLicensesAddFail(t *testing.T) {
})
}

func setupFakeLicenseServerTest(t *testing.T, withPty bool, args ...string) (*cobra.Command, *ptytest.PTY) {
func setupFakeLicenseServerTest(t *testing.T, args ...string) *cobra.Command {
t.Helper()
s := httptest.NewServer(&fakeAddLicenseServer{t})
t.Cleanup(s.Close)
Expand All @@ -135,13 +139,14 @@ func setupFakeLicenseServerTest(t *testing.T, withPty bool, args ...string) (*co
require.NoError(t, err)
err = root.Session().Write("sessiontoken")
require.NoError(t, err)
var pty *ptytest.PTY
if withPty {
pty = ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
}
return cmd, pty
return cmd
}

func attachPty(t *testing.T, cmd *cobra.Command) *ptytest.PTY {
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
return pty
}

type fakeAddLicenseServer struct {
Expand Down
0