Skip to content

Commit

Permalink
Remove redundant log
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sverdlov <[email protected]>
  • Loading branch information
sverdlov93 committed Sep 22, 2024
1 parent 160cd26 commit 8c75538
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
27 changes: 27 additions & 0 deletions general/ai/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type ApiCommand string
const (
cliAiAppApiUrl = "https://cli-ai-app-stg.jfrog.info/api/"
askRateLimitHeader = "X-JFrog-CLI-AI"
// The latest version of the terms and conditions for using the AI interface.
aiTermsRevision = 1
)

type ApiType string
Expand All @@ -44,6 +46,12 @@ func HowCmd(c *cli.Context) error {
log.Output(coreutils.PrintLink("This AI-based interface converts your natural language inputs into fully functional JFrog CLI commands.\n" +
"NOTE: This is an experimental version and it supports mostly Artifactory and Xray commands.\n"))

// Ask the user to agree to the terms and conditions. If the user does not agree, the command will not proceed.
// Ask this only once per user unless the terms are updated.
if agreed, err := handleAiTermsAgreement(); err != nil || agreed != true {

Check failure on line 51 in general/ai/cli.go

View workflow job for this annotation

GitHub Actions / Static Check ubuntu-latest

S1002: should omit comparison to bool constant, can be simplified to `!agreed` (gosimple)
return err
}

for {
var question string
scanner := bufio.NewScanner(os.Stdin)
Expand Down Expand Up @@ -176,3 +184,22 @@ func sendRestAPI(apiType ApiType, content interface{}) (response string, err err
response = strings.TrimSpace(string(body))
return
}

func handleAiTermsAgreement() (bool, error) {
latestTermsVer, err := cliutils.GetLatestAiTermsRevision()
if err != nil {
return false, err
}
if latestTermsVer == nil || *latestTermsVer < aiTermsRevision {
if !coreutils.AskYesNo("By using this CLI interface you on behalf of your organization agree to JFrog's terms and conditions.\n"+
"Review these terms at https://docs.jfrog-applications.jfrog.io/jfrog-applications/jfrog-cli/ai.\n"+
"Do you agree?", false) {
return false, nil
}
if err = cliutils.SetLatestAiTermsRevision(aiTermsRevision); err != nil {
return false, err
}
log.Output()
}
return true, nil
}
28 changes: 14 additions & 14 deletions utils/cliutils/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const persistenceFileName = "persistence.json"

// PersistenceInfo represents the fields we are persisting
type PersistenceInfo struct {
LatestVersionCheckTime *int64 `json:"latestVersionCheckTime,omitempty"`
AiTermsVersion *int `json:"aiTermsVersion,omitempty"`
LatestCliVersionCheckTime *int64 `json:"latestCliVersionCheckTime,omitempty"`
LatestAiTermsRevision *int `json:"latestAiTermsRevision,omitempty"`
}

var (
Expand All @@ -31,46 +31,46 @@ func init() {
persistenceFilePath = filepath.Join(homeDir, persistenceFileName)
}

// SetLatestVersionCheckTime updates the latest version check time in the persistence file
func SetLatestVersionCheckTime(timestamp int64) error {
// SetCliLatestVersionCheckTime updates the latest version check time in the persistence file
func SetCliLatestVersionCheckTime(timestamp int64) error {
info, err := getPersistenceInfo()
if err != nil {
return err
}

info.LatestVersionCheckTime = &timestamp
info.LatestCliVersionCheckTime = &timestamp
return setPersistenceInfo(info)
}

// GetLatestVersionCheckTime retrieves the latest version check time from the persistence file
func GetLatestVersionCheckTime() (*int64, error) {
// GetLatestCliVersionCheckTime retrieves the latest version check time from the persistence file
func GetLatestCliVersionCheckTime() (*int64, error) {
info, err := getPersistenceInfo()
if err != nil {
return nil, err
}

return info.LatestVersionCheckTime, nil
return info.LatestCliVersionCheckTime, nil
}

// SetAiTermsVersion updates the AI terms version in the persistence file
func SetAiTermsVersion(version int) error {
// SetLatestAiTermsRevision updates the AI terms version in the persistence file
func SetLatestAiTermsRevision(version int) error {
info, err := getPersistenceInfo()
if err != nil {
return err
}

info.AiTermsVersion = &version
info.LatestAiTermsRevision = &version
return setPersistenceInfo(info)
}

// GetAiTermsVersion retrieves the AI terms version from the persistence file
func GetAiTermsVersion() (*int, error) {
// GetLatestAiTermsRevision retrieves the AI terms version from the persistence file
func GetLatestAiTermsRevision() (*int, error) {
info, err := getPersistenceInfo()
if err != nil {
return nil, err
}

return info.AiTermsVersion, nil
return info.LatestAiTermsRevision, nil
}

// getPersistenceInfo reads the persistence file, creates it if it doesn't exist, and returns the persisted info
Expand Down
24 changes: 12 additions & 12 deletions utils/cliutils/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (
"time"
)

// TestSetAndGetLatestVersionCheckTime tests setting and getting the LatestVersionCheckTime
// TestSetAndGetLatestVersionCheckTime tests setting and getting the LatestCliVersionCheckTime
func TestSetAndGetLatestVersionCheckTime(t *testing.T) {
// Setup temporary directory
persistenceFilePath = filepath.Join(t.TempDir(), persistenceFileName)

// Set the timestamp
timestamp := time.Now().UnixMilli()
err := SetLatestVersionCheckTime(timestamp)
err := SetCliLatestVersionCheckTime(timestamp)
if err != nil {
t.Fatalf("Failed to set LatestVersionCheckTime: %v", err)
t.Fatalf("Failed to set LatestCliVersionCheckTime: %v", err)
}

// Get the timestamp
storedTimestamp, err := GetLatestVersionCheckTime()
storedTimestamp, err := GetLatestCliVersionCheckTime()
if err != nil {
t.Fatalf("Failed to get LatestVersionCheckTime: %v", err)
t.Fatalf("Failed to get LatestCliVersionCheckTime: %v", err)
}

// Assert equality
Expand All @@ -31,22 +31,22 @@ func TestSetAndGetLatestVersionCheckTime(t *testing.T) {
}
}

// TestSetAndGetAiTermsVersion tests setting and getting the AiTermsVersion
// TestSetAndGetAiTermsVersion tests setting and getting the LatestAiTermsRevision
func TestSetAndGetAiTermsVersion(t *testing.T) {
// Setup temporary directory
persistenceFilePath = filepath.Join(t.TempDir(), persistenceFileName)

// Set the AI terms version
version := 42
err := SetAiTermsVersion(version)
err := SetLatestAiTermsRevision(version)
if err != nil {
t.Fatalf("Failed to set AiTermsVersion: %v", err)
t.Fatalf("Failed to set LatestAiTermsRevision: %v", err)
}

// Get the AI terms version
storedVersion, err := GetAiTermsVersion()
storedVersion, err := GetLatestAiTermsRevision()
if err != nil {
t.Fatalf("Failed to get AiTermsVersion: %v", err)
t.Fatalf("Failed to get LatestAiTermsRevision: %v", err)
}

// Assert equality
Expand All @@ -68,9 +68,9 @@ func TestPersistenceFileCreation(t *testing.T) {

// Trigger file creation by setting version check time
timestamp := time.Now().UnixMilli()
err = SetLatestVersionCheckTime(timestamp)
err = SetCliLatestVersionCheckTime(timestamp)
if err != nil {
t.Fatalf("Failed to set LatestVersionCheckTime: %v", err)
t.Fatalf("Failed to set LatestCliVersionCheckTime: %v", err)
}

// Verify the persistence file was created
Expand Down
4 changes: 2 additions & 2 deletions utils/cliutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ func shouldCheckLatestCliVersion() (shouldCheck bool, err error) {
if strings.ToLower(os.Getenv(JfrogCliAvoidNewVersionWarning)) == "true" {
return
}
latestVersionCheckTime, err := GetLatestVersionCheckTime()
latestVersionCheckTime, err := GetLatestCliVersionCheckTime()
if err != nil {
return
}
Expand All @@ -708,7 +708,7 @@ func shouldCheckLatestCliVersion() (shouldCheck bool, err error) {
// Timestamp file exists and updated less than 6 hours ago, therefor no need to check version again
return
}
if err = SetLatestVersionCheckTime(timeNow); err != nil {
if err = SetCliLatestVersionCheckTime(timeNow); err != nil {
return
}
return true, nil
Expand Down
2 changes: 1 addition & 1 deletion utils/cliutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestShouldCheckLatestCliVersion(t *testing.T) {
assert.NoError(t, err)
assert.False(t, shouldCheck)

assert.NoError(t, SetLatestVersionCheckTime(time.Now().UnixMilli()-LatestCliVersionCheckInterval.Milliseconds()))
assert.NoError(t, SetCliLatestVersionCheckTime(time.Now().UnixMilli()-LatestCliVersionCheckInterval.Milliseconds()))
// Third run, more than 6 hours between runs, so should return true
shouldCheck, err = shouldCheckLatestCliVersion()
assert.NoError(t, err)
Expand Down

0 comments on commit 8c75538

Please sign in to comment.