Skip to content

Commit

Permalink
Refactor cmd/utils.go (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa authored Mar 3, 2024
1 parent 825c306 commit 9b6c038
Show file tree
Hide file tree
Showing 5 changed files with 895 additions and 897 deletions.
139 changes: 139 additions & 0 deletions cmd/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package cmd

import (
"context"
"encoding/json"
"log"
"os"

"github.com/gatewayd-io/gatewayd/config"
gerr "github.com/gatewayd-io/gatewayd/errors"
jsonSchemaGenerator "github.com/invopop/jsonschema"
"github.com/knadh/koanf"
koanfJson "github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/parsers/yaml"
jsonSchemaV5 "github.com/santhosh-tekuri/jsonschema/v5"
"github.com/spf13/cobra"
)

// generateConfig generates a config file of the given type.
func generateConfig(
cmd *cobra.Command, fileType configFileType, configFile string, forceRewriteFile bool,
) {
logger := log.New(cmd.OutOrStdout(), "", 0)

// Create a new config object and load the defaults.
conf := &config.Config{
GlobalKoanf: koanf.New("."),
PluginKoanf: koanf.New("."),
}
conf.LoadDefaults(context.TODO())

// Marshal the config file to YAML.
var konfig *koanf.Koanf
switch fileType {
case Global:
konfig = conf.GlobalKoanf
case Plugins:
konfig = conf.PluginKoanf
default:
logger.Fatal("Invalid config file type")
}
cfg, err := konfig.Marshal(yaml.Parser())
if err != nil {
logger.Fatal(err)
}

// Check if the config file already exists and if we should overwrite it.
exists := false
if _, err := os.Stat(configFile); err == nil && !forceRewriteFile {
logger.Fatal(
"Config file already exists. Use --force to overwrite or choose a different filename.")
} else if err == nil {
exists = true
}

// Create or overwrite the config file.
if err := os.WriteFile(configFile, cfg, FilePermissions); err != nil {
logger.Fatal(err)
}

verb := "created"
if exists && forceRewriteFile {
verb = "overwritten"
}
cmd.Printf("Config file '%s' was %s successfully.", configFile, verb)
}

// lintConfig lints the given config file of the given type.
func lintConfig(fileType configFileType, configFile string) error {
// Load the config file and check it for errors.
var conf *config.Config
switch fileType {
case Global:
conf = config.NewConfig(context.TODO(), configFile, "")
conf.LoadDefaults(context.TODO())
conf.LoadGlobalConfigFile(context.TODO())
conf.UnmarshalGlobalConfig(context.TODO())
case Plugins:
conf = config.NewConfig(context.TODO(), "", configFile)
conf.LoadDefaults(context.TODO())
conf.LoadPluginConfigFile(context.TODO())
conf.UnmarshalPluginConfig(context.TODO())
default:
return gerr.ErrLintingFailed
}

// Marshal the config to JSON.
var jsonData []byte
var err error
switch fileType {
case Global:
jsonData, err = conf.GlobalKoanf.Marshal(koanfJson.Parser())
case Plugins:
jsonData, err = conf.PluginKoanf.Marshal(koanfJson.Parser())
default:
return gerr.ErrLintingFailed
}
if err != nil {
return gerr.ErrLintingFailed.Wrap(err)
}

// Unmarshal the JSON data into a map.
var jsonBytes map[string]interface{}
err = json.Unmarshal(jsonData, &jsonBytes)
if err != nil {
return gerr.ErrLintingFailed.Wrap(err)
}

// Generate a JSON schema from the config struct.
var generatedSchema *jsonSchemaGenerator.Schema
switch fileType {
case Global:
generatedSchema = jsonSchemaGenerator.Reflect(&config.GlobalConfig{})
case Plugins:
generatedSchema = jsonSchemaGenerator.Reflect(&config.PluginConfig{})
default:
return gerr.ErrLintingFailed
}

// Marshal the schema to JSON.
schemaBytes, err := json.Marshal(generatedSchema)
if err != nil {
return gerr.ErrLintingFailed.Wrap(err)
}

// Compile the schema for validation.
schema, err := jsonSchemaV5.CompileString("", string(schemaBytes))
if err != nil {
return gerr.ErrLintingFailed.Wrap(err)
}

// Validate the config against the schema.
err = schema.Validate(jsonBytes)
if err != nil {
return gerr.ErrLintingFailed.Wrap(err)
}

return nil
}
3 changes: 3 additions & 0 deletions cmd/dsn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cmd

const DSN = "https://e22f42dbb3e0433fbd9ea32453faa598@o4504550475038720.ingest.sentry.io/4504550481723392"
Loading

0 comments on commit 9b6c038

Please sign in to comment.