Skip to content

Commit 58e982a

Browse files
feature(sscs-env-var): guarantee that env var has default string value
1 parent f5c957c commit 58e982a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

internal/wrappers/configuration/configuration.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,36 @@ func SafeWriteSingleConfigKey(configFilePath, key string, value int) error {
161161
return nil
162162
}
163163

164+
func SafeWriteSingleConfigKeyString(configFilePath, key string, value string) error {
165+
// Create a file lock
166+
lock := flock.New(configFilePath + ".lock")
167+
locked, err := lock.TryLock()
168+
if err != nil {
169+
return errors.Errorf("error acquiring lock: %s", err.Error())
170+
}
171+
if !locked {
172+
return errors.Errorf("could not acquire lock")
173+
}
174+
defer func() {
175+
_ = lock.Unlock()
176+
}()
177+
178+
// Load existing configuration or initialize a new one
179+
config, err := LoadConfig(configFilePath)
180+
if err != nil {
181+
return errors.Errorf("error loading config: %s", err.Error())
182+
}
183+
184+
// Update the configuration key
185+
config[key] = value
186+
187+
// Save the updated configuration back to the file
188+
if err = SaveConfig(configFilePath, config); err != nil {
189+
return errors.Errorf("error saving config: %s", err.Error())
190+
}
191+
return nil
192+
}
193+
164194
// LoadConfig loads the configuration from a file. If the file does not exist, it returns an empty map.
165195
func LoadConfig(path string) (map[string]interface{}, error) {
166196
config := make(map[string]interface{})

internal/wrappers/scan-overview-http.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@ package wrappers
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/checkmarx/ast-cli/internal/logger"
7+
"github.com/checkmarx/ast-cli/internal/wrappers/configuration"
68
"net/http"
79

810
commonParams "github.com/checkmarx/ast-cli/internal/params"
911
"github.com/pkg/errors"
1012
"github.com/spf13/viper"
1113
)
1214

15+
const defaultPath = "api/micro-engines/read/scans/%s/scan-overview"
16+
1317
type ScanOverviewHTTPWrapper struct {
1418
path string
1519
}
1620

1721
func NewHTTPScanOverviewWrapper(path string) ScanOverviewWrapper {
22+
validPath := configurePath(path)
1823
return &ScanOverviewHTTPWrapper{
19-
path: path,
24+
path: validPath,
2025
}
2126
}
2227

@@ -58,3 +63,19 @@ func (r *ScanOverviewHTTPWrapper) GetSCSOverviewByScanID(scanID string) (
5863
return nil, nil, errors.Errorf("response status code %d", resp.StatusCode)
5964
}
6065
}
66+
67+
// configurePath checks if the path is the default path, if not it writes the default path to the config file
68+
func configurePath(path string) string {
69+
if path != defaultPath {
70+
viper.Set(commonParams.ScsScanOverviewPathKey, defaultPath)
71+
configFilePath, err := configuration.GetConfigFilePath()
72+
if err != nil {
73+
logger.PrintfIfVerbose("Error getting config file path: %v", err)
74+
}
75+
err = configuration.SafeWriteSingleConfigKeyString(configFilePath, commonParams.ScsScanOverviewPathKey, defaultPath)
76+
if err != nil {
77+
logger.PrintfIfVerbose("Error writing Scan Overview path to config file: %v", err)
78+
}
79+
}
80+
return defaultPath
81+
}

0 commit comments

Comments
 (0)