Skip to content

Commit 8473338

Browse files
authored
Merge pull request #70 from per1234/cobra
Use `github.com/spf13/cobra` module to generate command line interface
2 parents e709fb5 + 3248be6 commit 8473338

File tree

9 files changed

+1075
-360
lines changed

9 files changed

+1075
-360
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ require (
77
github.com/arduino/go-paths-helper v1.6.1
88
github.com/arduino/golang-concurrent-workers v0.0.0-20170202182617-6710cdc954bc
99
github.com/go-git/go-git/v5 v5.4.2
10-
github.com/google/go-cmp v0.5.2 // indirect
10+
github.com/spf13/cobra v1.2.1
11+
github.com/spf13/pflag v1.0.5
1112
github.com/stretchr/testify v1.7.0
1213
github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec
1314
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18

go.sum

Lines changed: 468 additions & 23 deletions
Large diffs are not rendered by default.

internal/cli/root.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// This file is part of libraries-repository-engine.
2+
//
3+
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
// You can be released from the requirements of the above licenses by purchasing
19+
// a commercial license. Buying such a license is mandatory if you want to
20+
// modify or otherwise use the software for commercial activities involving the
21+
// Arduino software without disclosing the source code of your own applications.
22+
// To purchase a commercial license, send an email to [email protected].
23+
24+
// Package cli defines the command line interface.
25+
package cli
26+
27+
import (
28+
"os"
29+
30+
"github.com/arduino/libraries-repository-engine/internal/feedback"
31+
"github.com/spf13/cobra"
32+
)
33+
34+
// rootCmd defines the base CLI command.
35+
var rootCmd = &cobra.Command{
36+
Use: "libraries-repository-engine",
37+
Long: "The tool for managing the Arduino Library Manager content.",
38+
CompletionOptions: cobra.CompletionOptions{
39+
DisableDefaultCmd: true,
40+
},
41+
Aliases: []string{"sync"},
42+
}
43+
44+
func init() {
45+
rootCmd.PersistentFlags().String("config-file", "config.json", "Configuration file path")
46+
}
47+
48+
// Execute adds all child commands to the root command and sets flags appropriately.
49+
func Execute() error {
50+
// Support the old `libraries-repository-engine [CONFIG_FILE [REGISTRY_FILE]]` interface.
51+
func() {
52+
if len(os.Args) > 1 {
53+
if os.Args[1][0] == '-' {
54+
// The argument is a flag, so assume the new interface.
55+
return
56+
}
57+
58+
for _, command := range rootCmd.Commands() {
59+
for _, alias := range append(command.Aliases, command.Name(), "help") { // Hacky to check "help" redundantly, but whatever.
60+
if os.Args[1] == alias {
61+
// The argument is a registered subcommand, so assume the new interface.
62+
return
63+
}
64+
}
65+
}
66+
67+
// The argument is not a registered subcommand, so assume it is the CONFIG_FILE positional argument of the old interface.
68+
rootCmd.PersistentFlags().Set("config-file", os.Args[1]) // Transfer the argument to the new interface's flag.
69+
os.Args = append([]string{os.Args[0]}, os.Args[2:]...) // Remove the argument.
70+
}
71+
72+
feedback.Warning(
73+
`Using deprecated command line syntax. New syntax:
74+
libraries-repository-engine sync [--config-file=CONFIG_FILE] [REGISTRY_FILE]
75+
`,
76+
)
77+
// Set the subcommand to the root's alias.
78+
os.Args = append([]string{os.Args[0], rootCmd.Aliases[0]}, os.Args[1:]...)
79+
}()
80+
81+
return rootCmd.Execute()
82+
}

internal/cli/sync.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This file is part of libraries-repository-engine.
2+
//
3+
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
// You can be released from the requirements of the above licenses by purchasing
19+
// a commercial license. Buying such a license is mandatory if you want to
20+
// modify or otherwise use the software for commercial activities involving the
21+
// Arduino software without disclosing the source code of your own applications.
22+
// To purchase a commercial license, send an email to [email protected].
23+
24+
package cli
25+
26+
import (
27+
"github.com/arduino/libraries-repository-engine/internal/command/sync"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
// syncCmd defines the `sync` CLI subcommand.
32+
var syncCmd = &cobra.Command{
33+
Short: "Update Library Manager content",
34+
Long: "Update the Library Manager content",
35+
DisableFlagsInUseLine: true,
36+
Use: `sync [FLAG]... [REGISTRY_FILE_PATH]
37+
38+
For each of the library registrations in the file at REGISTRY_FILE_PATH:
39+
40+
- check their repository for tags not already in the database
41+
- check whether the new tag meets the requirements for addition to the index
42+
- add library release to the database and store archive for the compliant tag
43+
- generate the Library Manager index file`,
44+
Run: sync.Run,
45+
}
46+
47+
func init() {
48+
rootCmd.AddCommand(syncCmd)
49+
}

0 commit comments

Comments
 (0)