-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDP-225858: add changelog command skeleton (#137)
- Loading branch information
1 parent
c088a5b
commit ffa544c
Showing
7 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 | ||
} |
This file contains 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,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{}, | ||
) | ||
} |
This file contains 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,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 | ||
} |
This file contains 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 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 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 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,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) | ||
} | ||
} |