Skip to content
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
34 changes: 31 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,35 @@
*/
package cmd

// Command define single method interface
type Command interface {
Execute()
import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "microcks-cli",
Short: "A CLI tool for Microcks",
Long: `microcks-cli is a CLI for interacting with Microcks server APIs.
It allows to launch tests or import API artifacts with minimal dependencies.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}

func init() {
rootCmd.AddCommand(NewImportCommand())
rootCmd.AddCommand(NewVersionCommand())
rootCmd.AddCommand(NewTestCommand())
rootCmd.AddCommand(NewImportURLCommand())
}
73 changes: 0 additions & 73 deletions cmd/help.go

This file was deleted.

194 changes: 94 additions & 100 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,123 +16,117 @@
package cmd

import (
"flag"
"fmt"
"os"
"strconv"
"strings"

"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/spf13/cobra"
)

type importComamnd struct {
}

// NewImportCommand build a new ImportCommand implementation
func NewImportCommand() Command {
return new(importComamnd)
}

// Execute implementation of importComamnd structure
func (c *importComamnd) Execute() {

// Parse subcommand args first.
if len(os.Args) < 2 {
fmt.Println("import command require <specificationFile1[:primary],specificationFile2[:primary]> args")
os.Exit(1)
}

specificationFiles := os.Args[2]

// Then parse flags.
importCmd := flag.NewFlagSet("import", flag.ExitOnError)

var microcksURL string
var keycloakURL string
var keycloakClientID string
var keycloakClientSecret string
var insecureTLS bool
var caCertPaths string
var verbose bool

importCmd.StringVar(&microcksURL, "microcksURL", "", "Microcks API URL")
importCmd.StringVar(&keycloakClientID, "keycloakClientId", "", "Keycloak Realm Service Account ClientId")
importCmd.StringVar(&keycloakClientSecret, "keycloakClientSecret", "", "Keycloak Realm Service Account ClientSecret")
importCmd.BoolVar(&insecureTLS, "insecure", false, "Whether to accept insecure HTTPS connection")
importCmd.StringVar(&caCertPaths, "caCerts", "", "Comma separated paths of CRT files to add to Root CAs")
importCmd.BoolVar(&verbose, "verbose", false, "Produce dumps of HTTP exchanges")
importCmd.Parse(os.Args[3:])

// Validate presence and values of flags.
if len(microcksURL) == 0 {
fmt.Println("--microcksURL flag is mandatory. Check Usage.")
os.Exit(1)
}
if len(keycloakClientID) == 0 {
fmt.Println("--keycloakClientId flag is mandatory. Check Usage.")
os.Exit(1)
}
if len(keycloakClientSecret) == 0 {
fmt.Println("--keycloakClientSecret flag is mandatory. Check Usage.")
os.Exit(1)
}
func NewImportCommand() *cobra.Command {
var (
microcksURL string
keycloakClientID string
keycloakClientSecret string
insecureTLS bool
caCertPaths string
verbose bool
)
var importCmd = &cobra.Command{
Use: "import",
Short: "import API artifacts on Microcks server",
Long: `import API artifacts on Microcks server`,
Run: func(cmd *cobra.Command, args []string) {
// Parse subcommand args first.
if len(os.Args) < 2 {
fmt.Println("import command require <specificationFile1[:primary],specificationFile2[:primary]> args")
os.Exit(1)
}

// Collect optional HTTPS transport flags.
if insecureTLS {
config.InsecureTLS = true
}
if len(caCertPaths) > 0 {
config.CaCertPaths = caCertPaths
}
if verbose {
config.Verbose = true
}
specificationFiles := os.Args[2]

// Now we seems to be good ...
// First - retrieve the Keycloak URL from Microcks configuration.
mc := connectors.NewMicrocksClient(microcksURL)
keycloakURL, err := mc.GetKeycloakURL()
if err != nil {
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
os.Exit(1)
}
// Validate presence and values of flags.
if len(microcksURL) == 0 {
fmt.Println("--microcksURL flag is mandatory. Check Usage.")
os.Exit(1)
}
if len(keycloakClientID) == 0 {
fmt.Println("--keycloakClientId flag is mandatory. Check Usage.")
os.Exit(1)
}
if len(keycloakClientSecret) == 0 {
fmt.Println("--keycloakClientSecret flag is mandatory. Check Usage.")
os.Exit(1)
}

var oauthToken string = "unauthentifed-token"
if keycloakURL != "null" {
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
kc := connectors.NewKeycloakClient(keycloakURL, keycloakClientID, keycloakClientSecret)
// Collect optional HTTPS transport flags.
if insecureTLS {
config.InsecureTLS = true
}
if len(caCertPaths) > 0 {
config.CaCertPaths = caCertPaths
}
if verbose {
config.Verbose = true
}

oauthToken, err = kc.ConnectAndGetToken()
if err != nil {
fmt.Printf("Got error when invoking Keycloack client: %s", err)
os.Exit(1)
}
}
// Now we seems to be good ...
// First - retrieve the Keycloak URL from Microcks configuration.
mc := connectors.NewMicrocksClient(microcksURL)
keycloakURL, err := mc.GetKeycloakURL()
if err != nil {
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
os.Exit(1)
}

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

sepSpecificationFiles := strings.Split(specificationFiles, ",")
for _, f := range sepSpecificationFiles {
mainArtifact := true
oauthToken, err = kc.ConnectAndGetToken()
if err != nil {
fmt.Printf("Got error when invoking Keycloack client: %s", err)
os.Exit(1)
}
}

// Check if mainArtifact flag is provided.
if strings.Contains(f, ":") {
pathAndMainArtifact := strings.Split(f, ":")
f = pathAndMainArtifact[0]
mainArtifact, err = strconv.ParseBool(pathAndMainArtifact[1])
if err != nil {
fmt.Printf("Cannot parse '%s' as Bool, default to true\n", pathAndMainArtifact[1])
// Then - for each specificationFile, upload the artifact on Microcks Server.
mc.SetOAuthToken(oauthToken)

sepSpecificationFiles := strings.Split(specificationFiles, ",")
for _, f := range sepSpecificationFiles {
mainArtifact := true

// Check if mainArtifact flag is provided.
if strings.Contains(f, ":") {
pathAndMainArtifact := strings.Split(f, ":")
f = pathAndMainArtifact[0]
mainArtifact, err = strconv.ParseBool(pathAndMainArtifact[1])
if err != nil {
fmt.Printf("Cannot parse '%s' as Bool, default to true\n", pathAndMainArtifact[1])
}
}

// Try uploading this artifact.
msg, err := mc.UploadArtifact(f, mainArtifact)
if err != nil {
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
os.Exit(1)
}
fmt.Printf("Microcks has discovered '%s'\n", msg)
}
}

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