Skip to content

Commit

Permalink
Added state languages search.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Dec 19, 2023
1 parent b49e8c3 commit 3733aba
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 40 deletions.
5 changes: 4 additions & 1 deletion cmd/state/internal/cmdtree/cmdtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ func New(prime *primer.Values, args ...string) *CmdTree {
)

languagesCmd := newLanguagesCommand(prime)
languagesCmd.AddChildren(newLanguageInstallCommand(prime))
languagesCmd.AddChildren(
newLanguageInstallCommand(prime),
newLanguageSearchCommand(prime),
)

cleanCmd := newCleanCommand(prime)
cleanCmd.AddChildren(
Expand Down
16 changes: 16 additions & 0 deletions cmd/state/internal/cmdtree/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ func newLanguageInstallCommand(prime *primer.Values) *captain.Command {
},
).SetSupportsStructuredOutput()
}

func newLanguageSearchCommand(prime *primer.Values) *captain.Command {
runner := languages.NewSearch(prime)

return captain.NewCommand(
"search",
locale.Tl("languages_search_title", "Searching Languages"),
locale.Tl("languages_search_cmd_description", "Search for an available language to use in your project"),
prime,
[]*captain.Flag{},
[]*captain.Argument{},
func(ccmd *captain.Command, _ []string) error {
return runner.Run()
},
).SetSupportsStructuredOutput().SetUnstable(true)
}
4 changes: 2 additions & 2 deletions internal/language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func MakeByName(name string) Language {
}

// MakeByNameAndVersion will retrieve a language by a given name and version.
func MakeByNameAndVersion(name, version string) (Language, error) {
func MakeByNameAndVersion(name, version string) Language {
if strings.ToLower(name) == Python3.Requirement() {
name = Python3.String()
// Disambiguate python, preferring Python3.
Expand All @@ -130,7 +130,7 @@ func MakeByNameAndVersion(name, version string) (Language, error) {
name = Python2.String()
}
}
return MakeByName(name), nil
return MakeByName(name)
}

// MakeByText will retrieve a language by a given text
Expand Down
19 changes: 4 additions & 15 deletions internal/language/language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,55 +122,44 @@ func TestMakeByNameAndVersion(t *testing.T) {
version string
}
tests := []struct {
name string
args args
want Language
wantErr bool
name string
args args
want Language
}{
{
"Valid Python3 version",
args{"python", "3.6.6"},
Python3,
false,
},
{
"Valid Python2 version",
args{"python", "2.7.18"},
Python2,
false,
},
{
"Valid Python2 invalid patch",
args{"python", "2.7.18.1"},
Python2,
false,
},
{
"Valid Python3 invalid patch",
args{"python", "3.9"},
Python3,
false,
},
{
"Missing version",
args{"python", ""},
Python3,
false,
},
{
"Valid Perl version",
args{"perl", "5.28.1"},
Perl,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := MakeByNameAndVersion(tt.args.name, tt.args.version)
if (err != nil) != tt.wantErr {
t.Errorf("MakeByNameAndVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
got := MakeByNameAndVersion(tt.args.name, tt.args.version)
if got != tt.want {
t.Errorf("MakeByNameAndVersion() = %v, want %v", got, tt.want)
}
Expand Down
2 changes: 0 additions & 2 deletions internal/locale/locales/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1312,8 +1312,6 @@ arg_state_shell_namespace_description:
other: The namespace of the project you wish to start a virtual environment shell/prompt for, or just the project name if previously used
err_language_by_commit:
other: Could not get language from commit ID {{.V0}}
err_make_language:
other: Could not create language from commit ID
err_parse_project:
other: Could not parse project file at {{.V0}}
err_uninstall_privilege_mismatch:
Expand Down
6 changes: 1 addition & 5 deletions internal/runbits/checkout/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,5 @@ func getLanguage(commitID strfmt.UUID) (language.Language, error) {
return language.Unset, locale.WrapError(err, "err_language_by_commit", "", string(commitID))
}

lang, err := language.MakeByNameAndVersion(modelLanguage.Name, modelLanguage.Version)
if err != nil {
return language.Unset, locale.WrapError(err, "err_make_language")
}
return lang, nil
return language.MakeByNameAndVersion(modelLanguage.Name, modelLanguage.Version), nil
}
10 changes: 1 addition & 9 deletions internal/runners/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
return language.UnrecognizedLanguageError(languageName, language.RecognizedSupportedsNames())
}

lang, err := language.MakeByNameAndVersion(languageName, languageVersion)
if err != nil {
if inferred {
return locale.WrapError(err, "err_init_lang", "", languageName, languageVersion)
} else {
return locale.WrapInputError(err, "err_init_lang", "", languageName, languageVersion)
}
}

lang := language.MakeByNameAndVersion(languageName, languageVersion)
version, err := deriveVersion(lang, languageVersion)
if err != nil {
if inferred || !locale.IsInputError(err) {
Expand Down
42 changes: 42 additions & 0 deletions internal/runners/languages/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package languages

import (
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/language"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/primer"
"github.com/ActiveState/cli/pkg/platform/model"
)

// Search manages the searching execution context.
type Search struct {
out output.Outputer
}

// NewSearch prepares a search execution context for use.
func NewSearch(prime primer.Outputer) *Search {
return &Search{
out: prime.Output(),
}
}

// Run executes the search behavior.
func (s *Search) Run() error {
logging.Debug("Execute languages search")

modelLanguages, err := model.FetchLanguages()
if err != nil {
return errs.Wrap(err, "Unable to fetch languages")
}

supportedLanguages := []model.Language{}
for _, lang := range modelLanguages {
if language.MakeByNameAndVersion(lang.Name, lang.Version) == language.Unknown {
continue
}
supportedLanguages = append(supportedLanguages, lang)
}
s.out.Print(output.Prepare(supportedLanguages, supportedLanguages))
return nil
}
7 changes: 1 addition & 6 deletions internal/runners/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,7 @@ func fetchLanguage(commitID strfmt.UUID) (*language.Supported, string, error) {
return nil, "", errs.Wrap(err, "Failed to retrieve language information for headless commit")
}

l, err := language.MakeByNameAndVersion(lang.Name, lang.Version)
if err != nil {
return nil, "", errs.Wrap(err, "Failed to convert commit language to supported language")
}

ls := language.Supported{Language: l}
ls := language.Supported{Language: language.MakeByNameAndVersion(lang.Name, lang.Version)}
if !ls.Recognized() {
return nil, "", locale.NewError("err_push_invalid_language", lang.Name)
}
Expand Down
20 changes: 20 additions & 0 deletions test/integration/languages_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ func (suite *LanguagesIntegrationTestSuite) TestJSON() {
cp.Expect(`[{"name":"Python","version":`)
cp.ExpectExitCode(0)
AssertValidJSON(suite.T(), cp)

cp = ts.Spawn("languages", "search", "--output", "json")
cp.Expect(`[{"name":"perl","version":`)
cp.ExpectExitCode(0)
//AssertValidJSON(suite.T(), cp) // currently too big to fit in the terminal window for validation
}

func (suite *LanguagesIntegrationTestSuite) TestSearch() {
suite.OnlyRunForTags(tagsuite.Languages)
ts := e2e.New(suite.T(), false)
defer ts.Close()

cp := ts.Spawn("languages", "search")
cp.Expect("perl")
cp.Expect("5.32")
cp.Expect("python")
cp.Expect("3.11")
cp.Expect("ruby")
cp.Expect("3.2")
cp.ExpectExitCode(0)
}

func TestLanguagesIntegrationTestSuite(t *testing.T) {
Expand Down

0 comments on commit 3733aba

Please sign in to comment.