github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/scripts/scripts.go (about)

     1  package scripts
     2  
     3  import (
     4  	"github.com/ActiveState/cli/internal/locale"
     5  	"github.com/ActiveState/cli/internal/logging"
     6  	"github.com/ActiveState/cli/internal/output"
     7  	"github.com/ActiveState/cli/internal/primer"
     8  	"github.com/ActiveState/cli/internal/runbits/rationalize"
     9  	"github.com/ActiveState/cli/pkg/project"
    10  )
    11  
    12  type Scripts struct {
    13  	project *project.Project
    14  	output  output.Outputer
    15  }
    16  
    17  type primeable interface {
    18  	primer.Projecter
    19  	primer.Outputer
    20  	primer.Prompter
    21  	primer.Configurer
    22  }
    23  
    24  func NewScripts(prime primeable) *Scripts {
    25  	return &Scripts{
    26  		prime.Project(),
    27  		prime.Output(),
    28  	}
    29  }
    30  
    31  type scriptLine struct {
    32  	Name        string `json:"name,omitempty"`
    33  	Description string `json:"description,omitempty"`
    34  }
    35  
    36  func (s *Scripts) Run() error {
    37  	logging.Debug("Execute scripts command")
    38  
    39  	if s.project == nil {
    40  		return rationalize.ErrNoProject
    41  	}
    42  	s.output.Notice(locale.Tr("operating_message", s.project.NamespaceString(), s.project.Dir()))
    43  
    44  	name, owner := s.project.Name(), s.project.Owner()
    45  	logging.Debug("listing scripts for org=%s, project=%s", owner, name)
    46  
    47  	scripts := make([]scriptLine, len(s.project.Scripts()))
    48  	for i, s := range s.project.Scripts() {
    49  		scripts[i] = scriptLine{s.Name(), s.Description()}
    50  	}
    51  
    52  	var plainOutput interface{} = scripts
    53  	if len(scripts) == 0 {
    54  		plainOutput = locale.T("scripts_no_scripts")
    55  	}
    56  	s.output.Print(output.Prepare(plainOutput, scripts))
    57  	return nil
    58  }