-
Notifications
You must be signed in to change notification settings - Fork 0
feat(serverless): add apps rename for the display name #144
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
Open
Ryank90
wants to merge
1
commit into
rc/serverless
Choose a base branch
from
feat/serverless-apps-rename
base: rc/serverless
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| ## runware serverless apps rename | ||
|
|
||
| Rename a serverless application | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Change the display name of a serverless application. | ||
|
|
||
| The application ID is immutable. A name-only update records a version and does | ||
| not pin or roll workers. deploy --name still applies on create only. | ||
|
|
||
| ``` | ||
| runware serverless apps rename <appId> <name> [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| # rename an application | ||
| runware serverless apps rename my-app "Image generator" | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for rename | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| --debug Show full debug output | ||
| -F, --format string CLI output format: table, json, yaml (default "table") | ||
| --transport string Transport protocol: ws (WebSocket) or http (REST) (default "ws") | ||
| -v, --verbose Show request/response details | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [runware serverless apps](runware_serverless_apps.md) - Manage deployed serverless applications | ||
|
|
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,57 @@ | ||
| package serverless | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
| "strings" | ||
|
|
||
| "github.com/charmbracelet/log" | ||
| serverlessapi "github.com/runware/runware-cli/internal/api/serverless" | ||
| "github.com/runware/runware-cli/internal/cmdutil" | ||
| "github.com/runware/runware-cli/internal/config" | ||
| "github.com/runware/runware-cli/internal/output" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newAppsRenameCmd(logger *log.Logger) *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "rename <appId> <name>", | ||
| Short: "Rename a serverless application", | ||
| Long: `Change the display name of a serverless application. | ||
|
|
||
| The application ID is immutable. A name-only update records a version and does | ||
| not pin or roll workers. deploy --name still applies on create only.`, | ||
| Example: ` # rename an application | ||
| runware serverless apps rename my-app "Image generator"`, | ||
| Args: cobra.ExactArgs(2), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| id := args[0] | ||
| name := args[1] | ||
| if err := validateAppName(name); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| spin := cmdutil.NewSpinner(fmt.Sprintf("Renaming application %s...", id)) | ||
| spin.Start() | ||
|
|
||
| client := serverlessapi.NewClient(config.GetAPIKey(), config.GetServerlessBaseURL(), slog.New(logger)) | ||
| app, err := client.UpdateApp(cmd.Context(), id, serverlessapi.AppUpdate{ | ||
| AppName: &name, | ||
| }) | ||
| if err != nil { | ||
| spin.Stop() | ||
| return err | ||
| } | ||
| spin.Stop() | ||
|
|
||
| return output.Print(cmdutil.FormatFor(cmd), appResult(*app)) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func validateAppName(name string) error { | ||
| if name == "" || strings.TrimSpace(name) != name { | ||
| return fmt.Errorf("name must start and end with a non-whitespace character") | ||
| } | ||
| 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,89 @@ | ||
| package serverless | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/charmbracelet/log" | ||
| serverlessapi "github.com/runware/runware-cli/internal/api/serverless" | ||
| ) | ||
|
|
||
| func TestValidateAppName(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| wantErr bool | ||
| }{ | ||
| {name: "Image generator"}, | ||
| {name: "A"}, | ||
| {name: "", wantErr: true}, | ||
| {name: " padded", wantErr: true}, | ||
| {name: "padded ", wantErr: true}, | ||
| {name: " ", wantErr: true}, | ||
| } | ||
| for _, tc := range cases { | ||
| err := validateAppName(tc.name) | ||
| if tc.wantErr && err == nil { | ||
| t.Errorf("%q: expected error", tc.name) | ||
| } | ||
| if !tc.wantErr && err != nil { | ||
| t.Errorf("%q: %v", tc.name, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestAppsRename_PatchesNameOnly(t *testing.T) { | ||
| const wantName = "Image generator" | ||
| var patches int | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPatch || r.URL.Path != "/v1/apps/"+testAppID { | ||
| t.Errorf("unexpected %s %s", r.Method, r.URL.Path) | ||
| w.WriteHeader(http.StatusNotFound) | ||
| return | ||
| } | ||
| patches++ | ||
| var body serverlessapi.AppUpdate | ||
| if err := json.NewDecoder(r.Body).Decode(&body); err != nil { | ||
| t.Errorf("decode: %v", err) | ||
| return | ||
| } | ||
| if body.AppName == nil || *body.AppName != wantName { | ||
| t.Errorf("appName = %v", body.AppName) | ||
| } | ||
| if body.AppSource != nil || body.Configuration != nil || body.Secrets != nil || body.EnvironmentVariables != nil { | ||
| t.Errorf("patch included out-of-scope fields") | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(strings.Replace(activeAppBody(""), `"appName":"My App"`, `"appName":"`+wantName+`"`, 1))) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| t.Setenv("RUNWARE_API_KEY", "test-key") | ||
| t.Setenv("RUNWARE_SERVERLESS_BASE_URL", srv.URL) | ||
|
|
||
| cmd := newAppsRenameCmd(log.New(io.Discard)) | ||
| cmd.SetOut(&bytes.Buffer{}) | ||
| cmd.SetErr(&bytes.Buffer{}) | ||
| cmd.SetArgs([]string{testAppID, wantName}) | ||
| if err := cmd.Execute(); err != nil { | ||
| t.Fatalf("rename: %v", err) | ||
| } | ||
| if patches != 1 { | ||
| t.Fatalf("patches = %d, want 1", patches) | ||
| } | ||
| } | ||
|
|
||
| func TestAppsRename_RejectsPaddedName(t *testing.T) { | ||
| cmd := newAppsRenameCmd(nil) | ||
| cmd.SetOut(&bytes.Buffer{}) | ||
| cmd.SetErr(&bytes.Buffer{}) | ||
| cmd.SetArgs([]string{testAppID, " padded"}) | ||
| err := cmd.Execute() | ||
| if err == nil || !strings.Contains(err.Error(), "non-whitespace") { | ||
| t.Fatalf("err = %v", err) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
nit: output is discarded, so dropping the
output.Printinapps_rename.gostill passes. Keep the stdout buffer and assert it containswantName.