2018-04-25 09:01:00 +01:00
|
|
|
// Copyright 2018 jsonnet-bundler 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 pkg
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2019-10-16 16:34:53 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"io"
|
2018-04-25 09:01:00 +01:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2019-10-16 12:07:10 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
2019-07-22 17:32:53 +02:00
|
|
|
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
2018-04-25 09:01:00 +01:00
|
|
|
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
VersionMismatch = errors.New("multiple colliding versions specified")
|
|
|
|
|
)
|
|
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
func Ensure(want spec.JsonnetFile, vendorDir string, locks map[string]spec.Dependency) ([]spec.Dependency, error) {
|
|
|
|
|
var list []spec.Dependency
|
|
|
|
|
for _, d := range want.Dependencies {
|
|
|
|
|
l, present := locks[d.Name]
|
2019-04-24 18:20:16 +02:00
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
// already locked and the integrity is intact
|
|
|
|
|
if present && check(l, vendorDir) {
|
|
|
|
|
list = append(list, l)
|
|
|
|
|
continue
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|
2018-08-01 13:42:47 +02:00
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
// either not present or not intact: download again
|
|
|
|
|
dir := filepath.Join(vendorDir, d.Name)
|
|
|
|
|
os.RemoveAll(dir)
|
2019-03-11 14:21:49 +01:00
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
locked, err := download(d, vendorDir)
|
2018-08-02 08:47:43 +02:00
|
|
|
if err != nil {
|
2019-10-16 16:34:53 +02:00
|
|
|
return nil, errors.Wrap(err, "downloading")
|
2018-08-02 08:47:43 +02:00
|
|
|
}
|
2019-10-16 16:34:53 +02:00
|
|
|
list = append(list, *locked)
|
|
|
|
|
}
|
2018-08-02 08:47:43 +02:00
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
for _, d := range list {
|
|
|
|
|
f, err := jsonnetfile.Load(filepath.Join(vendorDir, d.Name, jsonnetfile.File))
|
2018-05-13 06:55:13 -07:00
|
|
|
if err != nil {
|
2019-10-16 16:34:53 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-05-13 06:55:13 -07:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-04-25 09:01:00 +01:00
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
nested, err := Ensure(f, vendorDir, locks)
|
2018-05-13 06:55:13 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-04-25 09:01:00 +01:00
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
list = append(list, nested...)
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
return list, nil
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
func download(d spec.Dependency, vendorDir string) (*spec.Dependency, error) {
|
|
|
|
|
var p Interface
|
|
|
|
|
switch {
|
|
|
|
|
case d.Source.GitSource != nil:
|
|
|
|
|
p = NewGitPackage(d.Source.GitSource)
|
|
|
|
|
case d.Source.LocalSource != nil:
|
|
|
|
|
p = NewLocalPackage(d.Source.LocalSource)
|
2018-04-25 09:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
if p == nil {
|
|
|
|
|
return nil, errors.New("either git or local source is required")
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|
2019-10-16 16:34:53 +02:00
|
|
|
|
|
|
|
|
version, err := p.Install(context.TODO(), d.Name, vendorDir, d.Version)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sum := hashDir(filepath.Join(vendorDir, d.Name))
|
|
|
|
|
|
|
|
|
|
return &spec.Dependency{
|
|
|
|
|
Name: d.Name,
|
|
|
|
|
Source: d.Source,
|
|
|
|
|
Version: version,
|
|
|
|
|
Sum: sum,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func check(d spec.Dependency, vendorDir string) bool {
|
|
|
|
|
if d.Sum == "" {
|
|
|
|
|
// no sum available, need to download
|
|
|
|
|
return false
|
2018-05-13 06:55:13 -07:00
|
|
|
}
|
2018-04-25 09:01:00 +01:00
|
|
|
|
2019-10-16 16:34:53 +02:00
|
|
|
dir := filepath.Join(vendorDir, d.Name)
|
|
|
|
|
sum := hashDir(dir)
|
|
|
|
|
return d.Sum == sum
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hashDir(dir string) string {
|
|
|
|
|
hasher := sha256.New()
|
|
|
|
|
|
|
|
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
if _, err := io.Copy(hasher, f); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString(hasher.Sum(nil))
|
2018-04-25 09:01:00 +01:00
|
|
|
}
|