Skip to content

Commit

Permalink
CLOUDP-225858: add changelog command skeleton (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored Aug 6, 2024
1 parent c088a5b commit ffa544c
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tools/cli/internal/cli/changelog/changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package changelog

import (
"github.com/spf13/cobra"
)

func Builder() *cobra.Command {
cmd := &cobra.Command{
Use: "changelog",
Short: "Manage the API Changelog for the OpenAPI spec.",
Annotations: map[string]string{
"toc": "true",
},
}

cmd.AddCommand(CreateBuilder())

return cmd
}
30 changes: 30 additions & 0 deletions tools/cli/internal/cli/changelog/changelog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package changelog

import (
"testing"

"github.com/mongodb/openapi/tools/cli/internal/test"
)

func TestBuilder(t *testing.T) {
test.CmdValidator(
t,
Builder(),
1,
[]string{},
)
}
67 changes: 67 additions & 0 deletions tools/cli/internal/cli/changelog/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package changelog

import (
"github.com/mongodb/openapi/tools/cli/internal/cli/flag"
"github.com/mongodb/openapi/tools/cli/internal/cli/usage"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

type Opts struct {
fs afero.Fs
basePath string
revisionPath string
dryRun bool
}

func (o *Opts) Run() error {
return nil
}

func (o *Opts) PreRunE(_ []string) error {
return nil
}

// Builder builds the merge command with the following signature:
// changelog create -b path_folder -r path_folder --dry-run
func CreateBuilder() *cobra.Command {
opts := &Opts{
fs: afero.NewOsFs(),
}

cmd := &cobra.Command{
Use: "create -b path_folder -r path_folder --dry-run ...",
Aliases: []string{"generate"},
Short: "Generate the changelog for the OpenAPI spec.",
Args: cobra.NoArgs,
PreRunE: func(_ *cobra.Command, args []string) error {
return opts.PreRunE(args)
},
RunE: func(_ *cobra.Command, _ []string) error {
return opts.Run()
},
}

cmd.Flags().StringVarP(&opts.basePath, flag.Base, flag.BaseShort, "", usage.BaseFolder)
cmd.Flags().StringVarP(&opts.revisionPath, flag.Revision, flag.RevisionShort, "", usage.RevisionFolder)
cmd.Flags().BoolVarP(&opts.dryRun, flag.DryRun, flag.DryRunShort, false, usage.DryRun)

_ = cmd.MarkFlagRequired(flag.Base)
_ = cmd.MarkFlagRequired(flag.Revision)

return cmd
}
4 changes: 4 additions & 0 deletions tools/cli/internal/cli/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package flag
const (
Base = "base"
BaseShort = "b"
Revision = "revision"
RevisionShort = "r"
External = "external"
ExternalShort = "e"
Output = "output"
Expand All @@ -29,4 +31,6 @@ const (
GitSha = "sha"
ExcludePrivatePaths = "exclude-private-paths"
ExcludePrivatePathsShort = "x"
DryRun = "dry-run"
DryRunShort = "d"
)
2 changes: 2 additions & 0 deletions tools/cli/internal/cli/root/openapi/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"runtime"

"github.com/mongodb/openapi/tools/cli/internal/cli/changelog"
"github.com/mongodb/openapi/tools/cli/internal/cli/merge"
"github.com/mongodb/openapi/tools/cli/internal/cli/split"
"github.com/mongodb/openapi/tools/cli/internal/cli/versions"
Expand Down Expand Up @@ -55,6 +56,7 @@ func Builder() *cobra.Command {
merge.Builder(),
split.Builder(),
versions.Builder(),
changelog.Builder(),
)
return rootCmd
}
Expand Down
3 changes: 3 additions & 0 deletions tools/cli/internal/cli/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ const (
Environment = "Environment to consider when generating the versioned OAS."
GitSha = "GitSHA to use as identifier (x-xgen-sha) of the generated specification."
ExcludePrivatePaths = "Exclude private paths from the generated specification."
BaseFolder = "Base folder where the command will store the output."
RevisionFolder = "Folder where the command will store the output."
DryRun = "Dry run mode. The command will not write any files."
)
40 changes: 40 additions & 0 deletions tools/cli/internal/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

// CmdValidator helps validate a cobra.Command, verifying the number of sub commands
// and the flags that are being defined for it.
func CmdValidator(t *testing.T, subject *cobra.Command, nSubCommands int, flags []string) {
t.Helper()
a := assert.New(t)
if len(subject.Commands()) != nSubCommands {
t.Errorf("Sub command count mismatch. Expected %d, got %d. Check the CmdValidator for your command.\n", nSubCommands, len(subject.Commands()))
}
if len(flags) == 0 {
a.False(subject.HasAvailableFlags(), "expected command to not have flags but it does")
return
}
a.True(subject.HasAvailableFlags(), "expected command to have flag but has none")
for _, f := range flags {
a.NotNilf(subject.Flags().Lookup(f), "command has no flag: %s", f)
}
}

0 comments on commit ffa544c

Please sign in to comment.