Skip to content

Commit

Permalink
Merge pull request #2265 from navendu-pottekkat/navendu-pottekkat/-me…
Browse files Browse the repository at this point in the history
…sheryctl]/system-reset

[mesheryctl] Make 'system reset' context aware
  • Loading branch information
leecalcote committed Jan 22, 2021
2 parents b8df326 + 7fba4aa commit 2b359a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
22 changes: 12 additions & 10 deletions mesheryctl/internal/cli/root/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ type Version struct {

// MesheryCtlConfig is configuration structure of mesheryctl with contexts
type MesheryCtlConfig struct {
Contexts map[string]Context `yaml:"contexts"`
CurrentContext string `yaml:"current-context"`
Tokens map[string]Token `yaml:"tokens"`
Contexts map[string]Context `mapstructure:"contexts"`
CurrentContext string `mapstructure:"current-context"`
Tokens map[string]Token `mapstructure:"tokens"`
}

// Token defines the structure of Token stored in mesheryctl
type Token struct {
Name string `yaml:"name"`
Location string `yaml:"location"`
Name string `mapstructure:"name"`
Location string `mapstructure:"location"`
}

// Context defines a meshery environment
type Context struct {
Endpoint string `yaml:"endpoint"`
Token Token `yaml:"token"`
Platform string `yaml:"platform"`
Adapters []string `yaml:"adapters,omitempty"`
Endpoint string `mapstructure:"endpoint,omitempty"`
Token Token `mapstructure:"token,omitempty"`
Platform string `mapstructure:"platform"`
Adapters []string `mapstructure:"adapters,omitempty"`
Channel string `mapstructure:"channel,omitempty"`
Version string `mapstructure:"version,omitempty"`
}

// GetMesheryCtl returns a reference to the mesheryctl configuration object.
Expand All @@ -48,7 +50,7 @@ func GetMesheryCtl(v *viper.Viper) (*MesheryCtlConfig, error) {

// CheckIfCurrentContextIsValid checks if current context is valid
func (mc *MesheryCtlConfig) CheckIfCurrentContextIsValid() (Context, error) {
currentContext := viper.GetString("current-context")
currentContext := mc.CurrentContext
if currentContext == "" {
return Context{}, errors.New("current context not set")
}
Expand Down
29 changes: 27 additions & 2 deletions mesheryctl/internal/cli/root/system/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ package system
import (
"fmt"

"github.com/layer5io/meshery/mesheryctl/internal/cli/root/config"
"github.com/layer5io/meshery/mesheryctl/pkg/utils"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// resetCmd represents the reset command
Expand All @@ -38,10 +40,33 @@ var resetCmd = &cobra.Command{

// resets meshery config
func resetMesheryConfig() error {
log.Info("Meshery resetting...")
log.Info("Meshery resetting...\n")

mctlCfg, err := config.GetMesheryCtl(viper.GetViper())
if err != nil {
return errors.Wrap(err, "error processing config")
}

currentContext := mctlCfg.CurrentContext
currChannel := mctlCfg.Contexts[currentContext].Channel
currVersion := mctlCfg.Contexts[currentContext].Version

fileURL := ""

if currChannel == "edge" {
fileURL = "https://raw.githubusercontent.com/layer5io/meshery/master/docker-compose.yaml"
} else if currChannel == "stable" {
fileURL = "https://raw.githubusercontent.com/layer5io/meshery/" + currVersion + "/docker-compose.yaml"
}

log.Printf("Current Context: %s", currentContext)
log.Printf("Channel: %s", currChannel)
log.Printf("Version: %s\n", currVersion)
log.Printf("Fetching default docker-compose file at version: %s...\n", currVersion)

if err := utils.DownloadFile(utils.DockerComposeFile, fileURL); err != nil {
return errors.Wrapf(err, utils.SystemError(fmt.Sprintf("failed to download %s file from %s", utils.DockerComposeFile, fileURL)))
}
log.Info("Meshery config (" + utils.DockerComposeFile + ") reset to default settings.")
log.Info("...Meshery config (" + utils.DockerComposeFile + ") now reset to default settings.")
return nil
}

0 comments on commit 2b359a3

Please sign in to comment.