Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change flag "name" to "id" and add "all" flag #562

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions cmd/devstream/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

var plugin string
var instanceName string
var instanceID string
var statusAllFlag bool

var showCMD = &cobra.Command{
Use: "show",
Expand All @@ -30,8 +31,10 @@ var showStatusCMD = &cobra.Command{
Short: "Show status information",
Long: `Show status is used for showing plugins' status information.
Examples:
dtm show status --plugin=A-PLUGIN-NAME --name=A-PLUGIN-INSTANCE-NAME
dtm show status -p=A-PLUGIN-NAME -n=A-PLUGIN-INSTANCE-NAME`,
dtm show status --plugin=A-PLUGIN-NAME --id=A-PLUGIN-INSTANCE-ID
dtm show status -p=A-PLUGIN-NAME -i=INSTANCE-ID
dtm show status --all
dtm show status -a`,
Run: showStatusCMDFunc,
}

Expand All @@ -55,5 +58,6 @@ func init() {

showConfigCMD.PersistentFlags().StringVarP(&plugin, "plugin", "p", "", "specify name with the plugin")
showStatusCMD.PersistentFlags().StringVarP(&plugin, "plugin", "p", "", "specify name with the plugin")
showStatusCMD.PersistentFlags().StringVarP(&instanceName, "name", "n", "", "specify name with the plugin instance")
showStatusCMD.PersistentFlags().StringVarP(&instanceID, "id", "i", "", "specify id with the plugin instance")
showStatusCMD.PersistentFlags().BoolVarP(&statusAllFlag, "all", "a", false, "show all instances of all plugins status")
}
30 changes: 15 additions & 15 deletions internal/pkg/show/status/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

type Output struct {
Name string `yaml:"Name"`
Plugin string `yaml:"Plugin"`
Drifted bool `yaml:"Drifted"`
Options map[string]interface{} `yaml:"Options"`
Status *Status `yaml:"Status"`
InstanceID string `yaml:"InstanceID"`
Plugin string `yaml:"Plugin"`
Drifted bool `yaml:"Drifted"`
Options map[string]interface{} `yaml:"Options"`
Status *Status `yaml:"Status"`
}

type Status struct {
Expand All @@ -23,17 +23,17 @@ type Status struct {

// If the resource has drifted, status.State & status.Resource must NOT be nil and status.InlineStatus should be nil.
// If the resource hasn't drifted, status.State & status.Resource should be nil and status.InlineStatus must NOT be nil.
func NewOutput(name, plugin string, options map[string]interface{}, status *Status) (*Output, error) {
if ok, err := validateParams(name, plugin, options, status); !ok {
func NewOutput(instanceID, plugin string, options map[string]interface{}, status *Status) (*Output, error) {
if ok, err := validateParams(instanceID, plugin, options, status); !ok {
return nil, err
}

output := &Output{
Name: name,
Plugin: plugin,
Drifted: false,
Options: options,
Status: status,
InstanceID: instanceID,
Plugin: plugin,
Drifted: false,
Options: options,
Status: status,
}

if status.InlineStatus == nil {
Expand All @@ -57,9 +57,9 @@ func (o *Output) Print() error {
return nil
}

func validateParams(name, plugin string, options map[string]interface{}, status *Status) (bool, error) {
if name == "" || plugin == "" {
return false, fmt.Errorf("name or plugin cannot be nil")
func validateParams(instanceID, plugin string, options map[string]interface{}, status *Status) (bool, error) {
if instanceID == "" || plugin == "" {
return false, fmt.Errorf("instanceID or plugin cannot be nil")
}
if options == nil {
return false, fmt.Errorf("options cannot be nil")
Expand Down
30 changes: 15 additions & 15 deletions internal/pkg/show/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import (

func Show(configFile string) error {
plugin := viper.GetString("plugin")
name := viper.GetString("name")
if name == "" {
log.Warnf("Empty instance name. Maybe you forgot to add --name=PLUGIN_INSTANCE_NAME. The default value \"default\" will be used.")
name = "default"
}
id := viper.GetString("id")
allFlag := viper.GetBool("all")

// if --plugin="" and --name="", we set the allFlag to true, it means all plugins' status need to be printed
var allFlag = false
if plugin == "" {
if plugin == "" && id == "" {
allFlag = true
}

if id == "" && !allFlag {
log.Warnf("Empty instance name. Maybe you forgot to add --id=INSTANCE_ID. The default value \"default\" will be used.")
id = "default"
}

cfg, err := configloader.LoadConf(configFile)
if err != nil {
return err
Expand All @@ -44,7 +44,7 @@ func Show(configFile string) error {
if allFlag {
return showAll(smgr)
}
return showOne(smgr, name, plugin)
return showOne(smgr, id, plugin)
}

// show all plugins' status
Expand Down Expand Up @@ -76,23 +76,23 @@ func showAll(smgr statemanager.Manager) error {
}

// show one plugin status
func showOne(smgr statemanager.Manager, name, plugin string) error {
func showOne(smgr statemanager.Manager, id, plugin string) error {
// get state from statemanager
state := smgr.GetState(statemanager.GenerateStateKeyByToolNameAndPluginKind(name, plugin))
state := smgr.GetState(statemanager.GenerateStateKeyByToolNameAndPluginKind(id, plugin))
if state == nil {
return fmt.Errorf("state with (name: %s, plugin: %s) not found", name, plugin)
return fmt.Errorf("state with (id: %s, plugin: %s) not found", id, plugin)
}

// get state from read
tool := &configloader.Tool{
InstanceID: name,
InstanceID: id,
Name: plugin,
DependsOn: state.DependsOn,
Options: state.Options,
}
stateFromRead, err := pluginengine.Read(tool)
if err != nil {
log.Debugf("Failed to get the resource state with %s.%s. Error: %s.", name, plugin, err)
log.Debugf("Failed to get the resource state with %s.%s. Error: %s.", id, plugin, err)
return err
}

Expand All @@ -110,7 +110,7 @@ func showOne(smgr statemanager.Manager, name, plugin string) error {
}

// get the output
output, err := NewOutput(name, plugin, state.Options, status)
output, err := NewOutput(id, plugin, state.Options, status)
if err != nil {
log.Debugf("Failed to get the output: %s.", err)
}
Expand Down