File: deperr.go

package info (click to toggle)
goiardi 0.11.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,692 kB
  • sloc: sql: 4,994; makefile: 158; sh: 95; python: 30
file content (157 lines) | stat: -rw-r--r-- 4,766 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (c) 2013-2017, Jeremy Bingham (<jeremy@goiardi.gl>)
 *
 * 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 cookbook

import (
	"fmt"
	"github.com/ctdk/goiardi/depgraph"
	"github.com/ctdk/goiardi/util"
	"github.com/tideland/golib/logger"
	"sort"
	"strings"
)

// Handling dependency errors, woo.

const (
	CookbookNotFound int = iota
	CookbookNoVersion
)

var cookbookVerErr = map[int]string{CookbookNotFound: "not found", CookbookNoVersion: "no version"}

type depMeta struct {
	version    string
	constraint versionConstraint
	notFound   bool
	noVersion  bool
}

type DependsError struct {
	depErr *depgraph.ConstraintError
}

func (d *DependsError) Error() string {
	errMap := d.ErrMap()
	return errMap["message"].(string)
}

func (d *DependsError) String() string {
	return d.Error()
}

func (d *DependsError) ErrMap() map[string]interface{} {
	errMap := make(map[string]interface{})

	allMsgs := make([]string, 0)
	notFound := make([]*versionConstraintError, 0)
	mostConstrained := make([]*versionConstraintError, 0)
	noVersion := make([]*versionConstraintError, 0)
	unsatisfiable := make([]*versionConstraintError, 0)

	for _, ce := range d.depErr.Violations {
		logger.Debugf("dependency violation: %+v", ce)
		verr := ce.Err.(*versionConstraintError)
		var unsat bool
		if verr.ParentCookbook != "^runlist_root^" {
			unsat = true
			unsatisfiable = append(unsatisfiable, verr)
		}
		if verr.ViolationType == CookbookNotFound {
			notFound = append(notFound, verr)
		} else {
			if unsat {
				mostConstrained = append(mostConstrained, verr)

			} else {
				noVersion = append(noVersion, verr)
			}
		}
	}
	notFoundStr := make([]string, 0)
	mostConstrainedStr := make([]string, 0)
	noVersionStr := make([]string, 0)
	unsatisfiableStr := make([]string, 0)

	if len(notFound) > 0 {
		for _, v := range notFound {
			notFoundStr = append(notFoundStr, v.Cookbook)
		}
		sort.Strings(notFoundStr)
		notFoundStr = util.RemoveDupStrings(notFoundStr)
	}
	if len(noVersion) > 0 {
		for _, v := range noVersion {
			noVersionStr = append(noVersionStr, fmt.Sprintf("(%s %s)", v.Cookbook, v.Constraint))
		}
		sort.Strings(noVersionStr)
		noVersionStr = util.RemoveDupStrings(noVersionStr)
	}
	if len(mostConstrained) > 0 {
		for _, v := range mostConstrained {
			failedDep := fmt.Sprintf("%s %s -> [%s %s]", v.Cookbook, v.Constraint, v.ParentCookbook, v.ParentConstraint)
			mostConstrainedStr = append(mostConstrainedStr, failedDep)
		}
		sort.Strings(mostConstrainedStr)
		mostConstrainedStr = util.RemoveDupStrings(mostConstrainedStr)
	}

	// Craft the messages
	if len(unsatisfiable) > 0 {
		for _, v := range unsatisfiable {
			var doesntExist string
			if v.ViolationType == CookbookNotFound {
				doesntExist = ", which does not exist,"
			}
			msgTmp := fmt.Sprintf("Unable to satisfy constraints on package %s%s due to solution constraint (%s %s). Solution constraints that may result in a constraint on %s: [(%s = %s) -> (%s %s)]", v.Cookbook, doesntExist, v.ParentCookbook, v.ParentConstraint, v.Cookbook, v.ParentCookbook, v.ParentVersion, v.Cookbook, v.Constraint)
			allMsgs = append(allMsgs, msgTmp)
			unsatisfiableStr = append(unsatisfiableStr, fmt.Sprintf("(%s %s)", v.ParentCookbook, v.ParentConstraint))
		}
		sort.Strings(unsatisfiableStr)
		util.RemoveDupStrings(unsatisfiableStr)
	}

	if len(notFoundStr) > 0 || len(noVersionStr) > 0 {
		msgTmp := "Run list contains invalid items:"
		if len(notFoundStr) > 0 {
			var werd string
			if len(notFoundStr) == 1 {
				werd = "cookbook"
			} else {
				werd = "cookbooks"
			}
			msgTmp = fmt.Sprintf("%s no such %s %s", msgTmp, werd, strings.Join(notFoundStr, ", "))
		}
		if len(noVersionStr) > 0 {
			msgTmp = fmt.Sprintf("%s no versions match the constraints on cookbook %s", msgTmp, strings.Join(noVersionStr, ", "))
		}
		msgTmp = strings.Join([]string{msgTmp, "."}, "")
		allMsgs = append(allMsgs, msgTmp)
	}

	errMap["message"] = strings.Join(allMsgs, "\n")

	errMap["non_existent_cookbooks"] = notFoundStr
	if len(unsatisfiable) > 0 {
		errMap["unsatisfiable_run_list_item"] = strings.Join(unsatisfiableStr, ", ")
		errMap["most_constrained_cookbooks"] = mostConstrainedStr
	} else {
		errMap["cookbooks_with_no_versions"] = noVersionStr
	}

	return errMap
}