-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CLI command for configuration validation (#1497)
- Loading branch information
Showing
11 changed files
with
126 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/0xERR0R/blocky/log" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewValidateCommand creates new command instance | ||
func NewValidateCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "validate", | ||
Args: cobra.NoArgs, | ||
Short: "Validates the configuration", | ||
RunE: validateConfiguration, | ||
} | ||
} | ||
|
||
func validateConfiguration(_ *cobra.Command, _ []string) error { | ||
log.Log().Infof("Validating configuration file: %s", configPath) | ||
|
||
_, err := os.Stat(configPath) | ||
if err != nil && errors.Is(err, os.ErrNotExist) { | ||
return errors.New("configuration path does not exist") | ||
} | ||
|
||
err = initConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Log().Info("Configuration is valid") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/0xERR0R/blocky/helpertest" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Validate command", func() { | ||
var tmpDir *helpertest.TmpFolder | ||
BeforeEach(func() { | ||
tmpDir = helpertest.NewTmpFolder("config") | ||
}) | ||
When("Validate is called with not existing configuration file", func() { | ||
It("should terminate with error", func() { | ||
c := NewRootCommand() | ||
c.SetArgs([]string{"validate", "--config", "/notexisting/path.yaml"}) | ||
|
||
Expect(c.Execute()).Should(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("Validate is called with existing valid configuration file", func() { | ||
It("should terminate without error", func() { | ||
cfgFile := tmpDir.CreateStringFile("config.yaml", | ||
"upstreams:", | ||
" groups:", | ||
" default:", | ||
" - 1.1.1.1") | ||
|
||
c := NewRootCommand() | ||
c.SetArgs([]string{"validate", "--config", cfgFile.Path}) | ||
|
||
Expect(c.Execute()).Should(Succeed()) | ||
}) | ||
}) | ||
|
||
When("Validate is called with existing invalid configuration file", func() { | ||
It("should terminate with error", func() { | ||
cfgFile := tmpDir.CreateStringFile("config.yaml", | ||
"upstreams:", | ||
" groups:", | ||
" default:", | ||
" - 1.broken file") | ||
|
||
c := NewRootCommand() | ||
c.SetArgs([]string{"validate", "--config", cfgFile.Path}) | ||
|
||
Expect(c.Execute()).Should(HaveOccurred()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters