Skip to content

Commit dd1eab3

Browse files
authored
fixes #124 feat: adapt spf13/cobra library (#125)
Signed-off-by: Harsh4902 <[email protected]>
1 parent b49b511 commit dd1eab3

File tree

9 files changed

+449
-490
lines changed

9 files changed

+449
-490
lines changed

cmd/cmd.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,35 @@
1515
*/
1616
package cmd
1717

18-
// Command define single method interface
19-
type Command interface {
20-
Execute()
18+
import (
19+
"fmt"
20+
"os"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var rootCmd = &cobra.Command{
26+
Use: "microcks-cli",
27+
Short: "A CLI tool for Microcks",
28+
Long: `microcks-cli is a CLI for interacting with Microcks server APIs.
29+
It allows to launch tests or import API artifacts with minimal dependencies.`,
30+
Run: func(cmd *cobra.Command, args []string) {
31+
cmd.HelpFunc()(cmd, args)
32+
},
33+
CompletionOptions: cobra.CompletionOptions{
34+
HiddenDefaultCmd: true,
35+
},
36+
}
37+
38+
func Execute() {
39+
if err := rootCmd.Execute(); err != nil {
40+
fmt.Fprintln(os.Stderr, err)
41+
}
42+
}
43+
44+
func init() {
45+
rootCmd.AddCommand(NewImportCommand())
46+
rootCmd.AddCommand(NewVersionCommand())
47+
rootCmd.AddCommand(NewTestCommand())
48+
rootCmd.AddCommand(NewImportURLCommand())
2149
}

cmd/help.go

Lines changed: 0 additions & 73 deletions
This file was deleted.

cmd/import.go

Lines changed: 94 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -16,123 +16,117 @@
1616
package cmd
1717

1818
import (
19-
"flag"
2019
"fmt"
2120
"os"
2221
"strconv"
2322
"strings"
2423

2524
"github.com/microcks/microcks-cli/pkg/config"
2625
"github.com/microcks/microcks-cli/pkg/connectors"
26+
"github.com/spf13/cobra"
2727
)
2828

29-
type importComamnd struct {
30-
}
31-
32-
// NewImportCommand build a new ImportCommand implementation
33-
func NewImportCommand() Command {
34-
return new(importComamnd)
35-
}
36-
37-
// Execute implementation of importComamnd structure
38-
func (c *importComamnd) Execute() {
39-
40-
// Parse subcommand args first.
41-
if len(os.Args) < 2 {
42-
fmt.Println("import command require <specificationFile1[:primary],specificationFile2[:primary]> args")
43-
os.Exit(1)
44-
}
45-
46-
specificationFiles := os.Args[2]
47-
48-
// Then parse flags.
49-
importCmd := flag.NewFlagSet("import", flag.ExitOnError)
50-
51-
var microcksURL string
52-
var keycloakURL string
53-
var keycloakClientID string
54-
var keycloakClientSecret string
55-
var insecureTLS bool
56-
var caCertPaths string
57-
var verbose bool
58-
59-
importCmd.StringVar(&microcksURL, "microcksURL", "", "Microcks API URL")
60-
importCmd.StringVar(&keycloakClientID, "keycloakClientId", "", "Keycloak Realm Service Account ClientId")
61-
importCmd.StringVar(&keycloakClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret")
62-
importCmd.BoolVar(&insecureTLS, "insecure", false, "Whether to accept insecure HTTPS connection")
63-
importCmd.StringVar(&caCertPaths, "caCerts", "", "Comma separated paths of CRT files to add to Root CAs")
64-
importCmd.BoolVar(&verbose, "verbose", false, "Produce dumps of HTTP exchanges")
65-
importCmd.Parse(os.Args[3:])
66-
67-
// Validate presence and values of flags.
68-
if len(microcksURL) == 0 {
69-
fmt.Println("--microcksURL flag is mandatory. Check Usage.")
70-
os.Exit(1)
71-
}
72-
if len(keycloakClientID) == 0 {
73-
fmt.Println("--keycloakClientId flag is mandatory. Check Usage.")
74-
os.Exit(1)
75-
}
76-
if len(keycloakClientSecret) == 0 {
77-
fmt.Println("--keycloakClientSecret flag is mandatory. Check Usage.")
78-
os.Exit(1)
79-
}
29+
func NewImportCommand() *cobra.Command {
30+
var (
31+
microcksURL string
32+
keycloakClientID string
33+
keycloakClientSecret string
34+
insecureTLS bool
35+
caCertPaths string
36+
verbose bool
37+
)
38+
var importCmd = &cobra.Command{
39+
Use: "import",
40+
Short: "import API artifacts on Microcks server",
41+
Long: `import API artifacts on Microcks server`,
42+
Run: func(cmd *cobra.Command, args []string) {
43+
// Parse subcommand args first.
44+
if len(os.Args) < 2 {
45+
fmt.Println("import command require <specificationFile1[:primary],specificationFile2[:primary]> args")
46+
os.Exit(1)
47+
}
8048

81-
// Collect optional HTTPS transport flags.
82-
if insecureTLS {
83-
config.InsecureTLS = true
84-
}
85-
if len(caCertPaths) > 0 {
86-
config.CaCertPaths = caCertPaths
87-
}
88-
if verbose {
89-
config.Verbose = true
90-
}
49+
specificationFiles := os.Args[2]
9150

92-
// Now we seems to be good ...
93-
// First - retrieve the Keycloak URL from Microcks configuration.
94-
mc := connectors.NewMicrocksClient(microcksURL)
95-
keycloakURL, err := mc.GetKeycloakURL()
96-
if err != nil {
97-
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
98-
os.Exit(1)
99-
}
51+
// Validate presence and values of flags.
52+
if len(microcksURL) == 0 {
53+
fmt.Println("--microcksURL flag is mandatory. Check Usage.")
54+
os.Exit(1)
55+
}
56+
if len(keycloakClientID) == 0 {
57+
fmt.Println("--keycloakClientId flag is mandatory. Check Usage.")
58+
os.Exit(1)
59+
}
60+
if len(keycloakClientSecret) == 0 {
61+
fmt.Println("--keycloakClientSecret flag is mandatory. Check Usage.")
62+
os.Exit(1)
63+
}
10064

101-
var oauthToken string = "unauthentifed-token"
102-
if keycloakURL != "null" {
103-
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
104-
kc := connectors.NewKeycloakClient(keycloakURL, keycloakClientID, keycloakClientSecret)
65+
// Collect optional HTTPS transport flags.
66+
if insecureTLS {
67+
config.InsecureTLS = true
68+
}
69+
if len(caCertPaths) > 0 {
70+
config.CaCertPaths = caCertPaths
71+
}
72+
if verbose {
73+
config.Verbose = true
74+
}
10575

106-
oauthToken, err = kc.ConnectAndGetToken()
107-
if err != nil {
108-
fmt.Printf("Got error when invoking Keycloack client: %s", err)
109-
os.Exit(1)
110-
}
111-
}
76+
// Now we seems to be good ...
77+
// First - retrieve the Keycloak URL from Microcks configuration.
78+
mc := connectors.NewMicrocksClient(microcksURL)
79+
keycloakURL, err := mc.GetKeycloakURL()
80+
if err != nil {
81+
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
82+
os.Exit(1)
83+
}
11284

113-
// Then - for each specificationFile, upload the artifact on Microcks Server.
114-
mc.SetOAuthToken(oauthToken)
85+
var oauthToken string = "unauthentifed-token"
86+
if keycloakURL != "null" {
87+
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
88+
kc := connectors.NewKeycloakClient(keycloakURL, keycloakClientID, keycloakClientSecret)
11589

116-
sepSpecificationFiles := strings.Split(specificationFiles, ",")
117-
for _, f := range sepSpecificationFiles {
118-
mainArtifact := true
90+
oauthToken, err = kc.ConnectAndGetToken()
91+
if err != nil {
92+
fmt.Printf("Got error when invoking Keycloack client: %s", err)
93+
os.Exit(1)
94+
}
95+
}
11996

120-
// Check if mainArtifact flag is provided.
121-
if strings.Contains(f, ":") {
122-
pathAndMainArtifact := strings.Split(f, ":")
123-
f = pathAndMainArtifact[0]
124-
mainArtifact, err = strconv.ParseBool(pathAndMainArtifact[1])
125-
if err != nil {
126-
fmt.Printf("Cannot parse '%s' as Bool, default to true\n", pathAndMainArtifact[1])
97+
// Then - for each specificationFile, upload the artifact on Microcks Server.
98+
mc.SetOAuthToken(oauthToken)
99+
100+
sepSpecificationFiles := strings.Split(specificationFiles, ",")
101+
for _, f := range sepSpecificationFiles {
102+
mainArtifact := true
103+
104+
// Check if mainArtifact flag is provided.
105+
if strings.Contains(f, ":") {
106+
pathAndMainArtifact := strings.Split(f, ":")
107+
f = pathAndMainArtifact[0]
108+
mainArtifact, err = strconv.ParseBool(pathAndMainArtifact[1])
109+
if err != nil {
110+
fmt.Printf("Cannot parse '%s' as Bool, default to true\n", pathAndMainArtifact[1])
111+
}
112+
}
113+
114+
// Try uploading this artifact.
115+
msg, err := mc.UploadArtifact(f, mainArtifact)
116+
if err != nil {
117+
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
118+
os.Exit(1)
119+
}
120+
fmt.Printf("Microcks has discovered '%s'\n", msg)
127121
}
128-
}
129122

130-
// Try uploading this artifact.
131-
msg, err := mc.UploadArtifact(f, mainArtifact)
132-
if err != nil {
133-
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
134-
os.Exit(1)
135-
}
136-
fmt.Printf("Microcks has discovered '%s'\n", msg)
123+
},
137124
}
125+
importCmd.Flags().StringVar(&microcksURL, "microcksURL", "", "Microcks API URL")
126+
importCmd.Flags().StringVar(&keycloakClientID, "keycloakClientId", "", "Keycloak Realm Service Account ClientId")
127+
importCmd.Flags().StringVar(&keycloakClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret")
128+
importCmd.Flags().BoolVar(&insecureTLS, "insecure", false, "Whether to accept insecure HTTPS connection")
129+
importCmd.Flags().StringVar(&caCertPaths, "caCerts", "", "Comma separated paths of CRT files to add to Root CAs")
130+
importCmd.Flags().BoolVar(&verbose, "verbose", false, "Produce dumps of HTTP exchanges")
131+
return importCmd
138132
}

0 commit comments

Comments
 (0)