Merge pull request #13 from brancz/fix-install-from-lock

Fix install from lock-file
This commit is contained in:
Frederic Branczyk 2018-08-01 14:23:32 +02:00 committed by GitHub
commit ba2afc3084
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 16 deletions

View file

@ -227,13 +227,7 @@ func parseGithubDependency(urlString string) *spec.Dependency {
func installCommand(jsonnetHome string, urls ...*url.URL) int { func installCommand(jsonnetHome string, urls ...*url.URL) int {
workdir := "." workdir := "."
useLock, err := pkg.LockExists(workdir) jsonnetfile, isLock, err := pkg.ChooseJsonnetFile(workdir)
if err != nil {
kingpin.Fatalf("failed to check if jsonnetfile.lock.json exists: %v", err)
return 1
}
jsonnetfile, err := pkg.ChooseJsonnetFile(workdir)
if err != nil { if err != nil {
kingpin.Fatalf("failed to choose jsonnetfile: %v", err) kingpin.Fatalf("failed to choose jsonnetfile: %v", err)
return 1 return 1
@ -288,14 +282,14 @@ func installCommand(jsonnetHome string, urls ...*url.URL) int {
return 3 return 3
} }
lock, err := pkg.Install(context.TODO(), jsonnetfile, m, jsonnetHome) lock, err := pkg.Install(context.TODO(), isLock, jsonnetfile, m, jsonnetHome)
if err != nil { if err != nil {
kingpin.Fatalf("failed to install: %v", err) kingpin.Fatalf("failed to install: %v", err)
return 3 return 3
} }
// If installing from lock file there is no need to write any files back. // If installing from lock file there is no need to write any files back.
if !useLock { if !isLock {
b, err := json.MarshalIndent(m, "", " ") b, err := json.MarshalIndent(m, "", " ")
if err != nil { if err != nil {
kingpin.Fatalf("failed to encode jsonnet file: %v", err) kingpin.Fatalf("failed to encode jsonnet file: %v", err)
@ -339,7 +333,9 @@ func updateCommand(jsonnetHome string, urls ...*url.URL) int {
return 3 return 3
} }
lock, err := pkg.Install(context.TODO(), jsonnetfile, m, jsonnetHome) // When updating, the lockfile is explicitly ignored.
isLock := false
lock, err := pkg.Install(context.TODO(), isLock, jsonnetfile, m, jsonnetHome)
if err != nil { if err != nil {
kingpin.Fatalf("failed to install: %v", err) kingpin.Fatalf("failed to install: %v", err)
return 3 return 3

View file

@ -33,7 +33,7 @@ var (
VersionMismatch = errors.New("multiple colliding versions specified") VersionMismatch = errors.New("multiple colliding versions specified")
) )
func Install(ctx context.Context, dependencySourceIdentifier string, m spec.JsonnetFile, dir string) (lock *spec.JsonnetFile, err error) { func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string, m spec.JsonnetFile, dir string) (lock *spec.JsonnetFile, err error) {
lock = &spec.JsonnetFile{} lock = &spec.JsonnetFile{}
for _, dep := range m.Dependencies { for _, dep := range m.Dependencies {
tmp := filepath.Join(dir, ".tmp") tmp := filepath.Join(dir, ".tmp")
@ -58,6 +58,14 @@ func Install(ctx context.Context, dependencySourceIdentifier string, m spec.Json
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to install package") return nil, errors.Wrap(err, "failed to install package")
} }
// If dependencies are being installed from a lock file, the transitive
// dependencies are not questioned, but the locked dependencies are
// just installed.
if isLock {
continue
}
lock.Dependencies, err = insertDependency(lock.Dependencies, spec.Dependency{ lock.Dependencies, err = insertDependency(lock.Dependencies, spec.Dependency{
Name: dep.Name, Name: dep.Name,
Source: dep.Source, Source: dep.Source,
@ -87,7 +95,7 @@ func Install(ctx context.Context, dependencySourceIdentifier string, m spec.Json
return nil, errors.Wrap(err, "failed to move package") return nil, errors.Wrap(err, "failed to move package")
} }
filepath, err := ChooseJsonnetFile(destPath) filepath, isLock, err := ChooseJsonnetFile(destPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -98,7 +106,7 @@ func Install(ctx context.Context, dependencySourceIdentifier string, m spec.Json
return nil, err return nil, err
} }
depsInstalledByDependency, err := Install(ctx, filepath, depsDeps, dir) depsInstalledByDependency, err := Install(ctx, isLock, filepath, depsDeps, dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -152,21 +160,23 @@ func LockExists(dir string) (bool, error) {
return true, nil return true, nil
} }
func ChooseJsonnetFile(dir string) (string, error) { func ChooseJsonnetFile(dir string) (string, bool, error) {
lockfile := path.Join(dir, JsonnetLockFile) lockfile := path.Join(dir, JsonnetLockFile)
jsonnetfile := path.Join(dir, JsonnetFile) jsonnetfile := path.Join(dir, JsonnetFile)
filename := lockfile filename := lockfile
isLock := true
lockExists, err := LockExists(dir) lockExists, err := LockExists(dir)
if err != nil { if err != nil {
return "", err return "", false, err
} }
if !lockExists { if !lockExists {
filename = jsonnetfile filename = jsonnetfile
isLock = false
} }
return filename, err return filename, isLock, err
} }
func LoadJsonnetfile(filepath string) (spec.JsonnetFile, error) { func LoadJsonnetfile(filepath string) (spec.JsonnetFile, error) {