|
16 | 16 | package cmd |
17 | 17 |
|
18 | 18 | import ( |
19 | | - "flag" |
20 | 19 | "fmt" |
21 | 20 | "os" |
22 | 21 | "strconv" |
23 | 22 | "strings" |
24 | 23 |
|
25 | 24 | "github.com/microcks/microcks-cli/pkg/config" |
26 | 25 | "github.com/microcks/microcks-cli/pkg/connectors" |
| 26 | + "github.com/spf13/cobra" |
27 | 27 | ) |
28 | 28 |
|
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(µcksURL, "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 | + } |
80 | 48 |
|
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] |
91 | 50 |
|
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 | + } |
100 | 64 |
|
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 | + } |
105 | 75 |
|
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 | + } |
112 | 84 |
|
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) |
115 | 89 |
|
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 | + } |
119 | 96 |
|
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) |
127 | 121 | } |
128 | | - } |
129 | 122 |
|
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 | + }, |
137 | 124 | } |
| 125 | + importCmd.Flags().StringVar(µcksURL, "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 |
138 | 132 | } |
0 commit comments