2021-10-21 23:08:56 +03:00
|
|
|
// Copyright 2021 Security Scorecard Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Package localdir is local repo containing source code.
|
|
|
|
package localdir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-28 21:30:02 +03:00
|
|
|
"os"
|
|
|
|
"path"
|
2021-11-02 04:43:22 +03:00
|
|
|
"strings"
|
2021-10-21 23:08:56 +03:00
|
|
|
|
2022-01-12 22:49:01 +03:00
|
|
|
clients "github.com/ossf/scorecard/v4/clients"
|
2021-10-21 23:08:56 +03:00
|
|
|
)
|
|
|
|
|
2021-11-02 04:43:22 +03:00
|
|
|
var (
|
|
|
|
errNotDirectory = errors.New("not a directory")
|
|
|
|
errInvalidURI = errors.New("invalid URI")
|
|
|
|
)
|
|
|
|
|
|
|
|
var filePrefix = "file://"
|
2021-10-28 21:30:02 +03:00
|
|
|
|
|
|
|
type repoLocal struct {
|
|
|
|
path string
|
|
|
|
metadata []string
|
2021-10-21 23:08:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// URI implements Repo.URI().
|
2021-10-28 21:30:02 +03:00
|
|
|
func (r *repoLocal) URI() string {
|
2021-10-21 23:08:56 +03:00
|
|
|
return fmt.Sprintf("file://%s", r.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements Repo.String.
|
2021-10-28 21:30:02 +03:00
|
|
|
func (r *repoLocal) String() string {
|
|
|
|
return r.URI()
|
2021-10-21 23:08:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Org implements Repo.Org.
|
2021-10-28 21:30:02 +03:00
|
|
|
func (r *repoLocal) Org() clients.Repo {
|
|
|
|
return nil
|
2021-10-21 23:08:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid implements Repo.IsValid.
|
2021-10-28 21:30:02 +03:00
|
|
|
func (r *repoLocal) IsValid() error {
|
|
|
|
f, err := os.Stat(r.path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !f.IsDir() {
|
|
|
|
return fmt.Errorf("%w", errNotDirectory)
|
|
|
|
}
|
|
|
|
return nil
|
2021-10-21 23:08:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata implements Repo.Metadata.
|
2021-10-28 21:30:02 +03:00
|
|
|
func (r *repoLocal) Metadata() []string {
|
2021-10-21 23:08:56 +03:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendMetadata implements Repo.AppendMetadata.
|
2021-10-28 21:30:02 +03:00
|
|
|
func (r *repoLocal) AppendMetadata(m ...string) {
|
|
|
|
r.metadata = append(r.metadata, m...)
|
2021-10-21 23:08:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeLocalDirRepo returns an implementation of clients.Repo interface.
|
2021-10-28 21:30:02 +03:00
|
|
|
func MakeLocalDirRepo(pathfn string) (clients.Repo, error) {
|
2021-11-02 04:43:22 +03:00
|
|
|
if !strings.HasPrefix(pathfn, filePrefix) {
|
|
|
|
return nil, fmt.Errorf("%w", errInvalidURI)
|
|
|
|
}
|
|
|
|
p := path.Clean(pathfn[len(filePrefix):])
|
2021-10-28 21:30:02 +03:00
|
|
|
repo := &repoLocal{
|
|
|
|
path: p,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.IsValid(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error in IsValid: %w", err)
|
|
|
|
}
|
|
|
|
return repo, nil
|
2021-10-21 23:08:56 +03:00
|
|
|
}
|