-
Notifications
You must be signed in to change notification settings - Fork 905
feat: Implement parameters list + more template list columns #2359
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func parameters() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Short: "List parameters for a given scope", | ||
Example: "coder parameters list workspace my-workspace", | ||
Use: "parameters", | ||
// Currently hidden as this shows parameter values, not parameter | ||
// schemes. Until we have a good way to distinguish the two, it's better | ||
// not to add confusion or lock ourselves into a certain api. | ||
// This cmd is still valuable debugging tool for devs to avoid | ||
// constructing curl requests. | ||
Hidden: true, | ||
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 { | ||
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() | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
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(scope), 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
@@ -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 | ||
|
@@ -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)), | ||
(time.Duration(template.MaxTTLMillis) * time.Millisecond).String(), | ||
(time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond).String(), | ||
}) | ||
} | ||
return tableWriter.Render() | ||
} | ||
Comment on lines
+49
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach of abstracting the presentation logic from the command logic; makes it more re-usable later.