Skip to content

Commit

Permalink
global improve
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl committed Aug 2, 2024
1 parent 08cf2e9 commit 055ff37
Show file tree
Hide file tree
Showing 25 changed files with 3,594 additions and 179 deletions.
4 changes: 2 additions & 2 deletions client/client.go → client/console_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Make(apiParameter ApiParameter) (*Client, error) {
func MakeFromEnv() (*Client, error) {
apiParameter := ApiParameter{
BaseUrl: os.Getenv("CDK_BASE_URL"),
Debug: strings.ToLower(os.Getenv("CDK_DEBUG")) == "true",
Debug: utils.CdkDebug(),
Cert: os.Getenv("CDK_CERT"),
Cacert: os.Getenv("CDK_CACERT"),
ApiKey: os.Getenv("CDK_API_KEY"),
Expand Down Expand Up @@ -310,7 +310,7 @@ func (client *Client) initKindFromApi() error {
return fmt.Errorf("Cannot parse openapi: %s", err)
}
strict := false
client.kinds, err = schema.GetKinds(strict)
client.kinds, err = schema.GetConsoleKinds(strict)
if err != nil {
fmt.Errorf("Cannot extract kinds from openapi: %s", err)
}
Expand Down
9 changes: 6 additions & 3 deletions client/gateway_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/conduktor/ctl/resource"
"github.com/conduktor/ctl/schema"
Expand Down Expand Up @@ -63,7 +62,7 @@ func MakeGateway(apiParameter GatewayApiParameter) (*GatewayClient, error) {
func MakeGatewayClientFromEnv() (*GatewayClient, error) {
apiParameter := GatewayApiParameter{
BaseUrl: os.Getenv("CDK_GATEWAY_BASE_URL"),
Debug: strings.ToLower(os.Getenv("CDK_DEBUG")) == "true",
Debug: utils.CdkDebug(),
CdkGatewayUser: os.Getenv("CDK_GATEWAY_USER"),
CdkGatewayPassword: os.Getenv("CDK_GATEWAY_PASSWORD"),
}
Expand Down Expand Up @@ -330,6 +329,10 @@ func (client *GatewayClient) DeleteInterceptor(kind *schema.Kind, name string, p
return err
}

func (client *GatewayClient) ActivateDebug() {
client.client.SetDebug(true)
}

func (client *GatewayClient) Apply(resource *resource.Resource, dryMode bool) (string, error) {
kinds := client.GetKinds()
kind, ok := kinds[resource.Kind]
Expand Down Expand Up @@ -382,7 +385,7 @@ func (client *GatewayClient) initKindFromApi() error {
return fmt.Errorf("Cannot parse openapi: %s", err)
}
strict := false
client.kinds, err = schema.GetKinds(strict)
client.kinds, err = schema.GetGatewayKinds(strict)
if err != nil {
fmt.Errorf("Cannot extract kinds from openapi: %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cmd

import (
"fmt"
"os"

"github.com/conduktor/ctl/resource"
"github.com/conduktor/ctl/schema"
"github.com/spf13/cobra"
"os"
)

var dryRun *bool
Expand Down Expand Up @@ -40,7 +41,7 @@ func initApply(kinds schema.KindCatalog) {
if isGatewayResource(resource, kinds) {
upsertResult, err = gatewayApiClient().Apply(&resource, *dryRun)
} else {
upsertResult, err = apiClient().Apply(&resource, *dryRun)
upsertResult, err = consoleApiClient().Apply(&resource, *dryRun)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Could not apply resource %s/%s: %s\n", resource.Kind, resource.Name, err)
Expand Down
30 changes: 30 additions & 0 deletions cmd/consoleMkKind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/conduktor/ctl/schema"
"github.com/conduktor/ctl/utils"
"github.com/spf13/cobra"
)

func initConsoleMkKind() {
var prettyPrint *bool
var nonStrict *bool

var makeKind = &cobra.Command{
Use: "makeKind [file]",
Short: "Make kind json from openapi file if file not given it will read from api",
Long: ``,
Aliases: []string{"mkKind", "makeConsoleKind"},
Args: cobra.RangeArgs(0, 1),
Hidden: !utils.CdkDebug(),
Run: func(cmd *cobra.Command, args []string) {
runMkKind(cmd, args, *prettyPrint, *nonStrict, func() ([]byte, error) { return consoleApiClient().GetOpenApi() }, func(schema *schema.Schema, strict bool) (schema.KindCatalog, error) {
return schema.GetConsoleKinds(strict)
})
},
}
rootCmd.AddCommand(makeKind)

prettyPrint = makeKind.Flags().BoolP("pretty", "p", false, "Pretty print the output")
nonStrict = makeKind.Flags().BoolP("non-strict", "n", false, "Don't be strict on the parsing of the schema")
}
7 changes: 3 additions & 4 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/conduktor/ctl/schema"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,7 +32,7 @@ func initDelete(kinds schema.KindCatalog) {
err = gatewayApiClient().DeleteResourceInterceptors(&resource)
}
} else {
err = apiClient().DeleteResource(&resource)
err = consoleApiClient().DeleteResource(&resource)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Could not delete resource %s/%s: %s\n", resource.Kind, resource.Name, err)
Expand Down Expand Up @@ -66,7 +65,7 @@ func initDelete(kinds schema.KindCatalog) {
Use: fmt.Sprintf("%s [name]", name),
Short: "Delete resource of kind " + name,
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"},
Aliases: buildAlias(name),
Run: func(cmd *cobra.Command, args []string) {
parentValue := make([]string, len(parentFlagValue))
for i, v := range parentFlagValue {
Expand All @@ -76,7 +75,7 @@ func initDelete(kinds schema.KindCatalog) {
if isGatewayKind(kind) {
err = gatewayApiClient().Delete(&kind, parentValue, args[0])
} else {
err = apiClient().Delete(&kind, parentValue, args[0])
err = consoleApiClient().Delete(&kind, parentValue, args[0])
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
Expand Down
15 changes: 7 additions & 8 deletions cmd/delete_gateway_specifics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/conduktor/ctl/schema"
"github.com/spf13/cobra"
Expand All @@ -15,11 +14,11 @@ func buildDeleteByVClusterAndNameCmd(kind schema.Kind) *cobra.Command {
name := kind.GetName()
var nameValue string
var vClusterValue string
var aliasTopicDeleteCmd = &cobra.Command{
var deleteCmd = &cobra.Command{
Use: fmt.Sprintf("%s [name]", name),
Short: "Delete resource of kind " + name,
Args: cobra.ExactArgs(0),
Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"},
Aliases: buildAlias(name),
Run: func(cmd *cobra.Command, args []string) {
var err error
queryParams := make(map[string]string)
Expand All @@ -40,12 +39,12 @@ func buildDeleteByVClusterAndNameCmd(kind schema.Kind) *cobra.Command {
},
}

aliasTopicDeleteCmd.Flags().StringVar(&nameValue, nameFlag, "", "name of the "+name)
aliasTopicDeleteCmd.Flags().StringVar(&vClusterValue, vClusterFlag, "", "vCluster of the "+name)
deleteCmd.Flags().StringVar(&nameValue, nameFlag, "", "name of the "+name)
deleteCmd.Flags().StringVar(&vClusterValue, vClusterFlag, "", "vCluster of the "+name)

aliasTopicDeleteCmd.MarkFlagRequired(nameFlag)
deleteCmd.MarkFlagRequired(nameFlag)

return aliasTopicDeleteCmd
return deleteCmd
}

func buildDeleteInterceptorsCmd(kind schema.Kind) *cobra.Command {
Expand All @@ -62,7 +61,7 @@ func buildDeleteInterceptorsCmd(kind schema.Kind) *cobra.Command {
Use: fmt.Sprintf("%s [name]", name),
Short: "Delete resource of kind " + name,
Args: cobra.ExactArgs(0),
Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"},
Aliases: buildAlias(name),
Run: func(cmd *cobra.Command, args []string) {
var err error
queryParams := make(map[string]string)
Expand Down
30 changes: 30 additions & 0 deletions cmd/gatewayMkKind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/conduktor/ctl/schema"
"github.com/conduktor/ctl/utils"
"github.com/spf13/cobra"
)

func initGatewayMkKind() {
var prettyPrint *bool
var nonStrict *bool

var makeKind = &cobra.Command{
Use: "gatewayMakeKind [file]",
Short: "Make kind json from openapi file if file not given it will read from api",
Long: ``,
Aliases: []string{"gatewayMkKind", "gwMkKind"},
Args: cobra.RangeArgs(0, 1),
Hidden: !utils.CdkDebug(),
Run: func(cmd *cobra.Command, args []string) {
runMkKind(cmd, args, *prettyPrint, *nonStrict, func() ([]byte, error) { return gatewayApiClient().GetOpenApi() }, func(schema *schema.Schema, strict bool) (schema.KindCatalog, error) {
return schema.GetGatewayKinds(strict)
})
},
}
rootCmd.AddCommand(makeKind)

prettyPrint = makeKind.Flags().BoolP("pretty", "p", false, "Pretty print the output")
nonStrict = makeKind.Flags().BoolP("non-strict", "n", false, "Don't be strict on the parsing of the schema")
}
6 changes: 4 additions & 2 deletions cmd/gateway_utils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cmd

import (
"strings"

"github.com/conduktor/ctl/resource"
"github.com/conduktor/ctl/schema"
"strings"
)

func isGatewayKind(kind schema.Kind) bool {
return strings.Contains(kind.GetLatestKindVersion().ListPath, "gateway")
_, ok := kind.GetLatestKindVersion().(*schema.GatewayKindVersion)
return ok
}

func isGatewayResource(resource resource.Resource, kinds schema.KindCatalog) bool {
Expand Down
52 changes: 52 additions & 0 deletions cmd/genericMkKind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/conduktor/ctl/schema"
"github.com/spf13/cobra"
)

func runMkKind(cmd *cobra.Command, args []string, prettyPrint bool, nonStrict bool, getOpenApi func() ([]byte, error), getKinds func(*schema.Schema, bool) (schema.KindCatalog, error)) {
var kinds map[string]schema.Kind
if len(args) == 1 {
data, err := os.ReadFile(args[0])
if err != nil {
panic(err)
}
schema, err := schema.New(data)
if err != nil {
panic(err)
}
kinds, err = getKinds(schema, !nonStrict)
if err != nil {
panic(err)
}
} else {
data, err := getOpenApi()
if err != nil {
panic(err)
}
schema, err := schema.New(data)
if err != nil {
panic(err)
}
kinds, err = getKinds(schema, !nonStrict)
if err != nil {
panic(err)
}
}
var payload []byte
var err error
if prettyPrint {
payload, err = json.MarshalIndent(kinds, "", " ")
} else {
payload, err = json.Marshal(kinds)
}
if err != nil {
panic(err)
}
fmt.Print(string(payload))
}
7 changes: 3 additions & 4 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/conduktor/ctl/resource"
"github.com/conduktor/ctl/schema"
Expand Down Expand Up @@ -39,7 +38,7 @@ func initGet(kinds schema.KindCatalog) {
Short: "Get resource of kind " + name,
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
Long: `If name not provided it will list all resource`,
Aliases: []string{strings.ToLower(name), strings.ToLower(name) + "s", name + "s"},
Aliases: buildAlias(name),
Run: func(cmd *cobra.Command, args []string) {
parentValue := make([]string, len(parentFlagValue))
for i, v := range parentFlagValue {
Expand All @@ -51,7 +50,7 @@ func initGet(kinds schema.KindCatalog) {
if isGatewayKind(kind) {
result, err = gatewayApiClient().Get(&kind, parentValue)
} else {
result, err = apiClient().Get(&kind, parentValue)
result, err = consoleApiClient().Get(&kind, parentValue)
}
for _, r := range result {
r.PrintPreservingOriginalFieldOrder()
Expand All @@ -62,7 +61,7 @@ func initGet(kinds schema.KindCatalog) {
if isGatewayKind(kind) {
result, err = gatewayApiClient().Describe(&kind, parentValue, args[0])
} else {
result, err = apiClient().Describe(&kind, parentValue, args[0])
result, err = consoleApiClient().Describe(&kind, parentValue, args[0])
}
result.PrintPreservingOriginalFieldOrder()
}
Expand Down
Loading

0 comments on commit 055ff37

Please sign in to comment.