Skip to content

feat: Allow inheriting parameters from previous template_versions when updating a template #2397

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 18 commits into from
Jun 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Implement parameters list
- Allow more columns on template list
  • Loading branch information
Emyrk committed Jun 15, 2022
commit 8d62c96685d75a9d9c3e3ecbf49f27087b0a30fd
47 changes: 47 additions & 0 deletions cli/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)

func parameters() *cobra.Command {
cmd := &cobra.Command{
Short: "List parameters for a given scope",
Use: "parameters",
Aliases: []string{"params"},
}
cmd.AddCommand(
parameterList(),
)
return cmd
}

// displayParameters will return a table displaying all parameters passed in.
// filterColumns must be a subset of the parameter fields and will determine which
// columns to display
func displayParameters(filterColumns []string, params ...codersdk.Parameter) string {
tableWriter := cliui.Table()
header := table.Row{"id", "scope", "scope id", "name", "source scheme", "destination scheme", "created at", "updated at"}
tableWriter.AppendHeader(header)
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns))
tableWriter.SortBy([]table.SortBy{{
Name: "name",
}})
for _, param := range params {
//fmt.Println(param, filterColumns)
tableWriter.AppendRow(table.Row{
param.ID.String(),
param.Scope,
param.ScopeID.String(),
param.Name,
param.SourceScheme,
param.DestinationScheme,
param.CreatedAt,
param.UpdatedAt,
})
}
return tableWriter.Render()
}
73 changes: 73 additions & 0 deletions cli/parameterslist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cli

import (
"fmt"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/coder/codersdk"
"github.com/spf13/cobra"
)

func parameterList() *cobra.Command {
var (
columns []string
)
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
scope, name := args[0], args[1]

client, err := createClient(cmd)
if err != nil {
return err
}

organization, err := currentOrganization(cmd, client)
if err != nil {
return xerrors.Errorf("get current organization: %w", err)
}

var scopeID uuid.UUID
switch codersdk.ParameterScope(scope) {
case codersdk.ParameterWorkspace:
workspace, err := namedWorkspace(cmd, client, name)
if err != nil {
return err
}
scopeID = workspace.ID
case codersdk.ParameterTemplate:
template, err := client.TemplateByName(cmd.Context(), organization.ID, name)
if err != nil {
return xerrors.Errorf("get workspace template: %w", err)
}
scopeID = template.ID

case codersdk.ParameterScopeImportJob, "template_version":
scope = string(codersdk.ParameterScopeImportJob)
scopeID, err = uuid.Parse(name)
if err != nil {
return xerrors.Errorf("%q must be a uuid for this scope type", name)
}
default:
return xerrors.Errorf("%q is an unsupported scope, use %v", scope, []codersdk.ParameterScope{
codersdk.ParameterWorkspace, codersdk.ParameterTemplate, codersdk.ParameterScopeImportJob,
})
}

params, err := client.Parameters(cmd.Context(), codersdk.ParameterScope(args[0]), scopeID)
if err != nil {
return xerrors.Errorf("fetch params: %w", err)
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), displayParameters(columns, params...))
return err
},
}
cmd.Flags().StringArrayVarP(&columns, "column", "c", []string{"name", "source_scheme", "destination_scheme"},
"Specify a column to filter in the table.")
return cmd
}
1 change: 1 addition & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func Root() *cobra.Command {
portForward(),
workspaceAgent(),
versionCmd(),
parameters(),
)

cmd.SetUsageTemplate(usageTemplate())
Expand Down
27 changes: 8 additions & 19 deletions cli/templatelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"

"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"

"github.com/coder/coder/cli/cliui"
)

func templateList() *cobra.Command {
return &cobra.Command{
var (
columns []string
)
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -34,22 +34,11 @@ func templateList() *cobra.Command {
return nil
}

tableWriter := cliui.Table()
tableWriter.AppendHeader(table.Row{"Name", "Last updated", "Used by"})

for _, template := range templates {
suffix := ""
if template.WorkspaceOwnerCount != 1 {
suffix = "s"
}
tableWriter.AppendRow(table.Row{
template.Name,
template.UpdatedAt.Format("January 2, 2006"),
cliui.Styles.Fuschia.Render(fmt.Sprintf("%d developer%s", template.WorkspaceOwnerCount, suffix)),
})
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), tableWriter.Render())
_, err = fmt.Fprintln(cmd.OutOrStdout(), displayTemplates(columns, templates...))
return err
},
}
cmd.Flags().StringArrayVarP(&columns, "column", "c", []string{"name", "last_updated", "used_by"},
"Specify a column to filter in the table.")
return cmd
}
40 changes: 39 additions & 1 deletion cli/templates.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cli

import (
"fmt"
"time"

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

"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)

func templates() *cobra.Command {
Expand All @@ -17,7 +22,7 @@ func templates() *cobra.Command {
` + cliui.Styles.Code.Render("$ coder templates create") + `

- Make changes to your template, and plan the changes

` + cliui.Styles.Code.Render("$ coder templates plan <name>") + `

- Update the template. Your developers can update their workspaces
Expand All @@ -37,3 +42,36 @@ func templates() *cobra.Command {

return cmd
}

// displayTemplates will return a table displaying all templates passed in.
// filterColumns must be a subset of the template fields and will determine which
// columns to display
func displayTemplates(filterColumns []string, templates ...codersdk.Template) string {
tableWriter := cliui.Table()
header := table.Row{
"Name", "Created At", "Last Updated", "Organization ID", "Provisioner",
"Active Version ID", "Used By", "Max TTL", "Min Autostart"}
tableWriter.AppendHeader(header)
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns))
tableWriter.SortBy([]table.SortBy{{
Name: "name",
}})
for _, template := range templates {
suffix := ""
if template.WorkspaceOwnerCount != 1 {
suffix = "s"
}
tableWriter.AppendRow(table.Row{
template.Name,
template.CreatedAt.Format("January 2, 2006"),
template.UpdatedAt.Format("January 2, 2006"),
template.OrganizationID.String(),
template.Provisioner,
template.ActiveVersionID.String(),
cliui.Styles.Fuschia.Render(fmt.Sprintf("%d developer%s", template.WorkspaceOwnerCount, suffix)),
fmt.Sprintf("%s", time.Duration(template.MaxTTLMillis)*time.Millisecond),
fmt.Sprintf("%s", time.Duration(template.MinAutostartIntervalMillis)*time.Millisecond),
})
}
return tableWriter.Render()
}
2 changes: 1 addition & 1 deletion cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func users() *cobra.Command {
// columns to display
func displayUsers(filterColumns []string, users ...codersdk.User) string {
tableWriter := cliui.Table()
header := table.Row{"id", "username", "email", "created_at", "status"}
header := table.Row{"id", "username", "email", "created at", "status"}
tableWriter.AppendHeader(header)
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns))
tableWriter.SortBy([]table.SortBy{{
Expand Down
5 changes: 3 additions & 2 deletions codersdk/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
type ParameterScope string

const (
ParameterTemplate ParameterScope = "template"
ParameterWorkspace ParameterScope = "workspace"
ParameterTemplate ParameterScope = "template"
ParameterWorkspace ParameterScope = "workspace"
ParameterScopeImportJob ParameterScope = "import_job"
)

type ParameterSourceScheme string
Expand Down