package emulator import ( "encoding/json" "errors" "fmt" "io/fs" "os" "github.com/go-logr/logr" ) const ( envFilePathDir = "RCLI_WORKSPACE_DIR" defaultDir = "./" ) func GetWorkspaceDir(logger logr.Logger) (string, error) { dir := os.Getenv(envFilePathDir) // If env var is empty or doesn't exist, use default if dir == "" { logger.Info("Environment variable was not set. Using default value", "envvar", envFilePathDir) dir = defaultDir } if dir != defaultDir { // If directory does not exist, return error // Need read, write, and execute permissions fi, err := os.Stat(dir) if errors.Is(err, fs.ErrNotExist) { return "", fmt.Errorf("workspace directory could not be found: %v", err) } if err != nil { return "", err } perms := fi.Mode() // Check for specific permissions: user rwx if perms&0b111000000 != 0b111000000 { return "", fmt.Errorf("workspace directory perms aren't accessible by remotecli") } } logger.Info("Workspace directory successfully set", "workspace", dir) return dir, nil } type workspace struct { ProjectID string `json:"project_ID"` BannerID string `json:"banner_ID"` StoreID string `json:"store_ID"` DisablePerSessionSubscription bool `json:"disable_per_session_subscription"` } func newWorkspace(path string) (*workspace, error) { var ws workspace data, err := os.ReadFile(path) if err != nil { return &ws, err } err = json.Unmarshal(data, &ws) if err != nil { return &ws, err } return &ws, nil } func (ws workspace) save(path string) error { data, err := json.Marshal(ws) if err != nil { return err } err = os.WriteFile(path, data, 0644) if err != nil { return err } return nil } func (ws workspace) string() string { strings := []string{ws.BannerID, ws.StoreID} for enum, item := range strings { if item == "" { strings[enum] = UNSET } } return fmt.Sprintf("BannerID: %s, StoreID: %s\n", strings[0], strings[1]) }