-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package storage | ||
|
||
/* | ||
* AWS SSO CLI | ||
* Copyright (c) 2021-2024 Aaron Turner <synfinatic at gmail dot com> | ||
* | ||
* This program is free software: you can redistribute it | ||
* and/or modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or with the authors permission any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/jpillora/backoff" | ||
"github.com/synfinatic/aws-sso-cli/internal/config" | ||
) | ||
|
||
const ( | ||
FLOCK_FILE = "%s/storage.lock" | ||
) | ||
|
||
func FlockFile(expand bool) string { | ||
if strings.HasPrefix(flockFile, "%s") { | ||
return fmt.Sprintf(flockFile, config.ConfigDir(expand)) | ||
} else { | ||
return flockFile | ||
} | ||
} | ||
|
||
var sleeper = &backoff.Backoff{} | ||
var flockFile string = FLOCK_FILE | ||
|
||
func init() { | ||
sleeper = &backoff.Backoff{ | ||
Min: 10 * time.Millisecond, | ||
Max: 1 * time.Second, | ||
Factor: 2, | ||
Jitter: true, | ||
} | ||
} | ||
|
||
func FlockBlockerReset() { | ||
sleeper.Reset() | ||
} | ||
|
||
// Implments fslock.Blocker | ||
func FlockBlocker() error { | ||
time.Sleep(sleeper.Duration()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package storage | ||
|
||
/* | ||
* AWS SSO CLI | ||
* Copyright (c) 2021-2024 Aaron Turner <synfinatic at gmail dot com> | ||
* | ||
* This program is free software: you can redistribute it | ||
* and/or modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or with the authors permission any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/synfinatic/aws-sso-cli/internal/config" | ||
) | ||
|
||
func TestFlockFile(t *testing.T) { | ||
cfgDir := config.ConfigDir(false) | ||
assert.Equal(t, fmt.Sprintf("%s/storage.lock", cfgDir), FlockFile(false)) | ||
|
||
d, err := os.MkdirTemp("", "test-flockfile") | ||
assert.NoError(t, err) | ||
// need to set this here as we're not using the normal location during tests | ||
flockFile = path.Join(d, "storage.lock") | ||
assert.Equal(t, fmt.Sprintf("%s/storage.lock", d), FlockFile(false)) | ||
} | ||
|
||
func TestFlockBlocker(t *testing.T) { | ||
FlockBlockerReset() | ||
assert.NoError(t, FlockBlocker()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters