use different file name for attestation files on windows

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-12-09 17:32:55 -07:00
parent acfdae466e
commit e97d01f265
3 changed files with 27 additions and 5 deletions

View file

@ -47,6 +47,11 @@ func NewDownloadCmd(f *cmdutil.Factory, runF func(*Options) error) *cobra.Comman
Any associated bundle(s) will be written to a file in the
current directory named after the artifact's digest. For example, if the
digest is "sha256:1234", the file will be named "sha256:1234.jsonl".
Because colons are special characters in Windows and cannot be used in
file names, the digest will be formatted with a dash separating the algorithm
from the digest. For example, if the digest is "sha256:1234", the file
will be named "sha256-1234.jsonl".
`, "`"),
Example: heredoc.Doc(`
# Download attestations for a local artifact linked with an organization

View file

@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"os"
"runtime"
"strings"
"github.com/cli/cli/v2/pkg/cmd/attestation/api"
)
@ -20,6 +22,12 @@ type LiveStore struct {
}
func (s *LiveStore) createJSONLinesFilePath(artifact string) string {
if runtime.GOOS == "windows" {
// Colons are special characters in Windows and cannot be used in file names.
// Replace them with dashes to avoid issues.
artifact = strings.ReplaceAll(artifact, ":", "-")
}
path := fmt.Sprintf("%s.jsonl", artifact)
if s.outputPath != "" {
return fmt.Sprintf("%s/%s", s.outputPath, path)

View file

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"runtime"
"testing"
"github.com/cli/cli/v2/pkg/cmd/attestation/api"
@ -31,7 +32,15 @@ func TestCreateJSONLinesFilePath(t *testing.T) {
artifact, err := artifact.NewDigestedArtifact(oci.MockClient{}, "../test/data/sigstore-js-2.1.0.tgz", "sha512")
require.NoError(t, err)
outputFileName := fmt.Sprintf("%s.jsonl", artifact.DigestWithAlg())
expectedPosixFileName := fmt.Sprintf("%s.jsonl", artifact.DigestWithAlg())
expectedWindowsFileName := fmt.Sprintf("%s-%s.jsonl", artifact.Algorithm(), artifact.Digest())
var expectedFileName string
if runtime.GOOS == "windows" {
expectedFileName = expectedWindowsFileName
} else {
expectedFileName = expectedPosixFileName
}
testCases := []struct {
name string
@ -41,22 +50,22 @@ func TestCreateJSONLinesFilePath(t *testing.T) {
{
name: "with output path",
outputPath: tempDir,
expected: path.Join(tempDir, outputFileName),
expected: path.Join(tempDir, expectedFileName),
},
{
name: "with nested output path",
outputPath: path.Join(tempDir, "subdir"),
expected: path.Join(tempDir, "subdir", outputFileName),
expected: path.Join(tempDir, "subdir", expectedFileName),
},
{
name: "with output path with beginning slash",
outputPath: path.Join("/", tempDir, "subdir"),
expected: path.Join("/", tempDir, "subdir", outputFileName),
expected: path.Join("/", tempDir, "subdir", expectedFileName),
},
{
name: "without output path",
outputPath: "",
expected: outputFileName,
expected: expectedFileName,
},
}