diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 0fb04e2d8..ce9f2cc0f 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -11,6 +11,10 @@ on: branches: - main +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + permissions: {} jobs: diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index da1952dce..3b0968bf9 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -1,13 +1,7 @@ name: Docs +# Community fork: automatic docs deployment is disabled; run manually if needed. on: - push: - branches: - - main - - test-docs-generator # for testing - # Update version in installation instructions when new version is released - release: - types: - - released + workflow_dispatch: permissions: contents: read diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 08748034a..4d3a17701 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -1,13 +1,7 @@ name: Examples +# Community fork: the examples run is slow and creates billable resources; run manually only. on: - pull_request: - paths: - - ".github/workflows/examples.yml" - - "**.go" - - "go.mod" - - "go.sum" - - "examples/**.md" workflow_dispatch: concurrency: @@ -39,8 +33,7 @@ jobs: version: 'latest' - name: Test examples env: - UPCLOUD_USERNAME: ${{ secrets.UPCLOUD_USERNAME }} - UPCLOUD_PASSWORD: ${{ secrets.UPCLOUD_PASSWORD }} + UPCLOUD_TOKEN: ${{ secrets.UPCLOUD_TOKEN }} run: | upctl version upctl account show > /dev/null # Ensure that credentials are valid @@ -60,8 +53,7 @@ jobs: - name: Install upctl uses: UpCloudLtd/upcloud-cli-action@7e373972e386da6ef8c8336be8a855bafeafcbc8 # v1.0.2 with: - username: ${{ secrets.UPCLOUD_USERNAME }} - password: ${{ secrets.UPCLOUD_PASSWORD }} + token: ${{ secrets.UPCLOUD_TOKEN }} env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Purge resources diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f99c59392..822a1386b 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -8,6 +8,11 @@ on: - '.github/workflows/lint.yml' - '.golangci.yaml' - '.tool-versions' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + permissions: contents: read jobs: diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index f5142a408..3ee61c7de 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -24,11 +24,6 @@ jobs: # uses: docker/setup-qemu-action@2b82ce82d56a2a04d2637cd93a637ae1b359c0a7 # v2.2.0 - name: Set up Docker Buildx uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0 - - name: DockerHub Login - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - name: GitHub Container Registry Login uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0 with: @@ -51,7 +46,6 @@ jobs: args: release --clean --release-notes=.release_notes env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }} - name: Clear Docker login session run: rm -f ${HOME}/.docker/config.json - name: Capture dist checksums in an output string @@ -63,27 +57,6 @@ jobs: echo "EOF" } >>"$GITHUB_OUTPUT" - aur: - name: AUR - runs-on: ubuntu-latest - needs: - - publish_release - steps: - - name: Checkout head - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - - name: Generate AUR PKGBUILD - run: ./scripts/generate_aur_pkgbuild.sh ${{ github.ref_name }} - - name: Publish AUR package - uses: KSXGitHub/github-actions-deploy-aur@da03e160361ce01bf087e790b6ffd196d7dccff7 # v4.1.3 - with: - pkgname: upcloud-cli - pkgbuild: PKGBUILD - commit_username: ${{ secrets.AUR_USERNAME }} - commit_email: ${{ secrets.AUR_EMAIL }} - ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }} - commit_message: Release ${{ github.ref_name }} - force_push: true - attest: name: Attest needs: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 81fb608b3..721c48cf0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,6 +5,11 @@ on: - '**.go' - 'go.mod' - 'go.sum' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + permissions: contents: read jobs: diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 9aa5b6f09..2b75b26cb 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -103,9 +103,6 @@ brews: repository: owner: UpCloudLtd name: homebrew-tap - - # Optionally a token can be provided, if it differs from the token provided to GoReleaser - token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}" commit_author: name: goreleaserbot email: goreleaser@carlosbecker.com @@ -124,7 +121,8 @@ brews: # If set to auto, the release will not be uploaded to the homebrew tap # in case there is an indicator for prerelease in the tag e.g. v1.0.0-rc1 # Default is false. - skip_upload: false + # Community fork: never publish to the upstream tap. + skip_upload: true # Custom block for brew. # Can be used to specify alternate downloads for devel or head releases. @@ -156,21 +154,17 @@ dockers: use: buildx dockerfile: Dockerfile.goreleaser image_templates: - - "upcloud/upctl:latest" - - "upcloud/upctl:{{ .Tag }}" - - "upcloud/upctl:{{ .Major }}" - - "upcloud/upctl:{{ .Major }}.{{ .Minor }}" - - "ghcr.io/upcloudltd/upctl:latest" - - "ghcr.io/upcloudltd/upctl:{{ .Tag }}" - - "ghcr.io/upcloudltd/upctl:{{ .Major }}" - - "ghcr.io/upcloudltd/upctl:{{ .Major }}.{{ .Minor }}" + - "ghcr.io/upcloud-tools/upctl:latest" + - "ghcr.io/upcloud-tools/upctl:{{ .Tag }}" + - "ghcr.io/upcloud-tools/upctl:{{ .Major }}" + - "ghcr.io/upcloud-tools/upctl:{{ .Major }}.{{ .Minor }}" build_flag_templates: - "--platform=linux/amd64" - "--label=org.opencontainers.image.created={{.Date}}" - "--label=org.opencontainers.image.title=upctl" - "--label=org.opencontainers.image.revision={{.FullCommit}}" - "--label=org.opencontainers.image.version={{.Version}}" - - "--label=org.opencontainers.image.source=https://github.com/UpCloudLtd/upcloud-cli.git" + - "--label=org.opencontainers.image.source=https://github.com/upcloud-tools/upcloud-cli.git" sboms: - documents: @@ -180,7 +174,7 @@ release: # Repo in which the release will be created. # Default is extracted from the origin remote URL or empty if its private hosted. github: - owner: UpCloudLtd + owner: upcloud-tools name: upcloud-cli # If set to true, will not auto-publish the release. @@ -202,7 +196,8 @@ release: # in case there is an indicator for this in the tag e.g. v1.0.0-rc1 # If set to true, will mark the release as not ready for production. # Default is false. - prerelease: false + # Community fork: -community tags are semver prereleases. + prerelease: auto # What to do with the release notes in case there the release already exists. # diff --git a/CHANGELOG.md b/CHANGELOG.md index 393728bad..ecf0c4147 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.37.1-community] - 2026-11-15 + +### Changed + +- `kubernetes nodegroup scale` uses the v9 SDK. Its existing JSON and YAML output shape is preserved. +- Command help no longer shows a default value for required flags, such as `kubernetes nodegroup scale --count`. + +### Fixed + +- Fix `kubernetes nodegroup scale --count 0` silently doing nothing. The node count is now always sent to the API, so node groups can be scaled down to zero. + ## [3.37.0] - 2026-10-13 ### Added @@ -754,7 +765,8 @@ Initial public beta release :tada: ### Added - Current feature set added! First internal release -[Unreleased]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.37.0...HEAD +[Unreleased]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.1-community...HEAD +[3.37.1-community]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.0...v3.37.1-community [3.37.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.36.0...v3.37.0 [3.36.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.35.0...v3.36.0 [3.35.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.34.0...v3.35.0 diff --git a/FORK.md b/FORK.md new file mode 100644 index 000000000..2cd84abe1 --- /dev/null +++ b/FORK.md @@ -0,0 +1,49 @@ +# Community fork + +This repository is an independent community fork of [UpCloudLtd/upcloud-cli](https://github.com/UpCloudLtd/upcloud-cli). +It tracks upstream releases and carries fixes that have not (yet) been merged upstream. +Changes are kept small and self-contained so that upstream can merge them at any time. + +## Versioning + +- Fork releases are always suffixed with `-community`, e.g. `3.37.1-community` (git tag `v3.37.1-community`). +- The base version is the upstream release the fork is synced to; fork-only changes bump the patch (or minor) component on top of it. +- Like upstream, the binary version is derived from the git tag at build time (goreleaser injects it into `internal/config.Version`); release preparation only updates `CHANGELOG.md`. +- Compare links for `-community` versions point to this fork; links for upstream versions point to UpCloudLtd/upcloud-cli. + +## Feature parity + +**Base: upstream v3.37.0 (2026-10-13).** The fork contains all upstream features; no upstream feature is removed or disabled. + +### Fork-only changes + +| Change | Introduced in | Related issue | Upstream status | +| ------ | ------------- | ------------- | --------------- | +| `kubernetes node-group scale --count 0` sends `{"count": 0}` instead of silently doing nothing. The command now uses the v9 SDK; JSON/YAML output shape is preserved. | 3.37.1-community | [#1](https://github.com/upcloud-tools/upcloud-cli/issues/1) | Not fixed as of upstream v3.37.0 | +| Command help no longer shows `Default:` for required flags (e.g. `scale --count` showed a misleading `Default: 0`). | 3.37.1-community | [#1](https://github.com/upcloud-tools/upcloud-cli/issues/1) | Not fixed as of upstream v3.37.0 | + +### Notes for upstream merging + +- The scale fix works around a bug in the `upcloud-go-api` v8 SDK: `request.ModifyKubernetesNodeGroup.Count` is tagged `json:"count,omitempty"`, so a zero count is dropped from the PATCH body and the API no-ops while the CLI reports success. The v9 SDK (`KubernetesNodeGroupPatch`) models `count` without `omitempty` and is not affected. Fixing the v8 SDK would additionally benefit all other v8 consumers. +- The help renderer change is in `internal/ui/usage.go` (`formatFlags`) and applies to every command with required flags. + +## CI and releases + +The fork runs the upstream workflows with a release pipeline adapted for the fork: + +- `test`, `lint`, `codeql`: run as upstream, no secrets needed. Superseded runs on the same PR or branch are cancelled automatically. +- `docs`: manual `workflow_dispatch` only here. Upstream deploys the MkDocs site to `gh-pages` on main pushes and releases; run it manually and enable GitHub Pages if you want the site. +- `examples`: manual `workflow_dispatch` only here. Run when necessary, as it creates billable resources. +- `publish`: on tags matching `v*.*.*` (e.g. `v3.37.1-community`), builds binaries, deb/rpm/apk packages, checksums, and SBOMs, creates a **draft** GitHub release on this fork (marked as prerelease for `-community` tags), pushes container images to `ghcr.io/upcloud-tools/upctl`, and attaches build provenance attestations. Publish the draft release manually after review. + +Differences from upstream: no Docker Hub (`upcloud/upctl`), or Homebrew tap publishing. Everything else uses the automatic `GITHUB_TOKEN`; no additional secrets are needed for releases. + +## Syncing with upstream + +```sh +git remote add upstream git@github.com:UpCloudLtd/upcloud-cli.git +git fetch upstream +git merge upstream/main +``` + +After syncing: update the base version above, drop fork-only changes that upstream merged from the table, and base the next `-community` version on the new upstream release. diff --git a/internal/commands/kubernetes/nodegroup/fakeapi_test.go b/internal/commands/kubernetes/nodegroup/fakeapi_test.go new file mode 100644 index 000000000..1f5c01782 --- /dev/null +++ b/internal/commands/kubernetes/nodegroup/fakeapi_test.go @@ -0,0 +1,74 @@ +package nodegroup + +import ( + "io" + "net/http" + "net/http/httptest" + "net/url" + "sync" + "testing" + + upcloudv9 "github.com/UpCloudLtd/upcloud-go-api/v9/pkg/upcloud" + "github.com/stretchr/testify/require" +) + +type apiRequest struct { + Method string + Path string + Query url.Values + Body []byte +} + +type fakeResponse struct { + status int + body string + contentType string +} + +// fakeAPI serves canned responses keyed by "METHOD /path" and records every request. +type fakeAPI struct { + mu sync.Mutex + requests []apiRequest + client *upcloudv9.ClientWithResponses +} + +func newFakeAPI(t *testing.T, responses map[string]fakeResponse) *fakeAPI { + t.Helper() + api := &fakeAPI{} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + if err != nil { + t.Errorf("reading request body: %v", err) + } + key := r.Method + " " + r.URL.Path + api.mu.Lock() + api.requests = append(api.requests, apiRequest{Method: r.Method, Path: r.URL.Path, Query: r.URL.Query(), Body: body}) + api.mu.Unlock() + + res, ok := responses[key] + if !ok { + t.Errorf("unexpected request %s", key) + w.WriteHeader(http.StatusInternalServerError) + return + } + contentType := res.contentType + if contentType == "" { + contentType = "application/json" + } + w.Header().Set("Content-Type", contentType) + w.WriteHeader(res.status) + _, _ = io.WriteString(w, res.body) + })) + t.Cleanup(server.Close) + + client, err := upcloudv9.NewClientWithResponses(server.URL) + require.NoError(t, err) + api.client = client + return api +} + +func (a *fakeAPI) all() []apiRequest { + a.mu.Lock() + defer a.mu.Unlock() + return append([]apiRequest(nil), a.requests...) +} diff --git a/internal/commands/kubernetes/nodegroup/scale.go b/internal/commands/kubernetes/nodegroup/scale.go index 420b36af8..22e64af53 100644 --- a/internal/commands/kubernetes/nodegroup/scale.go +++ b/internal/commands/kubernetes/nodegroup/scale.go @@ -2,12 +2,15 @@ package nodegroup import ( "fmt" + "math" + "github.com/UpCloudLtd/upcloud-cli/v3/internal/apierror" "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" "github.com/UpCloudLtd/upcloud-cli/v3/internal/completion" "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" "github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver" - "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" + upcloudv9 "github.com/UpCloudLtd/upcloud-go-api/v9/pkg/upcloud" + "github.com/google/uuid" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -49,16 +52,35 @@ func (s *scaleCommand) ExecuteSingleArgument(exec commands.Executor, arg string) msg := fmt.Sprintf("Scaling node group %s of cluster %v", s.name, arg) exec.PushProgressStarted(msg) - res, err := exec.All().ModifyKubernetesNodeGroup(exec.Context(), &request.ModifyKubernetesNodeGroupRequest{ - ClusterUUID: arg, - Name: s.name, - NodeGroup: request.ModifyKubernetesNodeGroup{Count: s.count}, + client, err := v9Client(exec) + if err != nil { + return commands.HandleError(exec, msg, err) + } + id, err := uuid.Parse(arg) + if err != nil { + return commands.HandleError(exec, msg, fmt.Errorf("invalid kubernetes cluster UUID %q: %w", arg, err)) + } + + if s.count < 0 || s.count > math.MaxInt32 { + return commands.HandleError(exec, msg, fmt.Errorf("--count must be between 0 and %d, got %d", math.MaxInt32, s.count)) + } + + res, err := client.ModifyKubernetesClusterNodeGroupWithResponse(exec.Context(), id, s.name, upcloudv9.KubernetesNodeGroupPatch{ + Count: int32(s.count), //gosec:disable G115 -- guarded by range check above }) if err != nil { return commands.HandleError(exec, msg, err) } + if res.JSON200 == nil { + return commands.HandleError(exec, msg, apierror.FromResponse(res.StatusCode(), res.Body)) + } + + legacy, err := legacyNodeGroupOutput(res.JSON200) + if err != nil { + return commands.HandleError(exec, msg, err) + } exec.PushProgressSuccess(msg) - return output.OnlyMarshaled{Value: res}, nil + return output.OnlyMarshaled{Value: legacy}, nil } diff --git a/internal/commands/kubernetes/nodegroup/scale_test.go b/internal/commands/kubernetes/nodegroup/scale_test.go index 16e6de1ff..67ff46622 100644 --- a/internal/commands/kubernetes/nodegroup/scale_test.go +++ b/internal/commands/kubernetes/nodegroup/scale_test.go @@ -1,6 +1,8 @@ package nodegroup import ( + "net/http" + "strconv" "testing" "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" @@ -8,20 +10,23 @@ import ( smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock" "github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute" - "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" - "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestScaleKubernetesNodeGroup(t *testing.T) { clusterUUID := "28c80353-98fd-4221-85e0-82d7603756ba" + path := "/1.3/kubernetes/" + clusterUUID + "/node-groups/my-node-group" + nodeGroup := func(count int) string { + return `{"name": "my-node-group", "count": ` + strconv.Itoa(count) + `, "plan": "dev-md", "state": "running"}` + } for _, test := range []struct { - name string - args []string - expected request.ModifyKubernetesNodeGroupRequest - errorMsg string + name string + args []string + responses map[string]fakeResponse + body string + errorMsg string }{ { name: "no args", @@ -29,37 +34,90 @@ func TestScaleKubernetesNodeGroup(t *testing.T) { errorMsg: `required flag(s) "name", "count" not set`, }, { - name: "delete success", + name: "scale to positive count", args: []string{ clusterUUID, "--name", "my-node-group", "--count", "6", }, - expected: request.ModifyKubernetesNodeGroupRequest{ - ClusterUUID: clusterUUID, - Name: "my-node-group", - NodeGroup: request.ModifyKubernetesNodeGroup{Count: 6}, + responses: map[string]fakeResponse{"PATCH " + path: {status: http.StatusOK, body: nodeGroup(6)}}, + body: `{"count": 6}`, + }, + { + name: "scale to zero", + args: []string{ + clusterUUID, + "--name", "my-node-group", + "--count", "0", + }, + responses: map[string]fakeResponse{"PATCH " + path: {status: http.StatusOK, body: nodeGroup(0)}}, + body: `{"count": 0}`, + }, + { + name: "API error is reported", + args: []string{ + clusterUUID, + "--name", "my-node-group", + "--count", "0", + }, + responses: map[string]fakeResponse{"PATCH " + path: { + status: http.StatusUnprocessableEntity, + contentType: "application/problem+json", + body: `{"type": "https://developers.upcloud.com/1.3/errors#ERROR_INVALID_REQUEST", "title": "Invalid request", "status": 422}`, + }}, + body: `{"count": 0}`, + errorMsg: "Invalid request (type=https://developers.upcloud.com/1.3/errors#ERROR_INVALID_REQUEST, status=422)", + }, + { + name: "invalid UUID", + args: []string{ + "not-a-uuid", + "--name", "my-node-group", + "--count", "0", + }, + errorMsg: `invalid kubernetes cluster UUID "not-a-uuid": invalid UUID length: 10`, + }, + { + name: "negative count", + args: []string{ + clusterUUID, + "--name", "my-node-group", + "--count", "-1", + }, + errorMsg: "--count must be between 0 and 2147483647, got -1", + }, + { + name: "count overflows int32", + args: []string{ + clusterUUID, + "--name", "my-node-group", + "--count", "2147483648", }, + errorMsg: "--count must be between 0 and 2147483647, got 2147483648", }, } { t.Run(test.name, func(t *testing.T) { + api := newFakeAPI(t, test.responses) conf := config.New() - testCmd := ScaleCommand() - mService := new(smock.Service) - - expected := test.expected - mService.On("ModifyKubernetesNodeGroup", &expected).Return(&upcloud.KubernetesNodeGroup{}, nil) - - c := commands.BuildCommand(testCmd, nil, conf) - + c := commands.BuildCommand(ScaleCommand(), nil, conf) c.Cobra().SetArgs(test.args) - _, err := mockexecute.MockExecute(c, mService, conf) + + _, err := mockexecute.MockExecuteWithV9(c, &smock.Service{}, api.client, conf) if test.errorMsg != "" { assert.EqualError(t, err, test.errorMsg) } else { require.NoError(t, err) - mService.AssertNumberOfCalls(t, "ModifyKubernetesNodeGroup", 1) + } + + requests := api.all() + if test.body != "" { + require.Len(t, requests, 1) + assert.Equal(t, http.MethodPatch, requests[0].Method) + assert.Equal(t, path, requests[0].Path) + assert.JSONEq(t, test.body, string(requests[0].Body)) + } else { + assert.Empty(t, requests) } }) } diff --git a/internal/commands/kubernetes/nodegroup/v9.go b/internal/commands/kubernetes/nodegroup/v9.go new file mode 100644 index 000000000..846585d7e --- /dev/null +++ b/internal/commands/kubernetes/nodegroup/v9.go @@ -0,0 +1,32 @@ +package nodegroup + +import ( + "encoding/json" + "errors" + "fmt" + + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" + upcloudv8 "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" + upcloudv9 "github.com/UpCloudLtd/upcloud-go-api/v9/pkg/upcloud" +) + +func v9Client(exec commands.Executor) (*upcloudv9.ClientWithResponses, error) { + client := exec.V9() + if client == nil { + return nil, errors.New("UpCloud API v9 client is not available, run with --debug for details") + } + return client, nil +} + +// legacyNodeGroupOutput preserves the node group JSON/YAML contract while the API call uses v9. +func legacyNodeGroupOutput(ng *upcloudv9.KubernetesNodeGroup) (*upcloudv8.KubernetesNodeGroup, error) { + body, err := json.Marshal(ng) + if err != nil { + return nil, fmt.Errorf("cannot marshal v9 node group response: %w", err) + } + legacy := &upcloudv8.KubernetesNodeGroup{} + if err := json.Unmarshal(body, legacy); err != nil { + return nil, fmt.Errorf("cannot convert v9 node group response to legacy output: %w", err) + } + return legacy, nil +} diff --git a/internal/ui/usage.go b/internal/ui/usage.go index dc5a5e95c..e14866815 100644 --- a/internal/ui/usage.go +++ b/internal/ui/usage.go @@ -39,7 +39,8 @@ func formatFlags(fs *pflag.FlagSet) string { def = strings.TrimPrefix(def, "[") def = strings.TrimSuffix(def, "]") } - if def != "" { + _, required := flag.Annotations[cobra.BashCompOneRequiredFlag] + if def != "" && !required { fmt.Fprintf(&flagUsage, "\nDefault: %s", def) } t.Append(table.Row{flagText.String(), flagUsage.String()}) diff --git a/internal/ui/usage_test.go b/internal/ui/usage_test.go new file mode 100644 index 000000000..00cae1854 --- /dev/null +++ b/internal/ui/usage_test.go @@ -0,0 +1,24 @@ +package ui + +import ( + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFormatFlagsRequiredHidesDefault(t *testing.T) { + cmd := &cobra.Command{Use: "test"} + cmd.Flags().Int("count", 0, "Node count") + cmd.Flags().String("name", "default-name", "Node group name") + cmd.Flags().Int("replicas", 3, "Replica count") + require.NoError(t, cmd.MarkFlagRequired("count")) + require.NoError(t, cmd.MarkFlagRequired("name")) + + out := formatFlags(cmd.Flags()) + + assert.NotContains(t, out, "Default: 0") + assert.NotContains(t, out, "Default: default-name") + assert.Contains(t, out, "Default: 3") +}