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

     1  package packages
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ActiveState/cli/internal/captain"
     7  	"github.com/ActiveState/cli/internal/errs"
     8  	"github.com/ActiveState/cli/pkg/localcommit"
     9  	"github.com/ActiveState/cli/pkg/platform/authentication"
    10  	"github.com/ActiveState/cli/pkg/platform/model"
    11  	"github.com/ActiveState/cli/pkg/project"
    12  )
    13  
    14  // getTime returns a timestamp based on the given "--ts" value.
    15  // If "now" was given, returns "now" according to the platform.
    16  // If no timestamp was given but the current project's commit time is after the latest inventory
    17  // timestamp, returns that commit time.
    18  // Otherwise, returns the specified timestamp or nil (which falls back on the default Platform
    19  // timestamp for a given operation)
    20  func getTime(ts *captain.TimeValue, auth *authentication.Auth, proj *project.Project) (*time.Time, error) {
    21  	if ts.Now() {
    22  		latest, err := model.FetchLatestRevisionTimeStamp(auth)
    23  		if err != nil {
    24  			return nil, errs.Wrap(err, "Unable to determine latest revision time")
    25  		}
    26  		return &latest, nil
    27  	}
    28  
    29  	if ts.Time == nil && proj != nil {
    30  		latest, err := model.FetchLatestTimeStamp(auth)
    31  		if err != nil {
    32  			return nil, errs.Wrap(err, "Unable to fetch latest Platform timestamp")
    33  		}
    34  
    35  		commitID, err := localcommit.Get(proj.Dir())
    36  		if err != nil {
    37  			return nil, errs.Wrap(err, "Unable to get commit ID")
    38  		}
    39  
    40  		atTime, err := model.FetchTimeStampForCommit(commitID, auth)
    41  		if err != nil {
    42  			return nil, errs.Wrap(err, "Unable to get commit time")
    43  		}
    44  
    45  		if atTime.After(latest) {
    46  			return atTime, nil
    47  		}
    48  	}
    49  
    50  	return ts.Time, nil
    51  }