func (client *clientImpl) ResolveVersion(packageName, version string) (common.Pin, error) { if err := common.ValidatePackageName(packageName); err != nil { return common.Pin{}, err } // Is it instance ID already? Don't bother calling the backend. if common.ValidateInstanceID(version) == nil { return common.Pin{PackageName: packageName, InstanceID: version}, nil } if err := common.ValidateInstanceVersion(version); err != nil { return common.Pin{}, err } client.Logger.Debugf("cipd: resolving version %q of %q...", version, packageName) return client.remote.resolveVersion(packageName, version) }
func (client *clientImpl) ProcessEnsureFile(r io.Reader) ([]common.Pin, error) { lineNo := 0 makeError := func(msg string) error { return fmt.Errorf("Failed to parse desired state (line %d): %s", lineNo, msg) } out := []common.Pin{} scanner := bufio.NewScanner(r) for scanner.Scan() { lineNo++ // Split each line into words, ignore white space. tokens := []string{} for _, chunk := range strings.Split(scanner.Text(), " ") { chunk = strings.TrimSpace(chunk) if chunk != "" { tokens = append(tokens, chunk) } } // Skip empty lines or lines starting with '#'. if len(tokens) == 0 || tokens[0][0] == '#' { continue } // Each line has a format "<package name> <version>". if len(tokens) != 2 { return nil, makeError("expecting '<package name> <version>' line") } err := common.ValidatePackageName(tokens[0]) if err != nil { return nil, makeError(err.Error()) } err = common.ValidateInstanceVersion(tokens[1]) if err != nil { return nil, makeError(err.Error()) } // Good enough. pin, err := client.ResolveVersion(tokens[0], tokens[1]) if err != nil { return nil, err } out = append(out, pin) } return out, nil }
func (r *remoteImpl) resolveVersion(packageName, version string) (pin common.Pin, err error) { if err = common.ValidatePackageName(packageName); err != nil { return } if err = common.ValidateInstanceVersion(version); err != nil { return } var reply struct { Status string `json:"status"` ErrorMessage string `json:"error_message"` InstanceID string `json:"instance_id"` } params := url.Values{} params.Add("package_name", packageName) params.Add("version", version) err = r.makeRequest("repo/v1/instance/resolve?"+params.Encode(), "GET", nil, &reply) if err != nil { return } switch reply.Status { case "SUCCESS": if common.ValidateInstanceID(reply.InstanceID) != nil { err = fmt.Errorf("Backend returned invalid instance ID: %s", reply.InstanceID) } else { pin = common.Pin{PackageName: packageName, InstanceID: reply.InstanceID} } case "PACKAGE_NOT_FOUND": err = fmt.Errorf("Package '%s' is not registered", packageName) case "INSTANCE_NOT_FOUND": err = fmt.Errorf("Package '%s' doesn't have instance with version '%s'", packageName, version) case "AMBIGUOUS_VERSION": err = fmt.Errorf("More than one instance of package '%s' match version '%s'", packageName, version) case "ERROR": err = errors.New(reply.ErrorMessage) default: err = fmt.Errorf("Unexpected backend response: %s", reply.Status) } return }