Skip to content

Commit

Permalink
The new hasura inventory service search endpoint requires a timestamp.
Browse files Browse the repository at this point in the history
By always using the latest inventory timestamp, we don't need to pass around or specify timestamps anymore.

This eliminates the `--ts now` flag for package operations.
  • Loading branch information
mitchell-as committed Sep 5, 2024
1 parent 4ee317c commit 613fb5c
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 131 deletions.
15 changes: 0 additions & 15 deletions cmd/state/internal/cmdtree/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ func newInstallCommand(prime *primer.Values) *captain.Command {
locale.T("package_install_cmd_description"),
prime,
[]*captain.Flag{
{
Name: "ts",
Description: locale.T("package_flag_ts_description"),
Value: &params.Timestamp,
},
{
Name: "revision",
Shorthand: "r",
Expand Down Expand Up @@ -181,11 +176,6 @@ func newSearchCommand(prime *primer.Values) *captain.Command {
Description: locale.T("package_search_flag_exact-term_description"),
Value: &params.ExactTerm,
},
{
Name: "ts",
Description: locale.T("package_flag_ts_description"),
Value: &params.Timestamp,
},
},
[]*captain.Argument{
{
Expand Down Expand Up @@ -217,11 +207,6 @@ func newInfoCommand(prime *primer.Values) *captain.Command {
Description: locale.T("package_info_flag_language_description"),
Value: &params.Language,
},
{
Name: "ts",
Description: locale.T("package_flag_ts_description"),
Value: &params.Timestamp,
},
},
[]*captain.Argument{
{
Expand Down
4 changes: 1 addition & 3 deletions internal/locale/locales/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,6 @@ namespace_list_flag_project_description:
other: The namespace packages should be listed from
package_search_flag_ns_description:
other: The namespace to search within.
package_flag_ts_description:
other: The timestamp at which you want to query. Can be either 'now' or RFC3339 formatted timestamp.
package_flag_rev_description:
other: The revision you want to use. This ensures you get this exact revision and nothing else.
package_search_flag_language_description:
Expand Down Expand Up @@ -1455,7 +1453,7 @@ uploadingredient_success:
Revision: [ACTIONABLE]{{.V3}}[/RESET]
Timestamp: [ACTIONABLE]{{.V4}}[/RESET]
You can install this package by running `[ACTIONABLE]state install {{.V1}}/{{.V0}} --ts now`[/RESET].
You can install this package by running `[ACTIONABLE]state install {{.V1}}/{{.V0}}`[/RESET].
err_runtime_cache_invalid:
other: Your runtime needs to be updated. Please run '[ACTIONABLE]state refresh[/RESET]'.
err_buildscript_not_exist:
Expand Down
29 changes: 0 additions & 29 deletions internal/runbits/commits_runbit/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (

"github.com/ActiveState/cli/internal/captain"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
"github.com/ActiveState/cli/pkg/project"
)

// ExpandTime returns a timestamp based on the given "--ts" value.
Expand Down Expand Up @@ -36,30 +34,3 @@ func ExpandTime(ts *captain.TimeValue, auth *authentication.Auth) (time.Time, er

return latest, nil
}

// ExpandTimeForProject is the same as ExpandTime except that it ensures the returned time is either the same or
// later than that of the most recent commit.
func ExpandTimeForProject(ts *captain.TimeValue, auth *authentication.Auth, proj *project.Project) (time.Time, error) {
timestamp, err := ExpandTime(ts, auth)
if err != nil {
return time.Time{}, errs.Wrap(err, "Unable to expand time")
}

if proj != nil {
commitID, err := localcommit.Get(proj.Dir())
if err != nil {
return time.Time{}, errs.Wrap(err, "Unable to get commit ID")
}

atTime, err := model.FetchTimeStampForCommit(commitID, auth)
if err != nil {
return time.Time{}, errs.Wrap(err, "Unable to get commit time")
}

if atTime.After(timestamp) {
return *atTime, nil
}
}

return timestamp, nil
}
47 changes: 21 additions & 26 deletions internal/runbits/runtime/requirements/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/ActiveState/cli/internal/analytics"
anaConsts "github.com/ActiveState/cli/internal/analytics/constants"
Expand Down Expand Up @@ -130,7 +129,7 @@ type Requirement struct {

// ExecuteRequirementOperation executes the operation on the requirement
// This has become quite unwieldy, and is ripe for a refactor - https://activestatef.atlassian.net/browse/DX-1897
func (r *RequirementOperation) ExecuteRequirementOperation(ts *time.Time, requirements ...*Requirement) (rerr error) {
func (r *RequirementOperation) ExecuteRequirementOperation(requirements ...*Requirement) (rerr error) {
defer r.rationalizeError(&rerr)

if len(requirements) == 0 {
Expand All @@ -154,7 +153,7 @@ func (r *RequirementOperation) ExecuteRequirementOperation(ts *time.Time, requir
}
out.Notice(locale.Tr("operating_message", r.Project.NamespaceString(), r.Project.Dir()))

if err := r.resolveNamespaces(ts, requirements...); err != nil {
if err := r.resolveNamespaces(requirements...); err != nil {
return errs.Wrap(err, "Could not resolve namespaces")
}

Expand Down Expand Up @@ -200,7 +199,7 @@ func (r *RequirementOperation) ExecuteRequirementOperation(ts *time.Time, requir
}

bp := bpModel.NewBuildPlannerModel(r.Auth)
script, err := r.prepareBuildScript(bp, parentCommitID, requirements, ts)
script, err := r.prepareBuildScript(bp, parentCommitID, requirements)
if err != nil {
return errs.Wrap(err, "Could not prepare build script")
}
Expand Down Expand Up @@ -281,25 +280,21 @@ func (r *RequirementOperation) ExecuteRequirementOperation(ts *time.Time, requir
return nil
}

func (r *RequirementOperation) prepareBuildScript(bp *bpModel.BuildPlanner, parentCommit strfmt.UUID, requirements []*Requirement, ts *time.Time) (*buildscript.BuildScript, error) {
func (r *RequirementOperation) prepareBuildScript(bp *bpModel.BuildPlanner, parentCommit strfmt.UUID, requirements []*Requirement) (*buildscript.BuildScript, error) {
script, err := bp.GetBuildScript(string(parentCommit))
if err != nil {
return nil, errs.Wrap(err, "Failed to get build expression")
}

if ts != nil {
script.SetAtTime(*ts)
} else {
// If no atTime was provided then we need to ensure that the atTime in the script is updated to use
// the most recent, which is either the current value or the platform latest.
latest, err := model.FetchLatestTimeStamp(r.Auth)
if err != nil {
return nil, errs.Wrap(err, "Unable to fetch latest Platform timestamp")
}
atTime := script.AtTime()
if atTime == nil || latest.After(*atTime) {
script.SetAtTime(latest)
}
// Ensure that the atTime in the script is updated to use
// the most recent, which is either the current value or the platform latest.
latest, err := model.FetchLatestTimeStamp(r.Auth)
if err != nil {
return nil, errs.Wrap(err, "Unable to fetch latest Platform timestamp")
}
atTime := script.AtTime()
if atTime == nil || latest.After(*atTime) {
script.SetAtTime(latest)
}

for _, req := range requirements {
Expand Down Expand Up @@ -334,9 +329,9 @@ func (e ResolveNamespaceError) Error() string {
return "unable to resolve namespace"
}

func (r *RequirementOperation) resolveNamespaces(ts *time.Time, requirements ...*Requirement) error {
func (r *RequirementOperation) resolveNamespaces(requirements ...*Requirement) error {
for _, requirement := range requirements {
if err := r.resolveNamespace(ts, requirement); err != nil {
if err := r.resolveNamespace(requirement); err != nil {
if err != errNoLanguage {
err = errs.Pack(err, &ResolveNamespaceError{requirement.Name})
}
Expand All @@ -346,7 +341,7 @@ func (r *RequirementOperation) resolveNamespaces(ts *time.Time, requirements ...
return nil
}

func (r *RequirementOperation) resolveNamespace(ts *time.Time, requirement *Requirement) error {
func (r *RequirementOperation) resolveNamespace(requirement *Requirement) error {
requirement.langName = "undetermined"

if requirement.NamespaceType != nil {
Expand Down Expand Up @@ -386,7 +381,7 @@ func (r *RequirementOperation) resolveNamespace(ts *time.Time, requirement *Requ

var nsv model.Namespace
var supportedLang *medmodel.SupportedLanguage
requirement.Name, nsv, supportedLang, err = resolvePkgAndNamespace(r.Prompt, requirement.Name, *requirement.NamespaceType, supported, ts, r.Auth)
requirement.Name, nsv, supportedLang, err = resolvePkgAndNamespace(r.Prompt, requirement.Name, *requirement.NamespaceType, supported, r.Auth)
if err != nil {
return errs.Wrap(err, "Could not resolve pkg and namespace")
}
Expand Down Expand Up @@ -441,7 +436,7 @@ func (r *RequirementOperation) validatePackage(requirement *Requirement) error {
multilog.Error("Failed to normalize '%s': %v", requirement.Name, err)
}

packages, err := model.SearchIngredientsStrict(requirement.Namespace.String(), normalized, false, false, nil, r.Auth) // ideally case-sensitive would be true (PB-4371)
packages, err := model.SearchIngredientsStrict(requirement.Namespace.String(), normalized, false, false, r.Auth) // ideally case-sensitive would be true (PB-4371)
if err != nil {
return locale.WrapError(err, "package_err_cannot_obtain_search_results")
}
Expand Down Expand Up @@ -591,11 +586,11 @@ func supportedLanguageByName(supported []medmodel.SupportedLanguage, langName st
return funk.Find(supported, func(l medmodel.SupportedLanguage) bool { return l.Name == langName }).(medmodel.SupportedLanguage)
}

func resolvePkgAndNamespace(prompt prompt.Prompter, packageName string, nsType model.NamespaceType, supported []medmodel.SupportedLanguage, ts *time.Time, auth *authentication.Auth) (string, model.Namespace, *medmodel.SupportedLanguage, error) {
func resolvePkgAndNamespace(prompt prompt.Prompter, packageName string, nsType model.NamespaceType, supported []medmodel.SupportedLanguage, auth *authentication.Auth) (string, model.Namespace, *medmodel.SupportedLanguage, error) {
ns := model.NewBlankNamespace()

// Find ingredients that match the input query
ingredients, err := model.SearchIngredientsStrict("", packageName, false, false, ts, auth)
ingredients, err := model.SearchIngredientsStrict("", packageName, false, false, auth)
if err != nil {
return "", ns, nil, locale.WrapError(err, "err_pkgop_search_err", "Failed to check for ingredients.")
}
Expand Down Expand Up @@ -644,7 +639,7 @@ func resolvePkgAndNamespace(prompt prompt.Prompter, packageName string, nsType m
}

func getSuggestions(ns model.Namespace, name string, auth *authentication.Auth) ([]string, error) {
results, err := model.SearchIngredients(ns.String(), name, false, nil, auth)
results, err := model.SearchIngredients(ns.String(), name, false, auth)
if err != nil {
return []string{}, locale.WrapError(err, "package_ingredient_err_search", "Failed to resolve ingredient named: {{.V0}}", name)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (r *Initialize) Run(params *RunParams) (rerr error) {
}

func getKnownVersions(lang language.Language, auth *authentication.Auth) ([]string, error) {
pkgs, err := model.SearchIngredientsStrict(model.NewNamespaceLanguage().String(), lang.Requirement(), false, true, nil, auth)
pkgs, err := model.SearchIngredientsStrict(model.NewNamespaceLanguage().String(), lang.Requirement(), false, true, auth)
if err != nil {
return nil, errs.Wrap(err, "Failed to fetch Platform languages")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/languages/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (u *Update) Run(params *UpdateParams) error {
}

op := requirements.NewRequirementOperation(u.prime)
return op.ExecuteRequirementOperation(nil, &requirements.Requirement{
return op.ExecuteRequirementOperation(&requirements.Requirement{
Name: lang.Name,
Version: lang.Version,
NamespaceType: &model.NamespaceLanguage,
Expand Down
13 changes: 3 additions & 10 deletions internal/runners/packages/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/runbits/commits_runbit"
hsInventoryModel "github.com/ActiveState/cli/pkg/platform/api/hasura_inventory/model"
"github.com/ActiveState/cli/pkg/platform/api/vulnerabilities/request"
"github.com/ActiveState/cli/pkg/platform/authentication"
Expand All @@ -22,9 +21,8 @@ import (

// InfoRunParams tracks the info required for running Info.
type InfoRunParams struct {
Package captain.PackageValue
Timestamp captain.TimeValue
Language string
Package captain.PackageValue
Language string
}

// Info manages the information execution context.
Expand Down Expand Up @@ -70,12 +68,7 @@ func (i *Info) Run(params InfoRunParams, nstype model.NamespaceType) error {
normalized = params.Package.Name
}

ts, err := commits_runbit.ExpandTimeForProject(&params.Timestamp, i.auth, i.proj)
if err != nil {
return errs.Wrap(err, "Unable to get timestamp from params")
}

packages, err := model.SearchIngredientsStrict(ns.String(), normalized, false, false, &ts, i.auth) // ideally case-sensitive would be true (PB-4371)
packages, err := model.SearchIngredientsStrict(ns.String(), normalized, false, false, i.auth) // ideally case-sensitive would be true (PB-4371)
if err != nil {
return locale.WrapError(err, "package_err_cannot_obtain_search_results")
}
Expand Down
14 changes: 3 additions & 11 deletions internal/runners/packages/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package packages

import (
"github.com/ActiveState/cli/internal/captain"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/runbits/commits_runbit"
"github.com/ActiveState/cli/internal/runbits/runtime/requirements"
"github.com/ActiveState/cli/pkg/platform/api/buildplanner/types"
"github.com/ActiveState/cli/pkg/platform/model"
)

// InstallRunParams tracks the info required for running Install.
type InstallRunParams struct {
Packages captain.PackagesValue
Timestamp captain.TimeValue
Revision captain.IntValue
Packages captain.PackagesValue
Revision captain.IntValue
}

// Install manages the installing execution context.
Expand Down Expand Up @@ -52,10 +49,5 @@ func (a *Install) Run(params InstallRunParams, nsType model.NamespaceType) (rerr
reqs = append(reqs, req)
}

ts, err := commits_runbit.ExpandTimeForProject(&params.Timestamp, a.prime.Auth(), a.prime.Project())
if err != nil {
return errs.Wrap(err, "Unable to get timestamp from params")
}

return requirements.NewRequirementOperation(a.prime).ExecuteRequirementOperation(&ts, reqs...)
return requirements.NewRequirementOperation(a.prime).ExecuteRequirementOperation(reqs...)
}
12 changes: 3 additions & 9 deletions internal/runners/packages/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/runbits/commits_runbit"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/platform/api/vulnerabilities/request"
"github.com/ActiveState/cli/pkg/platform/authentication"
Expand All @@ -22,7 +21,6 @@ type SearchRunParams struct {
Language string
ExactTerm bool
Ingredient captain.PackageValueNoVersion
Timestamp captain.TimeValue
}

// Search manages the searching execution context.
Expand Down Expand Up @@ -59,16 +57,12 @@ func (s *Search) Run(params SearchRunParams, nstype model.NamespaceType) error {
ns = model.NewRawNamespace(params.Ingredient.Namespace)
}

ts, err := commits_runbit.ExpandTimeForProject(&params.Timestamp, s.auth, s.proj)
if err != nil {
return errs.Wrap(err, "Unable to get timestamp from params")
}

var packages []*model.IngredientAndVersion
var err error
if params.ExactTerm {
packages, err = model.SearchIngredientsLatestStrict(ns.String(), params.Ingredient.Name, true, true, &ts, s.auth)
packages, err = model.SearchIngredientsLatestStrict(ns.String(), params.Ingredient.Name, true, true, s.auth)
} else {
packages, err = model.SearchIngredientsLatest(ns.String(), params.Ingredient.Name, true, &ts, s.auth)
packages, err = model.SearchIngredientsLatest(ns.String(), params.Ingredient.Name, true, s.auth)
}
if err != nil {
return locale.WrapError(err, "package_err_cannot_obtain_search_results")
Expand Down
9 changes: 1 addition & 8 deletions internal/runners/packages/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package packages

import (
"github.com/ActiveState/cli/internal/captain"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/runbits/commits_runbit"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/internal/runbits/runtime/requirements"
"github.com/ActiveState/cli/pkg/platform/api/buildplanner/types"
Expand Down Expand Up @@ -51,10 +49,5 @@ func (u *Uninstall) Run(params UninstallRunParams, nsType model.NamespaceType) (
reqs = append(reqs, req)
}

ts, err := commits_runbit.ExpandTimeForProject(&captain.TimeValue{}, u.prime.Auth(), u.prime.Project())
if err != nil {
return errs.Wrap(err, "Unable to get timestamp from params")
}

return requirements.NewRequirementOperation(u.prime).ExecuteRequirementOperation(&ts, reqs...)
return requirements.NewRequirementOperation(u.prime).ExecuteRequirementOperation(reqs...)
}
1 change: 0 additions & 1 deletion internal/runners/platforms/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (a *Add) Run(ps AddRunParams) error {
}

if err := requirements.NewRequirementOperation(a.prime).ExecuteRequirementOperation(
nil,
&requirements.Requirement{
Name: params.name,
Version: params.version,
Expand Down
1 change: 0 additions & 1 deletion internal/runners/platforms/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (r *Remove) Run(ps RemoveRunParams) error {
}

if err := requirements.NewRequirementOperation(r.prime).ExecuteRequirementOperation(
nil,
&requirements.Requirement{
Name: params.name,
Version: params.version,
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (r *Runner) Run(params *Params) error {

if ingredient == nil {
// Attempt to find the existing ingredient, if we didn't already get it from the version specific call above
ingredients, err := model.SearchIngredientsStrict(reqVars.Namespace, reqVars.Name, true, false, &latestRevisionTime, r.auth)
ingredients, err := model.SearchIngredientsStrict(reqVars.Namespace, reqVars.Name, true, false, r.auth)
var errSearch404 *model.ErrSearch404
if err != nil && !errors.As(err, &errSearch404) { // 404 means either the ingredient or the namespace was not found, which is fine
return locale.WrapError(err, "err_uploadingredient_search", "Could not search for ingredient")
Expand Down
Loading

0 comments on commit 613fb5c

Please sign in to comment.