-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Project list, get and set commands #22
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
11 commits
Select commit
Hold shift + click to select a range
b832b0f
Project get and set
nterbogt 4808238
Implementing list. Adding grouping. Cleaning up documentation. Adding…
nterbogt 42f0f47
Copypasta documentation
nterbogt 3de4745
Update the function comment to be correct
nterbogt d914153
No side effects from listing project environments
nterbogt b46b868
More copypasta comments
nterbogt 09359e9
Updating for what the command does.
nterbogt 4522ee5
More comment improvements
nterbogt aa85f28
Some cleanups based on copilot review
nterbogt 5345a99
Setting up concurrency rules for lint and test
nterbogt 0d8f0f3
Moving get to info
nterbogt 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
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/skpr/cli/cmd/skpr/project/info" | ||
| "github.com/skpr/cli/cmd/skpr/project/list" | ||
| "github.com/skpr/cli/cmd/skpr/project/set" | ||
| "github.com/skpr/cli/internal/command" | ||
| ) | ||
|
|
||
| // NewCommand creates a new cobra.Command for the top-level 'project' command | ||
| func NewCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "project", | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Manage projects", | ||
| GroupID: command.GroupLifecycle, | ||
| } | ||
|
|
||
| cmd.AddCommand(info.NewCommand()) | ||
| cmd.AddCommand(list.NewCommand()) | ||
| cmd.AddCommand(set.NewCommand()) | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package info | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| v1get "github.com/skpr/cli/internal/command/project/info" | ||
| ) | ||
|
|
||
| var ( | ||
| cmdExample = ` | ||
| # Get project information | ||
| skpr project info` | ||
| ) | ||
|
|
||
| // NewCommand creates a new cobra.Command for 'info' sub command | ||
| func NewCommand() *cobra.Command { | ||
| command := v1get.Command{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "info", | ||
| Args: cobra.NoArgs, | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Get full details for the current project.", | ||
| Example: cmdExample, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return command.Run(cmd.Context()) | ||
| }, | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package list | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| v1list "github.com/skpr/cli/internal/command/project/list" | ||
| ) | ||
|
|
||
| // NewCommand creates a new cobra.Command for 'list' sub command | ||
| func NewCommand() *cobra.Command { | ||
| command := v1list.Command{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Args: cobra.NoArgs, | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Overview of all projects", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return command.Run(cmd.Context()) | ||
| }, | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package set | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| v1set "github.com/skpr/cli/internal/command/project/set" | ||
| ) | ||
|
|
||
| var ( | ||
| cmdExample = ` | ||
| # Set the contact for the project. | ||
| skpr project set contact my-new-contact@example.com | ||
|
|
||
| # Set the tags for the project. | ||
| skpr project set tags "tag-a tag-b tag-c"` | ||
| ) | ||
|
|
||
| // NewCommand creates a new cobra.Command for 'set' sub command | ||
| func NewCommand() *cobra.Command { | ||
| command := v1set.Command{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "set <key> <value>", | ||
| Args: cobra.ExactArgs(2), | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Set an attribute for the current project.", | ||
| Example: cmdExample, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| command.Key = args[0] | ||
| command.Value = args[1] | ||
| return command.Run(cmd.Context()) | ||
| }, | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package info | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/skpr/api/pb" | ||
|
|
||
| "github.com/skpr/cli/internal/client" | ||
| "github.com/skpr/cli/internal/project" | ||
| "github.com/skpr/cli/internal/table" | ||
| ) | ||
|
|
||
| // Command for fetching and printing project details. | ||
| type Command struct { | ||
| } | ||
|
|
||
| // Run the command. | ||
| func (cmd *Command) Run(ctx context.Context) error { | ||
| ctx, client, err := client.New(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := client.Project().Get(ctx, &pb.ProjectGetRequest{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = Print(os.Stdout, resp.Project) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Print the table... | ||
| func Print(w io.Writer, item *pb.Project) error { | ||
| header := []string{ | ||
| "Property", | ||
| "Value", | ||
| } | ||
|
|
||
| rows := [][]string{ | ||
| {"ID", item.ID}, | ||
| {"Name", item.Name}, | ||
| {"Contact", item.Contact}, | ||
| {"Version", item.Version}, | ||
| {"Environments", strings.Join(project.ListEnvironmentsByName(item), ", ")}, | ||
| {"Size", item.Size}, | ||
| {"Tags", strings.Join(item.Tags, ", ")}, | ||
| } | ||
|
|
||
| return table.Print(w, header, rows) | ||
| } |
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,77 @@ | ||
| package list | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/skpr/api/pb" | ||
|
|
||
| "github.com/skpr/cli/internal/client" | ||
| "github.com/skpr/cli/internal/project" | ||
| "github.com/skpr/cli/internal/table" | ||
| ) | ||
|
|
||
| // Command for listing projects. | ||
| type Command struct { | ||
| } | ||
|
|
||
| // Run the command. | ||
| func (cmd *Command) Run(ctx context.Context) error { | ||
| ctx, client, err := client.New(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := client.Project().List(ctx, &pb.ProjectListRequest{}) | ||
| if err != nil { | ||
| return errors.Wrap(err, "Could not list projects") | ||
| } | ||
|
|
||
| return Print(os.Stdout, resp.Projects) | ||
| } | ||
|
|
||
| // Print the table... | ||
| func Print(w io.Writer, list []*pb.Project) error { | ||
| if len(list) == 0 { | ||
| fmt.Fprintln(w, "No projects found") | ||
| return nil | ||
| } | ||
|
|
||
| sortProjects(list) | ||
|
|
||
| header := []string{ | ||
| "Name", | ||
| "Contact", | ||
| "Version", | ||
| "Environments", | ||
| "Size", | ||
| "Tags", | ||
| } | ||
|
|
||
| var rows [][]string | ||
|
|
||
| for _, item := range list { | ||
| rows = append(rows, []string{ | ||
| item.Name, | ||
| item.Contact, | ||
| item.Version, | ||
| strings.Join(project.ListEnvironmentsByName(item), ", "), | ||
| item.Size, | ||
| strings.Join(item.Tags, ", "), | ||
| }) | ||
| } | ||
|
|
||
| return table.Print(w, header, rows) | ||
| } | ||
|
|
||
| // sortProjects sorts the list of projects by name in ascending order. | ||
| func sortProjects(list []*pb.Project) { | ||
| sort.Slice(list, func(i, j int) bool { | ||
| return list[i].Name < list[j].Name | ||
| }) | ||
| } |
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,48 @@ | ||
| package set | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/skpr/api/pb" | ||
|
|
||
| "github.com/skpr/cli/internal/client" | ||
| ) | ||
|
|
||
| // Command for setting project info. | ||
| type Command struct { | ||
| Key string | ||
| Value string | ||
| } | ||
|
|
||
| // Run the command. | ||
| func (cmd *Command) Run(ctx context.Context) error { | ||
| ctx, client, err := client.New(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| switch cmd.Key { | ||
| case "contact": | ||
| req := &pb.SetContactRequest{ | ||
| Contact: cmd.Value, | ||
| } | ||
| _, err = client.Project().SetContact(ctx, req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| case "tags": | ||
| req := &pb.SetTagsRequest{ | ||
| Tags: strings.Fields(cmd.Value), | ||
| } | ||
| _, err = client.Project().SetTags(ctx, req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| default: | ||
| return fmt.Errorf("invalid key. Currently supported keys are: contact, tags") | ||
| } | ||
|
|
||
| return nil | ||
| } |
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,20 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "sort" | ||
|
|
||
| "github.com/skpr/api/pb" | ||
| ) | ||
|
|
||
| // ListEnvironmentsByName lists them in printable order. | ||
| func ListEnvironmentsByName(project *pb.Project) []string { | ||
| environments := []string{ | ||
| project.Environments.Prod, | ||
| } | ||
|
|
||
| nonProd := append([]string(nil), project.Environments.NonProd...) | ||
| sort.Strings(nonProd) | ||
| environments = append(environments, nonProd...) | ||
|
|
||
| return environments | ||
| } |
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,27 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/skpr/api/pb" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestSort(t *testing.T) { | ||
| item := pb.Project{ | ||
| Environments: &pb.ProjectEnvironments{ | ||
| Prod: "prod", | ||
| NonProd: []string{ | ||
| "dev", | ||
| "training", | ||
| "stg", | ||
| "bvt", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| out := ListEnvironmentsByName(&item) | ||
| expected := []string{"prod", "bvt", "dev", "stg", "training"} | ||
|
|
||
| assert.Equal(t, expected, out) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.