Skip to content
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

Use github.com/spf13/cobra module to generate command line interface #70

Merged
merged 4 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ require (
github.com/arduino/go-paths-helper v1.6.1
github.com/arduino/golang-concurrent-workers v0.0.0-20170202182617-6710cdc954bc
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.5.2 // indirect
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18
Expand Down
491 changes: 468 additions & 23 deletions go.sum

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

// Package cli defines the command line interface.
package cli

import (
"os"

"github.com/arduino/libraries-repository-engine/internal/feedback"
"github.com/spf13/cobra"
)

// rootCmd defines the base CLI command.
var rootCmd = &cobra.Command{
Use: "libraries-repository-engine",
Long: "The tool for managing the Arduino Library Manager content.",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Aliases: []string{"sync"},
}

func init() {
rootCmd.PersistentFlags().String("config-file", "config.json", "Configuration file path")
}

// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() error {
// Support the old `libraries-repository-engine [CONFIG_FILE [REGISTRY_FILE]]` interface.
func() {
if len(os.Args) > 1 {
if os.Args[1][0] == '-' {
// The argument is a flag, so assume the new interface.
return
}

for _, command := range rootCmd.Commands() {
for _, alias := range append(command.Aliases, command.Name(), "help") { // Hacky to check "help" redundantly, but whatever.
if os.Args[1] == alias {
// The argument is a registered subcommand, so assume the new interface.
return
}
}
}

// The argument is not a registered subcommand, so assume it is the CONFIG_FILE positional argument of the old interface.
rootCmd.PersistentFlags().Set("config-file", os.Args[1]) // Transfer the argument to the new interface's flag.
os.Args = append([]string{os.Args[0]}, os.Args[2:]...) // Remove the argument.
}

feedback.Warning(
`Using deprecated command line syntax. New syntax:
libraries-repository-engine sync [--config-file=CONFIG_FILE] [REGISTRY_FILE]
`,
)
// Set the subcommand to the root's alias.
os.Args = append([]string{os.Args[0], rootCmd.Aliases[0]}, os.Args[1:]...)
}()

return rootCmd.Execute()
}
49 changes: 49 additions & 0 deletions internal/cli/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package cli

import (
"github.com/arduino/libraries-repository-engine/internal/command/sync"
"github.com/spf13/cobra"
)

// syncCmd defines the `sync` CLI subcommand.
var syncCmd = &cobra.Command{
Short: "Update Library Manager content",
Long: "Update the Library Manager content",
DisableFlagsInUseLine: true,
Use: `sync [FLAG]... [REGISTRY_FILE_PATH]

For each of the library registrations in the file at REGISTRY_FILE_PATH:

- check their repository for tags not already in the database
- check whether the new tag meets the requirements for addition to the index
- add library release to the database and store archive for the compliant tag
- generate the Library Manager index file`,
Run: sync.Run,
}

func init() {
rootCmd.AddCommand(syncCmd)
}
Loading