Skip to content

Commit ed9be78

Browse files
committed
Merge remote-tracking branch 'origin/main' into stevenmasley/rbac_endpoints_per_handler
2 parents a989224 + 22ec366 commit ed9be78

File tree

131 files changed

+4077
-3638
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+4077
-3638
lines changed

.github/workflows/coder.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
with:
4141
go-version: "~1.18"
4242
- name: golangci-lint
43-
uses: golangci/golangci-lint-action@v3.1.0
43+
uses: golangci/golangci-lint-action@v3.2.0
4444
with:
4545
version: v1.46.0
4646

@@ -172,7 +172,7 @@ jobs:
172172
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
173173

174174
- name: Install goreleaser
175-
uses: jaxxstorm/action-install-gh-release@v1.5.0
175+
uses: jaxxstorm/action-install-gh-release@v1.6.0
176176
env:
177177
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
178178
with:
@@ -241,7 +241,7 @@ jobs:
241241
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
242242

243243
- name: Install goreleaser
244-
uses: jaxxstorm/action-install-gh-release@v1.5.0
244+
uses: jaxxstorm/action-install-gh-release@v1.6.0
245245
env:
246246
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
247247
with:

.github/workflows/release.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ jobs:
1313
DOCKER_CLI_EXPERIMENTAL: "enabled"
1414
steps:
1515
# Docker is not included on macos-latest
16-
- uses: docker-practice/actions-setup-docker@v1.0.8
16+
- uses: docker-practice/actions-setup-docker@1.0.10
1717

1818
- uses: actions/checkout@v3
1919
with:
2020
fetch-depth: 0
2121

2222
- name: Set up QEMU
23-
uses: docker/setup-qemu-action@v1
23+
uses: docker/setup-qemu-action@v2
2424

2525
- name: Docker Login
26-
uses: docker/login-action@v1
26+
uses: docker/login-action@v2
2727
with:
2828
registry: ghcr.io
2929
username: ${{ github.repository_owner }}

cli/autostart.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ func autostartShow() *cobra.Command {
6363
return nil
6464
}
6565

66-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostartSchedule, validSchedule.Next(time.Now()))
66+
next := validSchedule.Next(time.Now())
67+
loc, _ := time.LoadLocation(validSchedule.Timezone())
68+
69+
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
70+
"schedule: %s\ntimezone: %s\nnext: %s\n",
71+
validSchedule.Cron(),
72+
validSchedule.Timezone(),
73+
next.In(loc),
74+
)
6775

6876
return nil
6977
},

cli/autostart_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func TestAutostart(t *testing.T) {
4545

4646
err = cmd.Execute()
4747
require.NoError(t, err, "unexpected error")
48-
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
48+
// CRON_TZ gets stripped
49+
require.Contains(t, stdoutBuf.String(), "schedule: 30 17 * * 1-5")
4950
})
5051

5152
t.Run("EnableDisableOK", func(t *testing.T) {

cli/autostop.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ func autostopShow() *cobra.Command {
6363
return nil
6464
}
6565

66-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostopSchedule, validSchedule.Next(time.Now()))
66+
next := validSchedule.Next(time.Now())
67+
loc, _ := time.LoadLocation(validSchedule.Timezone())
68+
69+
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
70+
"schedule: %s\ntimezone: %s\nnext: %s\n",
71+
validSchedule.Cron(),
72+
validSchedule.Timezone(),
73+
next.In(loc),
74+
)
6775

6876
return nil
6977
},

cli/autostop_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func TestAutostop(t *testing.T) {
4545

4646
err = cmd.Execute()
4747
require.NoError(t, err, "unexpected error")
48-
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
48+
// CRON_TZ gets stripped
49+
require.Contains(t, stdoutBuf.String(), "schedule: 30 17 * * 1-5")
4950
})
5051

5152
t.Run("EnableDisableOK", func(t *testing.T) {

cli/list.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/spf13/cobra"
1111

1212
"github.com/coder/coder/cli/cliui"
13+
"github.com/coder/coder/coderd/autobuild/schedule"
1314
"github.com/coder/coder/coderd/database"
1415
"github.com/coder/coder/codersdk"
1516
)
@@ -108,14 +109,18 @@ func list() *cobra.Command {
108109
durationDisplay = durationDisplay[:len(durationDisplay)-2]
109110
}
110111

111-
autostartDisplay := "not enabled"
112+
autostartDisplay := "-"
112113
if workspace.AutostartSchedule != "" {
113-
autostartDisplay = workspace.AutostartSchedule
114+
if sched, err := schedule.Weekly(workspace.AutostartSchedule); err == nil {
115+
autostartDisplay = sched.Cron()
116+
}
114117
}
115118

116-
autostopDisplay := "not enabled"
119+
autostopDisplay := "-"
117120
if workspace.AutostopSchedule != "" {
118-
autostopDisplay = workspace.AutostopSchedule
121+
if sched, err := schedule.Weekly(workspace.AutostopSchedule); err == nil {
122+
autostopDisplay = sched.Cron()
123+
}
119124
}
120125

121126
user := usersByID[workspace.OwnerID]

cli/login.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,31 @@ func login() *cobra.Command {
168168
return nil
169169
}
170170

171-
authURL := *serverURL
172-
authURL.Path = serverURL.Path + "/cli-auth"
173-
if err := openURL(cmd, authURL.String()); err != nil {
174-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Open the following in your browser:\n\n\t%s\n\n", authURL.String())
175-
} else {
176-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Your browser has been opened to visit:\n\n\t%s\n\n", authURL.String())
177-
}
171+
sessionToken, _ := cmd.Flags().GetString(varToken)
172+
if sessionToken == "" {
173+
authURL := *serverURL
174+
authURL.Path = serverURL.Path + "/cli-auth"
175+
if err := openURL(cmd, authURL.String()); err != nil {
176+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Open the following in your browser:\n\n\t%s\n\n", authURL.String())
177+
} else {
178+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Your browser has been opened to visit:\n\n\t%s\n\n", authURL.String())
179+
}
178180

179-
sessionToken, err := cliui.Prompt(cmd, cliui.PromptOptions{
180-
Text: "Paste your token here:",
181-
Secret: true,
182-
Validate: func(token string) error {
183-
client.SessionToken = token
184-
_, err := client.User(cmd.Context(), codersdk.Me)
185-
if err != nil {
186-
return xerrors.New("That's not a valid token!")
187-
}
188-
return err
189-
},
190-
})
191-
if err != nil {
192-
return xerrors.Errorf("paste token prompt: %w", err)
181+
sessionToken, err = cliui.Prompt(cmd, cliui.PromptOptions{
182+
Text: "Paste your token here:",
183+
Secret: true,
184+
Validate: func(token string) error {
185+
client.SessionToken = token
186+
_, err := client.User(cmd.Context(), codersdk.Me)
187+
if err != nil {
188+
return xerrors.New("That's not a valid token!")
189+
}
190+
return err
191+
},
192+
})
193+
if err != nil {
194+
return xerrors.Errorf("paste token prompt: %w", err)
195+
}
193196
}
194197

195198
// Login to get user data - verify it is OK before persisting

cli/login_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,16 @@ func TestLogin(t *testing.T) {
140140
cancelFunc()
141141
<-doneChan
142142
})
143+
144+
t.Run("TokenFlag", func(t *testing.T) {
145+
t.Parallel()
146+
client := coderdtest.New(t, nil)
147+
coderdtest.CreateFirstUser(t, client)
148+
root, cfg := clitest.New(t, "login", client.URL.String(), "--token", client.SessionToken)
149+
err := root.Execute()
150+
require.NoError(t, err)
151+
sessionFile, err := cfg.Session().Read()
152+
require.NoError(t, err)
153+
require.Equal(t, client.SessionToken, sessionFile)
154+
})
143155
}

cli/root.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,68 @@ func usageTemplate() string {
182182
// Customizes the color of headings to make subcommands
183183
// more visually appealing.
184184
header := cliui.Styles.Placeholder
185+
cobra.AddTemplateFunc("usageHeader", func(s string) string {
186+
return header.Render(s)
187+
})
185188

186-
return `{{if .HasExample}}` + header.Render("Get Started:") + `
189+
return `{{usageHeader "Usage:"}}
190+
{{- if .Runnable}}
191+
{{.UseLine}}
192+
{{end}}
193+
{{- if .HasAvailableSubCommands}}
194+
{{.CommandPath}} [command]
195+
{{end}}
196+
197+
{{- if gt (len .Aliases) 0}}
198+
{{usageHeader "Aliases:"}}
199+
{{.NameAndAliases}}
200+
{{end}}
201+
202+
{{- if .HasExample}}
203+
{{usageHeader "Get Started:"}}
187204
{{.Example}}
205+
{{end}}
206+
207+
{{- if .HasAvailableSubCommands}}
208+
{{usageHeader "Commands:"}}
209+
{{- range .Commands}}
210+
{{- if (or (and .IsAvailableCommand (eq (len .Annotations) 0)) (eq .Name "help"))}}
211+
{{rpad .Name .NamePadding }} {{.Short}}
212+
{{- end}}
213+
{{- end}}
214+
{{end}}
215+
216+
{{- if and (not .HasParent) .HasAvailableSubCommands}}
217+
{{usageHeader "Workspace Commands:"}}
218+
{{- range .Commands}}
219+
{{- if (and .IsAvailableCommand (ne (index .Annotations "workspaces") ""))}}
220+
{{rpad .Name .NamePadding }} {{.Short}}
221+
{{- end}}
222+
{{- end}}
223+
{{end}}
188224
189-
{{end}}{{if .HasAvailableLocalFlags}}` + header.Render("Flags:") + `
190-
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableSubCommands}}
225+
{{- if .HasAvailableLocalFlags}}
226+
{{usageHeader "Flags:"}}
227+
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}
228+
{{end}}
191229
192-
` + header.Render("Commands:") + `{{range .Commands}}{{if and .IsAvailableCommand (eq (len .Annotations) 0)}}
193-
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .HasParent }}
230+
{{- if .HasAvailableInheritedFlags}}
231+
{{usageHeader "Global Flags:"}}
232+
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}
233+
{{end}}
194234
195-
` + header.Render("Workspace Commands:") + `{{range .Commands}}{{if and .IsAvailableCommand (ne (index .Annotations "workspaces") "")}}
196-
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}
235+
{{- if .HasHelpSubCommands}}
236+
{{usageHeader "Additional help topics:"}}
237+
{{- range .Commands}}
238+
{{- if .IsAdditionalHelpTopicCommand}}
239+
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}
240+
{{- end}}
241+
{{- end}}
242+
{{end}}
197243
244+
{{- if .HasAvailableSubCommands}}
198245
Use "{{.CommandPath}} [command] --help" for more information about a command.
199-
`
246+
{{end}}`
200247
}
201248

202249
func versionTemplate() string {

cli/userlist.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ package cli
22

33
import (
44
"fmt"
5-
"time"
65

7-
"github.com/jedib0t/go-pretty/v6/table"
86
"github.com/spf13/cobra"
97

10-
"github.com/coder/coder/cli/cliui"
118
"github.com/coder/coder/codersdk"
129
)
1310

@@ -28,25 +25,11 @@ func userList() *cobra.Command {
2825
return err
2926
}
3027

31-
tableWriter := cliui.Table()
32-
header := table.Row{"Username", "Email", "Created At"}
33-
tableWriter.AppendHeader(header)
34-
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, columns))
35-
tableWriter.SortBy([]table.SortBy{{
36-
Name: "Username",
37-
}})
38-
for _, user := range users {
39-
tableWriter.AppendRow(table.Row{
40-
user.Username,
41-
user.Email,
42-
user.CreatedAt.Format(time.Stamp),
43-
})
44-
}
45-
_, err = fmt.Fprintln(cmd.OutOrStdout(), tableWriter.Render())
28+
_, err = fmt.Fprintln(cmd.OutOrStdout(), displayUsers(columns, users...))
4629
return err
4730
},
4831
}
49-
cmd.Flags().StringArrayVarP(&columns, "column", "c", nil,
32+
cmd.Flags().StringArrayVarP(&columns, "column", "c", []string{"username", "email", "created_at"},
5033
"Specify a column to filter in the table.")
5134
return cmd
5235
}

cli/users.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
11
package cli
22

3-
import "github.com/spf13/cobra"
3+
import (
4+
"time"
5+
6+
"github.com/jedib0t/go-pretty/v6/table"
7+
"github.com/spf13/cobra"
8+
9+
"github.com/coder/coder/cli/cliui"
10+
"github.com/coder/coder/codersdk"
11+
)
412

513
func users() *cobra.Command {
614
cmd := &cobra.Command{
715
Short: "Create, remove, and list users",
816
Use: "users",
917
}
10-
cmd.AddCommand(userCreate(), userList())
18+
cmd.AddCommand(
19+
userCreate(),
20+
userList(),
21+
createUserStatusCommand(codersdk.UserStatusActive),
22+
createUserStatusCommand(codersdk.UserStatusSuspended),
23+
)
1124
return cmd
1225
}
26+
27+
// displayUsers will return a table displaying all users passed in.
28+
// filterColumns must be a subset of the user fields and will determine which
29+
// columns to display
30+
func displayUsers(filterColumns []string, users ...codersdk.User) string {
31+
tableWriter := cliui.Table()
32+
header := table.Row{"id", "username", "email", "created_at", "status"}
33+
tableWriter.AppendHeader(header)
34+
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns))
35+
tableWriter.SortBy([]table.SortBy{{
36+
Name: "Username",
37+
}})
38+
for _, user := range users {
39+
tableWriter.AppendRow(table.Row{
40+
user.ID.String(),
41+
user.Username,
42+
user.Email,
43+
user.CreatedAt.Format(time.Stamp),
44+
user.Status,
45+
})
46+
}
47+
return tableWriter.Render()
48+
}

0 commit comments

Comments
 (0)