All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.artipie.helm.metadata.IndexYamlMapping Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2020 artipie.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.artipie.helm.metadata;

import com.artipie.asto.Content;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

/**
 * Mapping for content from index.yaml file.
 * @since 0.2
 */
@SuppressWarnings("unchecked")
public final class IndexYamlMapping {
    /**
     * Entries.
     */
    private static final String ENTRS = "entries";

    /**
     * Version.
     */
    private static final String VRSN = "version";

    /**
     * Mapping for fields from index.yaml file.
     */
    private final Map mapping;

    /**
     * Ctor.
     */
    public IndexYamlMapping() {
        this("apiVersion: v1\nentries:\n");
    }

    /**
     * Ctor.
     * @param yaml Index.yaml file
     */
    public IndexYamlMapping(final String yaml) {
        this((Map) new Yaml().load(yaml));
    }

    /**
     * Ctor.
     * @param mapfromindex Mapping for fields from index.yaml file
     */
    public IndexYamlMapping(final Map mapfromindex) {
        this.mapping = mapfromindex;
    }

    /**
     * Obtain mapping for `entries`.
     * @return Mapping for `entries`.
     */
    public Map entries() {
        this.mapping.computeIfAbsent(IndexYamlMapping.ENTRS, k -> new HashMap<>());
        return (Map) this.mapping.get(IndexYamlMapping.ENTRS);
    }

    /**
     * Obtain mapping for specified chart from `entries`.
     * @param chartname Chart name
     * @return Mapping for specified chart from `entries`.
     */
    public List> byChart(final String chartname) {
        this.entries().computeIfAbsent(chartname, nothing -> new ArrayList>(0));
        return (List>) this.entries().get(chartname);
    }

    /**
     * Obtains entry with specified version and chart name.
     * @param chartname Chart name
     * @param version Version of chart
     * @return Entry if version for specified name exists, empty otherwise.
     */
    public Optional> byChartAndVersion(final String chartname,
        final String version) {
        return this.byChart(chartname).stream()
            .filter(entry -> entry.get(IndexYamlMapping.VRSN).equals(version))
            .findFirst();
    }

    /**
     * Add info about chart to the existing mapping.
     * @param name Chart name
     * @param versions Collection with mapping for different versions of specified chart
     */
    public void addChartVersions(final String name, final List> versions) {
        final Map entr = this.entries();
        versions.forEach(vers -> vers.put("created", IndexYamlMapping.now()));
        if (entr.containsKey(name)) {
            final List> existed = this.byChart(name);
            for (final Map vers : versions) {
                final Optional> opt;
                opt = this.byChartAndVersion(name, (String) vers.get(IndexYamlMapping.VRSN));
                if (opt.isPresent()) {
                    existed.removeIf(
                        chart -> chart.get(IndexYamlMapping.VRSN)
                            .equals(opt.get().get(IndexYamlMapping.VRSN))
                    );
                }
                existed.add(vers);
            }
        } else {
            entr.put(name, versions);
        }
    }

    /**
     * Add entries to the existing mapping.
     * @param entries Entries that should be added
     * @return Itself.
     */
    public IndexYamlMapping addEntries(final Map> entries) {
        this.mapping.put(IndexYamlMapping.ENTRS, entries);
        return this;
    }

    /**
     * Converts mapping to bytes.
     * @return Bytes if entries mapping contains any chart, empty otherwise.
     */
    public Optional toContent() {
        final Optional res;
        if (this.entries().isEmpty()) {
            res = Optional.empty();
        } else {
            this.mapping.put("generated", IndexYamlMapping.now());
            res = Optional.of(new Content.From(this.toString().getBytes()));
        }
        return res;
    }

    @Override
    public String toString() {
        final DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setPrettyFlow(true);
        return new Yaml(options).dump(this.mapping);
    }

    /**
     * Obtains current time.
     * @return Current time.
     */
    private static String now() {
        return ZonedDateTime.now().format(IndexYaml.TIME_FORMATTER);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy