Skip to content

Commit

Permalink
fix: change local artifact validation logic from tag to if blocks (#…
Browse files Browse the repository at this point in the history
…183)

* fix config invalid validation tag

Signed-off-by: sh2 <[email protected]>

* disable silence errors option

Signed-off-by: sh2 <[email protected]>

* search plugin frontly

Signed-off-by: sh2 <[email protected]>

* fix lint

Signed-off-by: sh2 <[email protected]>

---------

Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 authored Dec 27, 2023
1 parent 42eaaba commit 6f534d2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
26 changes: 13 additions & 13 deletions cmd/gtctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ func NewRootCommand() *cobra.Command {
)

cmd := &cobra.Command{
Use: "gtctl",
Short: "gtctl is a command-line tool for managing GreptimeDB cluster.",
Long: fmt.Sprintf("%s\ngtctl is a command-line tool for managing GreptimeDB cluster.", GtctlTextBanner),
Version: version.Get().String(),
SilenceUsage: true,
SilenceErrors: true,
Use: "gtctl",
Short: "gtctl is a command-line tool for managing GreptimeDB cluster.",
Long: fmt.Sprintf("%s\ngtctl is a command-line tool for managing GreptimeDB cluster.", GtctlTextBanner),
Version: version.Get().String(),
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
type verboser interface {
SetVerbosity(log.Level)
Expand Down Expand Up @@ -77,15 +76,16 @@ func main() {
panic(err)
}

if err := NewRootCommand().Execute(); err != nil {
if pm.ShouldRun(err) {
if err := pm.Run(os.Args[1:]); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
if pm.ShouldRun(os.Args[1]) {
if err = pm.Run(os.Args[1:]); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}

if err = NewRootCommand().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
10 changes: 10 additions & 0 deletions pkg/cluster/baremetal/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func (c *Cluster) createCluster(ctx context.Context, options *opt.CreateOptions)
if c.config.Cluster.Artifact != nil {
if c.config.Cluster.Artifact.Local != "" {
binPath = c.config.Cluster.Artifact.Local

// Ensure the binary path exists.
if exist, _ := fileutils.IsFileExists(binPath); !exist {
return fmt.Errorf("greptimedb cluster artifact '%s' is not exist", binPath)
}
} else {
src, err := c.am.NewSource(artifacts.GreptimeBinName, c.config.Cluster.Artifact.Version,
artifacts.ArtifactTypeBinary, clusterOpt.UseGreptimeCNArtifacts)
Expand Down Expand Up @@ -124,6 +129,11 @@ func (c *Cluster) createEtcdCluster(ctx context.Context, options *opt.CreateOpti
if c.config.Etcd.Artifact != nil {
if c.config.Etcd.Artifact.Local != "" {
binPath = c.config.Etcd.Artifact.Local

// Ensure the binary path exists.
if exist, _ := fileutils.IsFileExists(binPath); !exist {
return fmt.Errorf("etcd artifact '%s' is not exist", binPath)
}
} else {
src, err := c.am.NewSource(artifacts.EtcdBinName, c.config.Etcd.Artifact.Version,
artifacts.ArtifactTypeBinary, etcdOpt.UseGreptimeCNArtifacts)
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type BareMetalClusterComponentsConfig struct {

type Artifact struct {
// Local is the local path of binary(greptime or etcd).
Local string `yaml:"local" validate:"omitempty,file"`
Local string `yaml:"local" validate:"omitempty,filepath"`

// Version is the release version of binary(greptime or etcd).
// Usually, it points to the version of binary of GitHub release.
Expand Down
10 changes: 6 additions & 4 deletions pkg/plugins/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"os/exec"
"path/filepath"
"strings"

fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

const (
Expand Down Expand Up @@ -66,9 +68,9 @@ func NewManager() (*Manager, error) {
}

// ShouldRun returns true whether you should run the plugin.
func (m *Manager) ShouldRun(err error) bool {
// The error is returned by cobra itself.
return strings.Contains(err.Error(), "unknown command")
func (m *Manager) ShouldRun(name string) bool {
_, err := m.searchPlugins(name)
return err == nil
}

// Run searches for the plugin and runs it.
Expand Down Expand Up @@ -102,7 +104,7 @@ func (m *Manager) searchPlugins(name string) (string, error) {
pluginName := m.prefix + name
for _, path := range m.searchPaths {
pluginPath := filepath.Join(path, pluginName)
if _, err := os.Stat(pluginPath); os.IsNotExist(err) {
if exist, _ := fileutils.IsFileExists(pluginPath); !exist {
continue
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/plugins/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package plugins

import (
"fmt"
"testing"
)

Expand All @@ -27,8 +26,7 @@ func TestPluginManager(t *testing.T) {
}

pluginName := "foo-plugin"
receiveErr := fmt.Errorf("unknown command: %s", pluginName)
if pm.ShouldRun(receiveErr) {
if pm.ShouldRun(pluginName) {
if err := pm.Run([]string{pluginName, "1", "2", "3"}); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 6f534d2

Please sign in to comment.